Article directory
foreword
There are ten commonly used sorting methods, namely:
- Bubble Sort;
- selection sort ;
- insertion sort;
- Hill sort;
- merge sort;
- quick sort;
- heap sort;
- count sort;
- bucket sort;
- Radix sort.
Let's talk about selection sorting today
1. The principle of selection sorting
First, find the smallest element in the array, and second, swap it with the first element of the array (if the first element is the smallest element then it swaps itself). Second, find the smallest element among the remaining elements and swap it with the second element of the array. And so on, until the entire array is sorted. We call this method selection sort.
Selection sort is actually an optimization of bubble sort.
2. Use steps
int[] arr = {
1, 2, 4, 3, 28, 23, 12, 14, 99, 34, 55 };
arr.ToList().ForEach(x => Console.Write(x + " "));
for (int i =0;i<arr.Length-1;i++)
{
Console.WriteLine();
Console.WriteLine("===========================================");
int index = i;
for(int j = i + 1; j < arr.Length; j++)
{
Console.WriteLine($"第{
j-i}次比较");
if (arr[index] > arr[j])
{
index = j;
}
}
if(index!=i)
{
var temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
Console.WriteLine("===========================================");
Console.Write($"第{
i + 1}轮:");
arr.ToList().ForEach(x => Console.Write(x + " "));
}
Console.WriteLine();
Console.WriteLine("===========================================");
arr.ToList().ForEach(x => Console.Write(x + " "));
3. Print the result
1 2 4 3 28 23 12 14 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
第5次比较
第6次比较
第7次比较
第8次比较
第9次比较
第10次比较
===========================================
第1轮:1 2 4 3 28 23 12 14 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
第5次比较
第6次比较
第7次比较
第8次比较
第9次比较
===========================================
第2轮:1 2 4 3 28 23 12 14 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
第5次比较
第6次比较
第7次比较
第8次比较
===========================================
第3轮:1 2 3 4 28 23 12 14 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
第5次比较
第6次比较
第7次比较
===========================================
第4轮:1 2 3 4 28 23 12 14 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
第5次比较
第6次比较
===========================================
第5轮:1 2 3 4 12 23 28 14 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
第5次比较
===========================================
第6轮:1 2 3 4 12 14 28 23 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
第4次比较
===========================================
第7轮:1 2 3 4 12 14 23 28 99 34 55
===========================================
第1次比较
第2次比较
第3次比较
===========================================
第8轮:1 2 3 4 12 14 23 28 99 34 55
===========================================
第1次比较
第2次比较
===========================================
第9轮:1 2 3 4 12 14 23 28 34 99 55
===========================================
第1次比较
===========================================
第10轮:1 2 3 4 12 14 23 28 34 55 99
===========================================
1 2 3 4 12 14 23 28 34 55 99
Summarize
Properties: 1. Time complexity: O(n2) 2. Space complexity: O(1) 3. Unstable sorting 4. In-place sorting