Select Sorting Algorithm and Sorting Algorithm

Source: Internet
Author: User

Select Sorting Algorithm and Sorting Algorithm

Select sorting: for example, in an unordered array with a length of N, traverse N data in the first row to find the minimum value and switch with the first element, the second trip traverses the remaining N-1 data and finds out the smallest value to exchange with the second element ...... the N-1 traverses the remaining 2 data, find out the smallest value exchange with the N-1 element, so now the selection is complete.

Java Implementation for sorting:

1 package com. mianshi. easy; 2 public class Selection {3 4 public static void main (String [] args) {5 int [] a = {, 7 }; 6 7 selectionSort (a); 8 9 for (int I = 0; I <. length; I ++) {10 System. out. print (a [I] + ""); 11} 12} 13 14 public static void selectionSort (int [] a) 15 {16 for (int I = 0; I <. length; I ++) 17 {18 int k = I; 19 for (int j = I + 1; j <. length; j ++) 20 {21 if (a [k]> a [j]) 22 {23 k = J; 24} 25} 26 // if k is not equal to I, it indicates that at least one element is smaller than the custom minimum value and the subscript of this element is obtained, used to exchange 27 if (k! = I) 28 {29 int temp = a [I]; 30 a [I] = a [k]; 31 a [k] = temp; 32} 33} 34} 35}

The function of k in the Code is similar to a pointer pointing to the smallest number.

 

Algorithm stability:An unstable sorting algorithm. The selection of sorting is unstable (for example, the first [5] and [3] of the sequence [5, 5, 3] are exchanged for the first time, cause the first 5 to move to the second 5 ).

Time Complexity:Average time complexity is

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.