Algorithm Note: Select sorting

Source: Internet
Author: User

I. Concepts

  • Select sorting (it is constantly selecting the smallest of the remaining elements): first, find the smallest element in the array, and then, swap it with the first element of the array (if the first element is the smallest element, it will swap it with itself ). Again, find the smallest element in the remaining element and exchange it with the second element of the array. In this way, the histogram sorts the entire array;

    Ii. Features

    • The running time has nothing to do with the input: finding the smallest element and scanning the array once does not provide any information for the next scan;

    • Data movement is minimal: the number of exchanges is linearly related to the size of the array;

      Iv. Code

      /*** Select sort ** @ author pengcx **/public class Selection {public static void main (String [] args) {String [] a = {"d ", "a", "w", "B", "q"}; Selection. sort (a); show (a);}/*** sort array a ** @ param a * sort array a */public static void sort (Comparable []) {int N =. length; for (int I = 0; I <N; I ++) {// The default minimum element is a [I] int min = I; // find the minimum element for (int j = I + 1; j <N; j ++) from a [I + 1] to a [N) {// if a [j] is smaller than a [min], the current minimum element is a [j] if (less (a [j], a [min]). {min = j ;}/// replace a [min] in a [I] With exch (a, I, min );}} /*** swap the I and j elements in array a ** @ param a * swap element array a * @ param I * swap element index I *@ param j * index j */private static void exch (Comparable [], int I, int j) {Comparable swap = a [I]; a [I] = a [j]; a [j] = swap ;} /*** compare the sizes of v and w ** @ param v * compare the element v * @ param w * compare the elements w * @ return size ID */private static boolean less (Comparable v, comparable w) {return (v. compareTo (w) <0);} private static void show (String [] a) {for (int I = 0; I <. length; I ++) {System. out. print (a [I]) ;}}

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.