code_junkie

Merge Sort a Linked List

Nov 10th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. typedef struct _aList {
  2. struct _aList* next;
  3. struct _aList* prev; // Optional.
  4. // some data
  5. } aList;
  6.  
  7. aList *merge_sort_list(aList *list,int (*compare)(aList *one,aList *two))
  8. {
  9. int listSize=1,numMerges,leftSize,rightSize;
  10. aList *tail,*left,*right,*next;
  11. if (!list || !list->next) return list; // Trivial case
  12.  
  13. do { // For each power of two<=list length
  14. numMerges=0,left=list;tail=list=0; // Start at the start
  15.  
  16. while (left) { // Do this list_len/listSize times:
  17. numMerges++,right=left,leftSize=0,rightSize=listSize;
  18. // Cut list into two halves (but don't overrun)
  19. while (right && leftSize<listSize) leftSize++,right=right->next;
  20. // Run through the lists appending onto what we have so far.
  21. while (leftSize>0 || (rightSize>0 && right)) {
  22. // Left empty, take right OR Right empty, take left, OR compare.
  23. if (!leftSize) {next=right;right=right->next;rightSize--;}
  24. else if (!rightSize || !right) {next=left;left=left->next;leftSize--;}
  25. else if (compare(left,right)<0) {next=left;left=left->next;leftSize--;}
  26. else {next=right;right=right->next;rightSize--;}
  27. // Update pointers to keep track of where we are:
  28. if (tail) tail->next=next; else list=next;
  29. // Sort prev pointer
  30. next->prev=tail; // Optional.
  31. tail=next;
  32. }
  33. // Right is now AFTER the list we just sorted, so start the next sort there.
  34. left=right;
  35. }
  36. // Terminate the list, double the list-sort size.
  37. tail->next=0,listSize<<=1;
  38. } while (numMerges>1); // If we only did one merge, then we just sorted the whole list.
  39. return list;
  40. }
  41.  
  42. typedef struct _aList {
  43. struct _aList* next;
  44. struct _aList* prev; // Optional.
  45. // some data
  46. } aList;
  47.  
  48. aList* merge_sort_list_recursive(aList *list,int (*compare)(aList *one,aList *two))
  49. {
  50. if (!list || !list->next) return list; // Trivial case.
  51. aList *right=list,*temp=list,*last=list,*result=0,*next=0,*tail=0;
  52. // Find halfway through the list (by running two pointers, one at twice the speed of the other).
  53. while (temp && temp->next) {last=right;right=right->next,temp=temp->next->next;}
  54. last->next=0; // Break the list in two. (prev pointers are broken here, but we fix later)
  55. // Recurse on the two smaller lists:
  56. list=merge_sort_list_recursive(list,compare);
  57. right=merge_sort_list_recursive(right,compare);
  58. // Merge:
  59. while (list || right)
  60. {
  61. // Take from empty lists, or compare:
  62. if (!right) {next=list;list=list->next;}
  63. else if (!list) {next=right;right=right->next;}
  64. else if (compare(list,right)<0) {next=list;list=list->next;}
  65. else {next=right;right=right->next;}
  66. if (!result) result=next; else tail->next=next;
  67. next->prev=tail; // Optional.
  68. tail=next;
  69. }
  70. return result;
  71. }
  72.  
  73. public int[] msort(int[] a) {
  74. if (a.Length > 1) {
  75. int min = a.Length / 2;
  76. int max = min;
  77.  
  78. int[] b = new int[min];
  79. int[] c = new int[max]; // dividing main array into two half arrays
  80. for (int i = 0; i < min; i++) {
  81. b[i] = a[i];
  82. }
  83.  
  84. for (int i = min; i < min + max; i++) {
  85. c[i - min] = a[i];
  86. }
  87.  
  88. b = msort(b);
  89. c = msort(c);
  90.  
  91. int x = 0;
  92. int y = 0;
  93. int z = 0;
  94.  
  95. while (b.Length != y && c.Length != z) {
  96. if (b[y] < c[z]) {
  97. a[x] = b[y];
  98. //r--
  99. x++;
  100. y++;
  101. } else {
  102. a[x] = c[z];
  103. x++;
  104. z++;
  105. }
  106. }
  107.  
  108. while (b.Length != y) {
  109. a[x] = b[y];
  110. x++;
  111. y++;
  112. }
  113.  
  114. while (c.Length != z) {
  115. a[x] = c[z];
  116. x++;
  117. z++;
  118. }
  119. }
  120.  
  121. return a;
  122. }
  123.  
  124. struct node {
  125. int data1;
  126. int data2;
  127. struct node *next;
  128. };
Add Comment
Please, Sign In to add comment