Simple selection sorting of sequencing series and its improved algorithm--java implementation

Source: Internet
Author: User

 Simple Selection sorting algorithm:

Basic idea:

In the data to be sorted, the smallest number is exchanged with the number of the first position, then the smallest number in the remaining number is exchanged with the second number, and so on, until the loop is only two numbers left to compare.

Instance:

0. Initial state 3,1,5,7,2,4,9,6 (a total of 8)

1.n=8 number, the minimum value is 1, and the first number of exchanges:1, 3,5,7,2,4,9,6

2. In the remaining n-1=7 number, the minimum value is 2, and the second numberis exchanged: 5,7,3,4,9,6

3. In the remaining n-2=6 number, the minimum value is 3, and the third numberis exchanged: three-in-one, 7,5,4,9,6

4. In the remaining n-3=5 number, the minimum value is 4, and the fourth number is exchanged:1,2,3,4, 5,7,9,6

5. In the remaining n-4=4 number, the minimum value is 5, and the fifth number is exchanged:1,2,3,4,5, 7,9,6

6. In the remaining n-5=3 number, the minimum value is 6, and the fifth number is exchanged:1,2,3,4,5,6, 9,7

7. In the remaining n-6=2 number, the minimum value is 7, and the fifth number is exchanged:1,2,3,4,5,6,7,9

Simple selection of improvements to the sorting algorithm:

 Basic idea:

Simple selection sorting algorithm, each time only select one data in the correct location, in order to improve efficiency, you can select two data at a time, put in the correct position, and select the maximum and minimum value each time.

Instance:

0. Initial state 3,1,5,7,2,4,9,6 (a total of 8)

1.n=8 number, the minimum value is 1, and the first number is exchanged ; The maximum value is 9, and the last number is exchanged:1,3,5,7,2,4,6,9

2. In the remaining n-2=6 number, the minimum value is 2, and the second number is exchanged ; The maximum value is 7, and the second-lowest number is exchanged :5,6,3,4, 7,9

3. In the remaining n-4=4 number, the minimum value is 3, and the third number is exchanged; The maximum value is 6, which is exchanged with the third-lowest number: Three -in-one,4,5,6,7,9

4. In the remaining n-6=2 number, the minimum value is 4, and the fourth number is exchanged ; The maximum value is 5, and the reciprocal fourth number is exchanged :1,2,3,4,5,6,7,9

Java implementations:

/*** Selection sequencing and implementation of improved algorithms * Stabilization algorithm *@authorThe ginkgo leaves of the season*/ Public classSelectsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        NewSelectsort (). run (); }     Public voidrun () {intA[] = {3,1,5,7,2,4,9,6}; /*** Select Sort*/Selectsort (a); /*** Selection of sorting improvements--Two yuan Select sort*/Selecttwosort (a); }    /*** Select the smallest element of the order each time *@parama*/    Private voidSelectsort (int[] a) {//TODO auto-generated Method StubSystem.out.println ("——————————————————— Select sorting algorithm ———————————————————"); intn=a.length;  for(inti=0;i<n;i++){            intk=i;  for(intj=i;j<n;j++){                if(A[j] <A[k]) {k=K; }            }            if(k! =i) {                inttemp =A[i]; A[i]=A[k]; A[K]=temp;        } print (A,n,i);    } printresult (A,n); }    /*** Select the improved algorithm for sorting * Maximum number and minimum number per tick *@parama*/    Private voidSelecttwosort (int[] a) {System.out.println ("——————————————— the choice of sorting algorithm--Improvement —————————————————"); intn=a.length; inti,j,max,min,temp;  for(i=0;i<n/2;i++) {Max=i; Min=i;  for(j = i+1;j<n-i;j++){                if(a[j]<A[min]) {min=J; }                if(a[j]>A[max]) {Max=J; }} Temp=A[i]; A[i]=A[min]; A[min]=temp; Temp= A[n-i-1]; A[n-I-1] =A[max]; A[max]=temp;        Print (a,n,i);    } printresult (A,n); }    /*** Print the final result of sorting *@paramA *@paramN*/    Private voidPrintresult (int[] A,intN) {System.out.print ("Final sort Result:");  for(intj=0;j<n;j++) {System.out.print (" "+A[j]);    } System.out.println (); }    /*** Print the result of each cycle of sorting *@paramA *@paramN *@paramI*/    Private voidPrintint[] A,intNinti) {//TODO auto-generated Method StubSystem.out.print ("+i+" Times: ");  for(intj=0;j<n;j++) {System.out.print (" "+A[j]);    } System.out.println (); }}

running results show:

(This article is only for learning exchange, if there are better ideas, welcome to leave comments for everyone to explore learning ~)

Simple selection sorting of sequencing series and its improved algorithm--java implementation

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.