Implementation of quick-row, recursive and non-recursive

Source: Internet
Author: User

The core idea of the fast-line is to determine the position of a number at a time and make the number on the left less than that number, and the number on the right is greater than the book. The same operation is further performed on the left and right sides until the sort is finished.

The program body is:

public static void QuickSort (int[] nums, int begin, int end) {if (Begin < end) {int MID = partition (nums, begin, end); q Uicksort (Nums, Begin, Mid-1) QuickSort (Nums, mid + 1, end);}}


After the partition execution is complete, the position of a number (which defaults to the first number of input arrays) is determined.

Take an array of nums={5,2,7,3,6} as an example:

I.quicksort (nums,0,4)

After calling Quicksort (nums,0,4), mid=partition (nums,0,4) = 2, the array distribution after the partition is executed as shown. Next executes QuickSort (nums,0,1), QuickSort (3,4).

II. QuickSort (nums,0,1) &quicksort (nums,3,4)

QuickSort (nums,0,1) process, mid=partition (nums,0,1) = 1, after the completion of the partition number as shown in the left of the third, the final result is obtained. This section goes on to execute quicksort (nums,0,0) and quicksort (nums,1,1), in Quicksort (nums,0,0) and quicksort (nums,1,1), (begin<end) = = False,quicksort stop execution.

QuickSort (nums,3,4) is similar to QuickSort (nums,0,1)

The process is as follows, where red indicates where the partition begins, and yellow indicates where the numbers are placed after partition completes:


The function of partition is to find the specified position for a number. The execution process for Partition (nums,0,4) is as follows:

1. Base point is 0,nums[0]=5. Int x = 5. Point two pointers to nums[1] and nums[4], medium process I

2. First look at the right side of the execution, 6>5, do not perform the action, right pointer left, middle Process II.

3. Look again at the right hand, 3<5, will 3 as nums[0],nums[0]=3, such as III.

4. Start looking at the left pointer, 2<5, do not perform the action, left pointer to right, middle process IV

5. The left pointer points to the NUMS[3]=7>X, swapping the values in the left and right hands, middle procedure V

6. The right pointer moves left, overlaps with the left hand, ends with the loop, and partition the x as it is done. As shown in VI


Partition function:

public static int partition (int[] nums, int begin, int end) {int i = begin, J = end;int x = Nums[i];while (i < j) {Whil E (i < J && x < nums[j])--j;if (i < j) nums[i++] = Nums[j];while (i < J && Nums[i] < x) ++i;i F (I < j) nums[j--] = nums[i];} Nums[i] = X;return i;}

According to the above, the process of the fast-line is actually "constantly call partition to cut the problem, and constantly reduce the size of the problem" process. The non-recursive notation of the fast line can be done by stack to hold the required begin and end in the partition.

Overall code:

Import Java.util.stack;public class Quick {public static int partition (int[] nums, int begin, int end) {int i = begin, J = End;int x = Nums[i];while (i < J) {while (I < J && x < nums[j])--j;if (i < j) nums[i++] = Nums[j];whil E (i < J && Nums[i] < x) ++i;if (i < j) nums[j--] = nums[i];} Nums[i] = X;return i;} Recursive public static void QuickSort (int[] nums, int begin, int end) {if (Begin < end) {int MID = partition (Nums, Begin, E nd); QuickSort (Nums, begin, Mid-1); QuickSort (Nums, mid + 1, end);}} Non-recursive public static void QuickSort2 (int[] nums, int begin, int end) {stack<integer> Stack = new stack<integer> (); int k = 0;if (Begin < End) {Stack.push (begin); Stack.push (end); while (!stack.empty ()) {int j = stack.pop (); int i = St Ack.pop (); k = partition (Nums, I, j); if (I < k-1) {Stack.push (i); Stack.push (k-1);} if (k + 1 < J) {Stack.push (k + 1); Stack.push (j);}}} public static void Main (string[] args) {//TODO auto-generated method stubint[] Arr = {3, 5, 9, 7, 1, 6,, 2};quicksort2 (arr, 0, Arr.length-1), for (int i:arr) System.out.print (i + "");}} 



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of quick-row, recursive and non-recursive

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.