An example of the choice sorting algorithm for Java array implementation _java

Source: Internet
Author: User

I. Algorithm description
Select sort: For example, in a unordered array of length n, the first pass through n data, find the smallest of the number and the first element of the exchange, the second traversal of the remaining N-1 data, find the smallest of the number and the second element exchange ... The N-1 trip traverses the remaining 2 data and finds the smallest number in exchange for the first N-1 element, which is done by selecting the order.
Take the following 5 unordered data as an example:
56 12 80 91 20 (this article only refines the first selection process)
1th Trip: 12 56 80 91 20

2nd Trip: 12 20 80 91 56
3rd Trip: 12 20 56 91 80
4th Trip: 12 20 56 80 91
two. Algorithm analysis
Average Time complexity: O (n2)
Space complexity: O (1) (for Exchange and recording index)
Stability: Instability (such as sequence "5, 5, 3" The first trip will be the first [5] and [3] exchange, causing the first 5 to move to the second 5 back)
three. Algorithm implementation

 
public class Selectionsort {public static void main (string[] args) {int len = 15; 
    int[] ary = new Int[len]; 
    Random Random = new Random (); 
    for (int j = 0; J < Len; J +) {Ary[j] = random.nextint (1000); 
} System.out.println ("Before-------------sort"); Ary=new int[]{10,9,8,7,6,5,4,3,2,1}; Test Exchange times//Ary=new int[]{1,2,3,4,5,6,7,8,10,9}; 
    The number of test exchanges for (int j = 0; J < Ary.length J + +) {System.out.print (Ary[j] + ""); 
    } selectdesc (ary); 
  SELECTASC (ary); /* * Select Sort: Descending */static void Selectdesc (int[] ary) {int comparecount = 0;//compare times int Changecount 
    = 0;//exchange number int len = ary.length; int maxvalueindex =-1;  Record a comparison of the minimum index for (int i = 0; i < len-1 i++) {maxvalueindex = i;//starting from 0 for (int j = i + 1; j < Len; 
        J + +) {if (Ary[maxvalueindex] < ary[j]) {maxvalueindex = J;//record large index comparecount++; 
     } }//System.out.println ("minvalueindex==" + maxvalueindex); if (Maxvalueindex!= i) {//If the record index to the left is different, swap ary[i] = Ary[maxvalueindex] + (Ary[maxvalueindex] = ary[i]) * 0;//one step 
      Change changecount++; 
    } System.out.println ("\ n-------Descending sort------compare times:" + Comparecount + ", number of exchanges + Changecount); 
    for (int j = 0; J < Ary.length J + +) {System.out.print (Ary[j] + ""); 
    }/* * Select sort: ascending/static void Selectasc (int[] ary) {int comparecount = 0, changecount = 0; 
    int len = ary.length; 
    int minindex =-1; 
      for (int i = 0; i < len-1 i++) {minindex = i; 
          for (int j = i + 1; j < Len; J + +) {if (Ary[minindex] > Ary[j]) {minindex = J;//record smaller index 
        comparecount++; 
        } if (Minindex!= i) {//If the record index to the left is different, swap ary[i] = Ary[minindex] + (Ary[minindex] = ary[i]) * 0; 
      changecount++; }} SYSTEM.OUT.PRintln ("\ n-------Ascending sort------Compare times:" + Comparecount + ", exchange times" + Changecount); 
    for (int j = 0; J < Ary.length J + +) {System.out.print (Ary[j] + ""); 

 } 
  } 
}

Print

-------Sort before------ 
648 789 319 699 855 755 552 489 187 916 596 731 852  
-------Descending sort------compare times: 26, number 
of exchanges 916 855 852 789 755 731 699 648 596 552 489 319 187  
-------Ascending sort------compared number of times: 56, Exchange 7 
125 187 319 350 489 2 596 648 699 731 755 789 852 855 916  

Related Article

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.