Sort--Direct selection sort (simple selection sort)

Source: Internet
Author: User
Tags sin

Direct selection sorting, also known as simple selection sorting, is a relatively simple sorting algorithm, its basic idea is: to find the smallest, and the first exchange from a number of columns, the rest of the re-find the smallest, and the number of the second exchange, ... After a n-1 comparison, the sequence is already an orderly sequence.

Example: A set of unordered numbers is known: 6 3 5 1 4 2 9

First time: [6 3 5 1 4 2 9] The minimum number is: 1

Second time: 1 [3 5 6 4 2 9] The minimum number is: 2

Third: 1 2 [5 6 4 3 9] The minimum number is: 3

Fourth time: 1 2 3 [6 4 5 9] The minimum number is: 4

Fifth time: 1 2 3 4 [6 5 9] The minimum number is: 5

Sixth time: 1 2 3 4 5 [6 9] The minimum number is: 6

Seventh time: 1 2 3 4 5 6 [9] It's already an orderly sequence.

Code implementation (Java language):

ImportJava.math.BigDecimal;ImportJava.math.RoundingMode;ImportJava.util.Scanner; Public classmain{//Public final static double pi = 3.1415927;     Public Static voidMain (string[] args) {Scanner sin=NewScanner (system.in);  while(Sin.hasnextint ()) {intLen = Sin.nextint ();//the length of the input array            intArray[] =New int[100];  for(inti=0; i<len; i++) {Array[i]= Sin.nextint ();//to enter the unordered array} s_sort (array, len);//Direct Insert SortDisplay (array, len);//Show ordered series after sorting        }    }     Public Static voidDisplayintArray[],intLen) {                 for(inti=0; i<len; i++) {System.out.print (Array[i]+" "); }    }     Public Static voidS_sort (intArray[],intLen) {         for(inti=0; i<len; i++){            intMin =i;  for(intj=i+1; j<len; J + +){                if(array[j]<Array[min]) {min=J; }            }            inttemp =Array[min]; Array[min]=Array[i]; Array[i]=temp; }    }    }

Efficiency analysis:

In the direct selection of the sorting, there is a total need for n-1 selection and exchange, each choice requires a n-i comparison (1<=i<=n-1), and each exchange requires up to 3 moves, so the total number of comparisons c= (n*n-n)/2, the total number of moves 3 (n-1). The time complexity of direct selection sorting is O (n^2), this means that the value in the case of N is relatively small, the choice of sorting algorithm can guarantee a certain speed, but when n is large enough, the efficiency of the algorithm will be reduced. So be careful when using it so when the record takes up a lot of bytes, it's usually faster to execute than the direct insert sort. Since there is an interchange between nonadjacent elements in the direct selection sort, direct selection of the sort is an unstable sort method.

Sort--Direct selection sort (simple selection sort)

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.