Java data structure and algorithm example: select Sort Selection Sort_java

Source: Internet
Author: User
/** * The idea of choosing a sort: * to find the smallest element each time from the list to be sorted, * then place it on the leftmost of the sequence to be sorted until all elements are ordered * * Select sort to improve the bubble sort, reduce the number of exchanges from O (n^2) to O (N) * But compare the Times or O 
(N) */package Al; public class Selectsort {public static void main (string[] args) {Selectsort selectsort = new Selectsor 
    T (); 
    Int[] elements = {14, 77, 21, 9, 10, 50, 43, 14}; 
    Sort the array selectsort.sort (elements); 
      Print the sorted array for (int i = 0; i < elements.length i++) {System.out.print (elements[i)); 
    System.out.print (""); }/** * @author * @param array to row arrays */public void sort (int[] array) {//min to save 
     
    Minimum element for each round int min, tmp; 
      for (int i=0; i<array.length; i++) {min = i; 
          Search for the minimum element for (int j=i; j<array.length; J + +) {if (Array[j] < array[min]) { 
        min = j; }//swap minimum element TMP = arrAy[i]; 
      Array[i] = Array[min];       
    Array[min] = tmp; 
 } 
  } 
}

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.