Choosing a sort is as simple as this.

Source: Internet
Author: User

Choosing a sort is as simple as this.

From the previous article has already explained the bubble sort, this chapter mainly explains is chooses the sorting, hoped that everybody can understand and the handwriting chooses the sorting code, then passed the interview! If I write the wrong place also ask you to point out in the comments.

Select sort description and stability instructions

SOURCE Baidu Encyclopedia:

Select sort (Selection sort) is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the start (end) position of the sequence until all the data elements to be sorted are finished. Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move back to the second 5).

The above mentioned that the choice of sorting is not a stable sorting method , then our bubble sort is not a stable sorting method? What does the meaning of stability mean?

To determine if a sorting algorithm is stable, we can simply understand that the first 2 equal number of the order is the same as before and after the sequence and after the ordering of their two positions.

    • If the same, it is a stable sorting method.
    • If it is not the same, it is an unstable sort method

If the array before the sort is [3,3,1] , assuming that we use the selection sort, then the result is the first order [1,3,3] . This array has two identical values, both in array[0] and array[1] , the results are sorted, and array[0] the run is up array[2] .

So this leads to the following:2 equal numbers whose order of position before and after the sequence and the order of their two are not the same, so we say it is unstable

Back to the question above, the last one said the bubble sort is stable, the main reason is: when the two comparisons, there is no exchange of equal data (because there is no need). So it does not exist in 2 equal numbers, and the order of their positions before and after the sequence and their two positions are not the same.

So what are the benefits of stable sequencing?

    • Refer to answer @ Lone Ranger's Answer:

If we sort only on a string of numbers, then stability is really not important, because the properties of a string of numbers are single, that is, the size of the numeric value. But the ordering element often has not only one attribute, for example we to the group of people by age sorts, but in addition to the age attribute also has the height weight attribute, in the age same time if does not want to destroy the original height weight the order, must use the stable sorting algorithm.

It is clear that stability is only meaningful if you do not want to break the original order when you sort "two times" .

Resources:

    • https://www.zhihu.com/question/46809714/answer/281361290
    • http://tieba.baidu.com/p/872860935
    • Http://www.cnblogs.com/codingmylife/archive/2012/10/21/2732980.html
First Order of the tour

It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning (end) of the sequence until all the data elements to be sorted are finished

First, we create the array and find its maximum value (which is very simple):

    int[] arrays = {23143516123723};    //假定max是最大的    int0;    for (int0; i < arrays.length; i++) {        if (arrays[i] > max) {            max = arrays[i];        }    }        

The largest number and the number of the end of the array are then exchanged:

    //使用临时变量,让两个数互换    int temp;    temp = arrays[11];    arrays[11] = arrays[13];    arrays[13] = temp;

So after the first order, our maximum value has reached the end of the array.

Second order of the two trips

Get the maximum number again from the array (except for the one that is already lined up):

    int0;    for (int0; i < (arrays.length1); i++) {        if (arrays[i] > max2) {            max2 = arrays[i];        }    }    System.out.println(max2);

Then the maximum value to be obtained is exchanged with the second reciprocal of the array:

    temp = arrays[7];    arrays[7] = arrays[12];    arrays[12] = temp;

After the second sort, the array can be sorted by a maximum of two numbers.

Third, Code simplification

From the first two trips we can actually get a pattern:

    • An array is required to be n-1 sorted (because it is not necessary to find the maximum value until one element is left)
    • Reduce the range by 1 when you find the maximum value 1 times per swap
    • Query the maximum value of the current number of times do not really know what the maximum value (I found the maximum value, but also I have to manually count its corner mark), know its array angle , Exchange is also based on the angle of the exchange

First trip: Iterate through the array 14 numbers, get the maximum value, place the maximum value at the end of the array[13]
Second pass: Iterate through the array 13 numbers, get the maximum value, place the maximum value in the second-to-last array[12]

....

The array has 14 numbers and needs to be sorted 13 times.

    //Record the maximum value of the current number of times the corner label    intPOS;//exchange of variables    intTemp//Outer loop control the number of trips that need to be sorted     for(inti =0; I < arrays.length-1; i++) {//new number of times, the value of the corner is re-assigned to 0pos =0;//Inner loop control iterates over the number of arrays and gets the maximum number of corner labels         for(intj =0; J < Arrays.lengthI J + +) {if(Arrays[j] > Arrays[pos])            {pos = j; }        }//Exchangetemp = Arrays[pos]; Arrays[pos] = arrays[arrays.length-1-I]; Arrays[arrays.length-1-I] = temp; } System. out.println("Public number java3y"+ arrays);

Iv. Selection of sorting optimization

Bo Master has not thought of a better optimization method, if you see this article students know there is better optimization method or code can write a better place, welcome to comment under the message Oh!

Find this selection of sorting optimization method, feel the choice of sorting changed a flavor , we can also go to see:

    • He gets both the maximum and the minimum values, and then inserts the header and the tail of the array (This is a bit worse than the sorting principle, I don't know if it's a good idea)
    • Http://www.cnblogs.com/TangMoon/p/7552921.html
V. Expansion of Reading

C Language Implementation

          intFindmaxpos (intArr[],intN) {intmax = arr[0];intpos =0; for(inti =1; I < n; i++) {if(Arr[i] > Max)                    {max = arr[i];                pos = i; }            }returnPos }voidSelectionsort (intArr[],intN) { while(N >1)             {intpos = Findmaxpos (arr, n);inttemp = Arr[pos]; Arr[pos] = arr[n-1]; Arr[n-1] = temp; n--;//}        }intMain () {intArr[] = {5, +,7, the,2,3,4,8,9}; Selectionsort (arr,9); for(inti =0; I <9;        i++) cout << arr[i] << Endl; }

If the article is wrong, welcome to correct, we communicate with each other. Accustomed to look at technical articles, want to get more Java resources of students, can pay attention to the public number: Java3y

Choosing a sort is as simple as this.

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.