Select sort--Sort algorithm Series

Source: Internet
Author: User

Let's say we have an array like this:

      

To sort the array using the Select Sort algorithm, proceed as follows:

1th time

    • To find the smallest number between subscript 0 and 6, we can find that the smallest number is 15, which is positioned at the subscript 4;
    • The number above the subscript 4 is swapped with the number above the subscript 0 to get the sorted array:

      

2nd time

    • To find the smallest number between subscript 1 and 6, we can find that the smallest number is 33, which is positioned at the subscript 5;
    • The number above the subscript 5 is swapped with the number above the subscript 1 to get the sorted array:

      

3rd time

    • To find the smallest number between subscript 2 and 6, we can find that the smallest number is 48, which is positioned at the subscript 5;
    • The number above the subscript 5 is swapped with the number above the subscript 2 to get the sorted array:

      

According to the above way to the 6th time, completed the sorting, the Java implementation of the choice of sorting algorithm code is as follows:

 Public classSelectionsort { Public Static voidMain (string[] args) {int[] arr =New int[]{3,4,1,5,7,6,0,2};        Sort (arr);    Printarr (arr); }     Public Static voidSortint[] arr) {        intLength =arr.length;  for(inti = 0; I < length;i++){            intIndexoflastsmall =Getindexoflastsmall (arr,i); if(Arr[i] >Arr[indexoflastsmall])                {swap (Arr,i,indexoflastsmall);            Printarr (arr); }        }    }     Public Static intGetindexoflastsmall (int[] arr,intstart) {        intLength =arr.length; intsmallest =Arr[start]; intIndexoflastsmall =start;  for(inti = start + 1; i<length;i++){            if(Arr[i] <smallest) {Smallest=Arr[i]; Indexoflastsmall=i; }        }        returnIndexoflastsmall; }     Public Static voidSwapint[] arr,intLeftintRight ) {        inttemp =Arr[left]; Arr[left]=Arr[right]; Arr[right]=temp; }     Public Static voidPrintarr (int[] arr) {         for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ " ");    } System.out.println (); }}
Analysis:

In the sorting process above, we can find a rule: In order to find the smallest number in the K-options, we need to make a k-1 comparison.

Suppose there are n numbers in the array:

      1. The 1th step, find the smallest of the n numbers, we carried out the n-1 times comparison;
      2. 2nd, we find the smallest of the remaining n-1 numbers, and we perform a n-2 comparison;

... And so on ... Until the last step, find the smallest one in two numbers, and we have compared it 1 times.

We can set the number of steps to J, set the length of the array is n, in each step of the comparison, we will be in N-j + 1 numbers inside the n-j comparison, find the smallest number inside these numbers.

In summary, we can get the following formula:

Sort Process Total Comparison = 1 + 2 + 3 + ... + (n-1) = ($) * n * (n-1) ≈ () * N2

From the above formula, it is known that the time complexity of selecting the sorting algorithm is O (n2), n2 the front coefficient is not important, because N2 is the number of times the increase plays a decisive role.

So, if the array before sorting is already in order, will it increase the efficiency of the sorting algorithm?

The answer is in the negative. Interested can try it out, you will see that regardless of the given array is ordered, the choice of sorting algorithm requires the same number of comparisons.

The selection sort belongs to the "in place" sort, which is the in situ sort because it does not require additional memory space.

  

Select sort--Sort algorithm Series

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.