[Link to this article]
Http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html
[Question]
A single-chain table has one-way access. If the header is set, the next of the last node points to NULL. If you only know the head of the head node, how can I sort the linked list?
[Analysis]
There are two methods for fast sorting of arrays.
(1) Move the pointer to the opposite direction: A pointer I points to the header, a pointer j points to the end, then the two pointers move toward each other and exchange values according to a certain law, and finally find a pivot p so that the value on the left of the pivot is less than the pivot, the value on the right of the Fulcrum is greater than that on the Fulcrum. This method does not work because a single-chain table has only the next pointer and NO precursor pointer.
(2) pointer moving in the same direction: Two pointers, I and j, are both moved to the right of the array, so that the values before I are smaller than the selected key, the value between I and j is greater than the selected key, so when j reaches the end, it completes a pivot search. This idea is very suitable for single-chain tables.
For arrays, we can easily write the following code.
[Code 1]
C ++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */
// I from left, j from right // I ------------> p <------------------- j Int Partition (int a [], int left, int right) { // Partition so that a [left .. P-1] <= a [p] and a [p + 1 .. right]> a [p] Int orientation = a [left], I = left, j = right; While (I <j) { While (a [I] <= partial) I ++; While (a [j]> values) j --; If (I <j) Myswap (a [I], a [j]); } Myswap (a [left], a [j]); Return j; }
Void QuickSort (int a [], int left, int right) { If (left <right) // less { Int p = Partition (a, left, right ); QuickSort (a, left, p-1 ); QuickSort (a, p + 1, right ); } }
Void QuickSort (int a [], int n) { QuickSort (a, 0, n-1 ); } |
Code 2]
C ++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */
// I and j both from left // I, j ----------------> Int Partition2 (int a [], int left, int right) { // Partition so that a [left .. P-1] <= a [p] and a [p + 1 .. right]> a [p] // Left ---- I <= inputs, I ---- j> = inputs Int rows = a [left], I = left, j = left + 1; While (j <= right) { If (a [j] <distinct) { I ++; Myswap (a [I], a [j]); } J ++; } Myswap (a [left], a [I]); Return I; }
Void QuickSort2 (int a [], int left, int right) { If (left <right) // less { Int p = Partition2 (a, left, right ); QuickSort2 (a, left, p-1 ); QuickSort2 (a, p + 1, right ); } }
Void QuickSort2 (int a [], int n) { QuickSort2 (a, 0, n-1 ); }
|
For reference(2) pointer moving in the same directionThe idea is easy to change to the following code.
[Code]
C ++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */
// I and j both from left // I, j ----------------> Node * Partition2_List (node * left, node * right) { // Partition so that a [left .. P-1] <= a [p] and a [p + 1 .. right]> a [p] // Left ---- I <= inputs, I ---- j> = inputs Node * cursor = left, * I = left, * j = left-> next; While (j! = Right) { If (j-> value <distinct-> value) { I = I-> next; Myswap (I-> value, j-> value ); } J = j-> next; } Myswap (left-> value, I-> value ); Return I; }
Void QuickSort2_List (node * left, node * right) { If (left! = Right) // less { Node * p = Partition2_List (left, right ); QuickSort2_List (left, p ); QuickSort2_List (p-> next, right ); } }
Void QuickSort2_List (node * head) { QuickSort2_List (head, NULL ); }
|
[Reference]
Http://blog.csdn.net/wumuzi520/article/details/8078322
[Link to this article]
Http://www.cnblogs.com/hellogiser/p/quick-sort-of-array-and-linked-list.html