A small exploration of quick sorting

Source: Internet
Author: User
Tags benchmark

The quick sorting algorithm is a division exchange method, which uses the Division Control Method for sorting.

1 public static void quikSort(int a[],int left,int right)2     {3         if(left >= right)return;4         int p = partition(a,left,right);5         quikSort(a,left,p-1);6         quikSort(a,p+1,right);7     }

 

Start, the reference element of the division method partition () in the quick sort is selected as the value at the leftmost left:

1 public static int partition (int A [], int left, int right) 2 {3 int Index = left; 4 int value = A [Index]; // base element 5 while (left <right) 6 {7 // comparison with equal signs. For repeated elements such as 4, 3, 4, the detection pointer will be moved, otherwise, 8 while (left <Right & A [right]> = value) 9 {10 right --; 11} 12 A [left] = A [right]; 13 // If (left <right) 14 // {15 /// check whether left and right overlap. Although it can avoid comparing its own value with its own, it has an additional judgment and comparison; the effect has not improved 16 // left ++; 17 //} 18 // left ++; // It is an error to directly move the left pointer. When the Left and Right pointers are repeated, this statement is not executed correctly, and it is forced to be added once! 19 while (left <Right & A [left] <= value) 20 {21 left ++; 22} 23 A [right] = A [left]; 24} 25 // The exit loop must be left = right, when left and right overlap, the position 26 A [left] = value; 27 // system is found when the base element is in place. out. println ("division point (final position of the element):" + Left); 28 return left; 29}

 

 

Later, I thought of using random values to improve the quick sorting to some extent (for example, in the case of a basic order) (the execution result of the following code is incorrect), as shown below:

1 public static int partition2 (int A [], int left, int right) 2 {3 4 int Index = left + R. nextint (right-left + 1); // randomly selects the benchmark element 5 Int value = A [Index]; // uses value to save the currently selected pivot element, you can leave a position of 6 While (left <right) 7 {8 // with equal signs in the comparison. For repeated elements such as, 4, the detection pointer will be moved, otherwise, it will take an infinite loop of 9 10 // first start detection on the right side, for; 9 is selected as the value, start right directly -- it will not be 11 while (left <Right & A [right]> = value) 12 {13 right --; 14} 15 A [Index] = A [right]; 16 Index = right; 17 while (left <Right & A [left] <= value) 18 {19 Left ++; 20} 21 A [right] = A [left]; 22 Index = left; 23} 24 A [left] = value; 25 return left; 26}

 

Think about the results and find out the cause of the above Code error: You must switch the benchmark element to the leftmost or rightmost (put it to the leftmost and start right detection, right-most detection should start from left), so that the Left position is from the edge position, from the left to the right, respectively, to the real position of the reference element, instead of moving the index from the original position of the pivot element at the beginning.

 

Modify the Code as follows:

1 public static int partition (int A [], int left, int right) 2 {3 4 int Index = left + R. nextint (right-left + 1); // randomly selects the Benchmark 5 Int value = A [Index];
6 // swap the selected baseline element to the left-most left, and the Right check must start first. For example, [, 4 ], if a [0] = 4 is selected as the reference element, and the detection from left is incorrect. 7 int temp = A [left]; 8 A [left] = A [Index]; 9 A [Index] = temp; 10 11 while (left <right) 12 {13 14 While (left <Right & A [right]> = value) 15 {16 Right --; 17}
A [left] = A [right]; 18 while (left <Right & A [left] <= value) 19 {20 left ++; 21} 22 A [right] = A [left]; 23} 24 A [left] = value; 25 return left; 26}

 

Another way to implement the Division function is to determine the final position of the benchmark element from one direction (each time an element is found smaller than the benchmark element, it is placed from the very beginning to the end by using the index. Scan all the items from the past to the next, and put all the items smaller than value in the front. Naturally, all those greater than value are in the back ):

1 public static int partition (int A [], int left, int right) 2 {3 int Index = left; 4 int value = A [left]; 5 // always detect 6 for (INT I = left + 1; I <= right; I ++) 7 {8 if (a [I] <value) 9 {10 index ++; 11 if (index! = I) 12 {13 swap (A [I], a [Index]); 14} 15} 16} 17 A [left] = A [Index]; 18 A [Index] = value; // The reference element is in position 19 20 Return Index; 21}

  

Conclusion: To quickly sort the position of the positioning axis, the key is how you do it. If the value is smaller than the value of the pivot element (baseline element), it is placed on the left side of the pivot, and if the value is greater than the value of the pivot element, it is placed on the right; the same is true if you want to leave a position empty or swap.

 

Extended thinking:

1. Consider using a two-way linked list to set the head pointer and tail pointer. After comparing with the base element value, (unplug the node) insert a small value before the head node, insert a large value after the end node.

Ii. Improved Sorting Algorithm: (the sequence length is 5 ~ Sort the sorted sub-sequences by direct insertion.

 

A small exploration of quick sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.