Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Merge Sort</p> <pre><code> #include&lt;stdio.h&gt; struct node { int number; struct node *next; }; /* add a node to the linked list */ struct node *addnode(int number, struct node *next); /* preform merge sort on the linked list */ struct node *mergesort(struct node *head); /* merge the lists.. */ struct node *merge(struct node *head_one, struct node *head_two); int main(void) { struct node *head; struct node *current; struct node *next; int test[] = {82, 13, 22, 61, 12, 52, 41, 75, 98, 20}; int i; head = NULL; /* insert some numbers into the linked list */ for(i = 0; i &lt; 10; i++) head = addnode(test[i], head); /* sort the list */ head = mergesort(head); /* print the list */ printf(" before after\n"), i = 0; for(current = head; current != NULL; current = current-&gt;next) printf("%4d\t%4d\n", test[i++], current-&gt;number); /* free the list */ for(current = head; current != NULL; current = next) next = current-&gt;next, free(current); /* done... */ return 0; } /* add a node to the linked list */ struct node *addnode(int number, struct node *next) { struct node *tnode; tnode = (struct node*)malloc(sizeof(*tnode)); if(tnode != NULL) { tnode-&gt;number = number; tnode-&gt;next = next; } return tnode; } /* preform merge sort on the linked list */ struct node *mergesort(struct node *head) { struct node *head_one; struct node *head_two; if((head == NULL) || (head-&gt;next == NULL)) return head; head_one = head; head_two = head-&gt;next; while((head_two != NULL) &amp;&amp; (head_two-&gt;next != NULL)) { head = head-&gt;next; head_two = head-&gt;next-&gt;next; } head_two = head-&gt;next; head-&gt;next = NULL; return merge(mergesort(head_one), mergesort(head_two)); } /* merge the lists.. */ struct node *merge(struct node *head_one, struct node *head_two) { struct node *head_three; if(head_one == NULL) return head_two; if(head_two == NULL) return head_one; if(head_one-&gt;number &lt; head_two-&gt;number) { head_three = head_one; head_three-&gt;next = merge(head_one-&gt;next, head_two); } else { head_three = head_two; head_three-&gt;next = merge(head_one, head_two-&gt;next); } return head_three; } </code></pre> <p>QuickSort</p> <pre><code> #include &lt;iostream.h&gt; class LinkList{ private: struct Node { Node* prev; int value; Node *next; }; Node *first,*t1; void _display(Node *temp){ if (temp) { cout&lt;&lt;temp-&gt;value&lt;&lt;" , "; _display(temp-&gt;next); } } public : LinkList(int order){ t1 = new Node(); t1-&gt;prev = NULL; t1-&gt;value = order; t1-&gt;next = NULL; first = t1; } ~LinkList(){ destroy(first); } void destroy(Node*current){ Node *temp; if(current != NULL){ temp = current-&gt;next; delete current; current = temp; destroy(current); } } void add(int order){ Node *t2 = new Node(); t2-&gt;prev = t1; t2-&gt;value = order; t2-&gt;next = NULL; t1-&gt;next = t2; t1=t1-&gt;next; } void operator&lt;&lt;(int order){ add(order); } void display(){ _display(first); } void sort() { Node *current, *cur; for(current = first; current-&gt;next != NULL; current = current-&gt;next) { Node *minimum = current; for(cur = current ; cur != NULL; cur = cur-&gt;next) { if(minimum-&gt;value &gt; cur-&gt;value) { minimum = cur; } } if(minimum != current) { Node *current_previous, *current_next, *min_previous, *min_next; // Initialize them current_next = current-&gt;next; min_previous = minimum-&gt;prev; min_next = minimum-&gt;next; current_previous = current-&gt;prev; if(current_previous == NULL) { // Change the First Node first = minimum; } if(current-&gt;next == minimum) { // Nodes are Adjacent minimum-&gt;prev = current_previous; minimum-&gt;next = current; current-&gt;prev = minimum; current-&gt;next = min_next; if(min_next) { min_next-&gt;prev = current; } if(current_previous) { current_previous-&gt;next = minimum; } } else { minimum-&gt;prev = current_previous; minimum-&gt;next = current_next; current-&gt;prev = min_previous; current-&gt;next = min_next; if(current_next) { current_next-&gt;prev = minimum; } if(min_previous) { min_previous-&gt;next = current; } if(min_next) { min_next-&gt;prev = current; } if(current_previous) { current_previous-&gt;next = minimum; } } current = minimum; } } } }; void main(){ cout&lt;&lt;"\n\nLinkList =\n"; LinkList *list=new LinkList(4); *list&lt;&lt;7; *list&lt;&lt;5; *list&lt;&lt;3; *list&lt;&lt;11; list-&gt;display(); list-&gt;sort(); cout&lt;&lt;"\n\nSorting\n"; list-&gt;display(); delete list; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload