The implementation method of selecting Sort in Java sort advanced _java

Source: Internet
Author: User

The example of this article describes the implementation method of the choice of Java sorting advanced. Share to everyone for your reference. Specifically as follows:

Select sort (Selection sort) is a simple and intuitive sort algorithm. Its working principle is as follows. The smallest (Large) element is first found in the unordered sequence, placed at the start of the sort sequence, and then continues to look for the smallest (large) element from the remaining unsorted elements and then to the end of the sorted sequence. And so on until all the elements are sorted.
The primary advantage of selecting a sort is related to data movement. If an element is in the correct final position, it is not moved. Select the sort to exchange a pair of elements each time, at least one of them will be moved to its final position, so the table of n elements is sorted in total for up to n-1 exchanges. In all sorts of sorting methods that rely entirely on swapping to move elements, choosing a sort is a very good one.

Worst time complexity о (n²)
О of optimal Time complexity (n²)
О of average time complexity (n²)
Worst spatial complexity о (n) Total, O (1) Auxiliary

Code implementation:

Package com.baobaotao.test; /** * Sorting Research */public class Sort {/** * Select sort * @param array array/public static void Select 
    Sort (int[] array) {int length = Array.Length; 
    int index = 0; 
      for (int i=0;i<length-1;i++) {index = i; 
        for (int j=i+1;j<length;j++) {if (Array[j] < Array[index]) {index = j; 
      } swap (array, I, index); 
    Printarr (array); }/** * In the order of small to large Exchange array * @param a incoming array * @param b the number of incoming to exchange B * @param c the number of incoming to exchange c * * * * public s 
    tatic void Swap (int[] A, int b, int c) {if (b = = c) return; 
    int temp = A[b]; 
    A[B] = A[c];  
  A[C] = temp; 
      /** * Print Array * @param array/public static void Printarr (int[] array) {for (int c:array) { 
    System.out.print (c + ""); 
  } System.out.println (); public static void Main (string[] args) {int[] number={11,95,45,15,78,84,51,24,12}; 
  Selectsort (number); } 
}

Output:

95
   11 (
11)
12 15 24 45 51 78 84 95 (+)---------

I hope this article will help you with your Java programming.

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.