table of Contents
Points
Select sort is a simple select sort .
Select Sort : Every trip from selected records to be sorted in the smallest key records, order at the end of a sequence of records sorted until the end of all sort so far.
Simple ordering process flow
(1) from the sequence to be sorted, the key to find the smallest element;
(2) If the first element is not the smallest element to be sorted sequence, and the first element to be interchanged;
(3) from the remaining N - 1 system elements, find the smallest element keywords, repeating (1), (2) step, until the end of sorting.
As shown, sorting each trip, the current of the i smaller elements placed in the position i on.
Core code
// iterate required minimum number of times to obtain
// is to be noted that, when the number to be sorted N, N-1 has elapsed after traversal already ordered sequence
for (int i 0 =; i <List.length -. 1; i ++) {
int TEMP = 0;
int index = i; // save for the minimum index worth
// find the i-th small value
for (int j = i + 1 ; J <List.length; J ++) {
IF (List [index]> List [J]) {
index = J;
}
}
// the value of i is found in small i-th position
temp = list [ index];
List [index] = List [I];
List [I] = TEMP;
System.out.format ( "% d of Run: \ T", I +. 1);
printAll (List);
}
}
Analysis of Algorithms
Simply sorting algorithm performance
Sort by category |
Sort method |
time complexity |
Space complexity |
stability |
Complexity |
||
Average case |
Worst case |
Best case |
|||||
Selection Sort |
Simple selection sort |
O (N2) |
O (N2) |
O (N2) |
O (1) |
Unstable |
simple |
time complexity
Independent simply sorted initial ordering of the sequence number of comparisons. Is assumed to be sorted sequence of N elements, the number of comparisons is always N (N -. 1) / 2 .
The initial ordering sequence number related to the mobile. When the sequence is positive, it moves the least number is 0.
When the sequence in reverse order, moving most often, is 3N (N - 1) / 2.
Therefore, the above, the time complexity is simple sorting O (N2 of) .
Space complexity
Simple selection sort needs to occupy 1 temporary space used during the exchange value.
Complete Reference Code
JAVA version
Code
2
. 3 Import java.util.Random;
. 4
. 5 SelectionSort {public class
. 6
. 7 SelectionSort public void (int [] List) {
. 8 // need to be traversed to get a minimum number of
9 to // Note that when the number to be sorted N, N-1 has elapsed after traversal already ordered sequence
10 for (int I = 0; I <List.length -. 1; I ++) {
. 11 TEMP int = 0;
12 is int index = i; // save for the minimum index worth
13 is
14 // find the i-th small value
15 for (int i = J +. 1; J <List.length; J ++) {
16 IF (List [index] > List [J]) {
. 17 J = index;
18 is}
. 19}
20 is
21 is the value of i // find small placed on the i-th position
22 is List TEMP = [index];
23 is List [index] = List [i];
24 List [i] = TEMP;
25
26 is the System .out.format ( "% d of Run: \ T", I +. 1);
27 printAll (List);
28}
29}
30
31 is the complete sequence of the print //
32 printAll public void (int [] List) {
33 is for (int value: List) {
34 is of System.out.print (value + "\ T");
35}
36 System.out.println ();
37 [}
38 is
39 public static void main (String [] args) {
40 / / initialize a random sequence
41 final int MAX_SIZE = 10;
42 int[] array = new int[MAX_SIZE];
43 Random random = new Random();
44 for (int i = 0; i < MAX_SIZE; i++) {
45 array[i] = random.nextInt(MAX_SIZE);
46 }
47
48 // 调用冒泡排序方法
49 SelectionSort selection = new SelectionSort();
50 System.out.print("排序前:\t");
51 selection.printAll(array);
52 selection.selectionSort(array);
53 System.out.print("排序后:\t");
54 selection.printAll(array);
55 }
56
57 }
operation result
Run 1: 0528123841
Run 2: 0128523841
Run 3: 01185 23842
Run 4: 0112583842
Run 5: 0112283845
Run 6: 0112238845
Run 7 : 0112234885
Run 8: 01122345 8 8
01122345 8 8: 9 Run
sorted: 01122345 88
Reference material
"Data structure and analysis Exercises" (3rd Edition Class B)
Related Reading
Welcome to the programmers of internal strength - algorithm Series
Example Source: https: //github.com/dunwu/algorithm-notes