0% found this document useful (0 votes)
45 views3 pages

Algorithms

Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in O(n log n) time.

Uploaded by

yogita123456
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Algorithms

Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in O(n log n) time.

Uploaded by

yogita123456
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Which recursive sorting technique always makes recursive calls to sort subarrays that

are about half size of the original array?

Please enable JavaScript to view this page content properly.

Answer: Mergesort always makes recursive calls to sort subarrays that are about half size of the
original array, resulting in O(n log n) time.

Write a program to reverse a singly linked list

Please enable JavaScript to view this page content properly.


Node* ReverseList( Node ** List )
{

Node *temp1 = *List;


Node * temp2 = NULL;
Node * temp3 = NULL;

while ( temp1 )
{
*List = temp1; //set the head to last node
temp2= temp1->pNext; // save the next ptr in temp2
temp1->pNext = temp3; // change next to privous
temp3 = temp1;
temp1 = temp2;
}

return *List;
}

Write a program to delete a node in double linked list

Please enable JavaScript to view this page content properly.


void deleteNode(node *n)
{
node *np = n->prev;
node *nn = n->next;
np->next = n->next;
nn->prev = n->prev;
delete n;
}

Write a program to sort a linked list

Please enable JavaScript to view this page content properly.


struct node
{
int value;
node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)


{
node* first,second,temp;
first= Head;
while(first!=null)
{
second=first->NEXT;
while(second!=null)
{
if(first->value < second->value)
{
temp = new node();
temp->value=first->value;
first->value=second->value;
second->value=temp->value;
delete temp;
}
second=second->NEXT;
}

first=first->NEXT;
}
}

Write a program to reverse a string

Please enable JavaScript to view this page content properly.


void ReverseString (char *String)
{
char *Begin = String;
char *End = String + strlen(String) - 1;
char TempChar = '\0';

while (Begin < End)


{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End--;
}
}
Write a program to insert a node in a sorted linked list

Please enable JavaScript to view this page content properly.


void sortedInsert(Node * head, Node* newNode)
{
Node *current = head;

// traverse the list until you find item bigger the // new node value
//
while (current!= NULL && current->data < newNode->data)
{
current = current->next);
}
//
// insert the new node before the big item
//
newNode->next = current->next;
current = newNode;
}

You might also like