Fast sorting algorithm-java

Source: Internet
Author: User

The idea of a fast algorithm is to sort the array partitions

The general first takes the first number as the base number, then divides the whole series by the size into two zones, which is greater than the base number on the right, less than the left. The partitions are then sorted according to the method above, until the entire array is sorted.

The following is an example of an array:

6,7,1,8,3,9,2,5,10

In general, the first number as the base number, then how to move more than 6 to the right, less than 6 to move to the left?

If you use 6, 6:7 small, 6:1 large transposition, so that can not be sorted (bubble sort is adjacent to the two-digit comparison transposition).

If you create an empty array of the same size, it will be smaller than 6 on the left, large on the right, the remaining seat is the base number, and record its seat, this is also very troublesome, recursive need to go to the middle number will be more and more, and no head and tail. It is also very memory-intensive to create so many groups, preferably to be able to modify the original array.

Since the partition, there must be a middle position, assuming that the middle of the pointer is called middle, it is obvious that this middle can be compared, then must be two pointers to indicate the location of the area, assuming that the beginning of the area is left, and the end is right. To recursively partition down, you need such three parameters, int[] array, int left,int right. So when does recursion end? When it is no longer possible to partition the end of the time, you can not partition it? Left=right of the time.

The most important thing here is a function that asks for the middle value of the partition, which we assume as getmiddle (int array, int left, int. right)

Sorted function sort (int [] Array,int left, int. right)

So the function body of this sort function is as follows

     Public void sort (int [] array,int left,int. right )    {           if( left< right)           {               int middle=getmiddle (array,left,right);               Sort (array, left, middle-1); // Partitioning to the left               Sort (array, middle+1, right); // partitioning to the right            }    }

Now to implement the Getmiddle function, this function needs to find the middle position in the partition and operate on the original array.

First, the first left and the last of the area are compared

The goal is to find a number smaller than the left point on the right, and replace it with the position pointing to it, so will this substitution cause the number left pointing to disappear? Of course, continue to the right to find the words will certainly be so, how to avoid such a problem? That is the right to find out, replace the left pointing to the number, that right point to the number now has a repetition, now left and right point to the number is the same, you need to find a number in right to be able to replace the number of right, so you start to compare, To find a larger than the base number to replace the right, as long as the left<right will not stop. When left and right are equal, this position is smaller than the base number, and has a greater number on the far side than the base. This position is middle.

     Public intGetmiddle (int[] list,intLeftintRight ) {        intpvoit=List[left];  while(left<Right ) {             while(Left<right && list[right]>Pvoit) { Right--; } List[left]=List[right];  while(Left<right && list[left]<Pvoit) { Left++; } List[right]=List[left]; } List[left]=Pvoit; returnLeft//returns the position of the middle axis}

Final Test

     Public void Quick (int[] str) {        if (Str.length > 0) {    //  See if the array is empty            sort (str, 0, str.length-1);}    }

 

Fast sorting algorithm-java

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.