Java Quick Sort (dig pit + divide)

Source: Internet
Author: User

The quick sort is a sort of division exchange proposed by C.r.a.hoare in 1962. It adopts a strategy of division, which is usually referred to as the Division method (Divide-and-conquermethod). The basic idea of this method is:

1. First, a number is taken from the series as the base number.

2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.

3. Repeat the second step for the left and right intervals until there is only one number for each interval.

Although the quick sort is called the divide-and-conquer method, the three words of the divide-and-conquer method clearly fail to generalize all the steps of the quick sort.

So my quick sort is further explained: Pit filling + divide-and-conquer method:

Let's take a look at the example, and give it the definition below (it would be better to summarize the definition in your own words, which would be helpful for implementing the code).

Taking an array as an example, the first number of intervals is the base count.

0

1

2

3

4

5

6

7

8

9

72

6

57

88

60

42

83

73

48

85

Initially, i = 0;   j = 9; X = A[i] = 72

Since the number in a[0] has been saved to X, it can be understood that a hole has been dug in the array a[0], and other data can be populated here.

Looking forward from J to a number smaller than x or equal to X. When j=8, meet the conditions, will a[8] dug up and then filled in the last pit A[0]: a[0]=a[8];  i++; Such a pit a[0] was taken care of.

But a new pit was formed A[8], what about this? Simple, and then find the number to fill a[8] this pit. This time from I began to find a number greater than X, when i=3, meet the conditions, will a[3] dug up and then filled in the previous pit: a[8]=a[3]; j--;

The array becomes:

0    

1   

2     

3     

4  

5    

6   

7   

8  

48

6

57

88

60

42

83

73

88

85

i = 3;   j = 7; x=72

Repeat the above steps, looking forward from behind, then looking backwards .

Start looking forward from J, when j=5, meet the conditions, will a[5] dug into the last pit, a[3] = a[5]; i++;

From I start looking backwards, when i=5, due to i==j exit.

At this point, I = j = 5, and a[5] is just the last hole dug, so X is filled into a[5].

The array becomes:

0

1

2

3

4

5

6

7

8

9

48

6

57

42

60

72

83

73

88

85

You can see that the number before a[5] is less than it, and the number behind a[5] is greater than it . So again to a[0 ... 4] and a[6 ... 9] These two sub-zones repeat the above steps.

Summarize the number of digging pits

1. I =l; j = R; Gouge out the base number to form the first pit a[i].

2. j--from the back forward to find a number smaller than it, found after digging out of this number before filling a pit a[i].

3. i++ from the front to find a larger number than it, find and dig out this number to fill the previous pit a[j].

4. Repeat 2, 32 steps until I==j, and fill in the base number into a[i].

According to this summary it is easy to achieve the number of pit fill code:

1  Packagesorting;2 3  Public classquicksorting {4 5      Public Static voidMain (string[] args) {6         //TODO auto-generated Method Stub7          intarr[]={1,5,6,84,15,26,355,26,35,36,45,-5,36,45,18,4,44,58};8          intBrr[]={1,5,6,84,15,26,35,36,45,18,4,44,58,555,63,78,98,15,0,-5,-8};9QuickSort qs=NewQuickSort ();TenQs.sort1 (0, Arr.length-1, arr); One            AQs.sort2 (0, Brr.length-1, BRR); -        -        for(intE:arr) { theSystem.out.print (e+ ""); -         } - System.out.println (); -System.out.println ("**********************************"); +        for(intE:BRR) { -System.out.print (e+ ""); +           } A     } at } -  -    classQuickSort { -         -        //1. Pit filling + divide-and-conquer method ****************** -         in             Public voidSort1 (intLintRints[]) { -              if(l<s) { to                  inti=l,j=r,x=S[l]; +              while(i<j) { -                      //find the first number smaller than x from right to left pits the                       while(I<j && s[j]>=x) { *j--; } $                      Panax Notoginseng                          if(i<j) { -S[I]=S[J];//fill s[j] into s[i], s[j] form a new pit. thei++; +                          } A                       the                       +                     //from left to right, find the first number larger than X to fill in the last occurrence of a pit. -                       while(I<j && s[i]<x) { $i++; } $                       -                          if(i<j) { -S[j]=s[i];//fill s[i] into s[j], s[i] form a new pit. thej--; -                          }Wuyi                    }    thes[i]=x; -Sort1 (l,i-1,s);//Recursive invocation WuSort1 (i+1, r,s); -              } About         } $           -             -          //2. Two-way quick sort -             Public voidSort2 (intLeftintRightintarr[]) { A                intL=left,r=Right ; +                intpivot=arr[(Left+right)/2];//find the middle value the                intTemp=0; -                 while(l<R) { $                     while(Arr[l]<pivot) l++; the                     while(Arr[r]>pivot) r--; the                    if(L&GT;=R) Break; thetemp=Arr[l]; thearr[l]=Arr[r]; -arr[r]=temp; in                    if(Arr[l]==pivot)--R; the                    if(Arr[r]==pivot) + +l; the                } About                if(l==R) { thel++; ther--; the                } +                 -                if(left<r) sort2 (Left,r,arr); the                if(right>l) sort2 (L,right,arr);Bayi            } the             the             -    } -        

Java Quick Sort (dig pit + divide)

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.