Sorting algorithm - direct selection sort

What is direct selection sort?

Direct selection sort (Selection Sort) is a simple and intuitive sorting algorithm. Its basic idea is to select the smallest (or largest) element among the unsorted elements each time and put it at the end (or beginning) of the sorted element until all elements are sorted. Direct selection sort is an unstable sorting algorithm. Its time complexity is O(N^2) .

Insert image description here

Algorithm steps:

1. From the sequence to be sorted, find the element with the smallest key;
2. If the smallest element is not the first element of the sequence to be sorted, exchange it with the first element;
3. From the remaining N-1 Among the elements, find the element with the smallest keyword;
4. Repeat steps 2 and 3 until the sorting is completed.

For example, perform direct selection sorting on [5, 3, 8, 6, 4]. The sorting process is as follows:

The first sorting: find the minimum element 3, exchange positions with the first element 5, and the sequence becomes [3, 5, 8, 6, 4]; the second sorting: find the
minimum element 4, and exchange it with the second element 5 Exchange positions, and the sequence becomes [3, 4, 8, 6, 5];
third sorting: find the smallest element 5, exchange positions with the third element 8, and the sequence becomes [3, 4, 5, 6, 8 ];
The fourth sorting: Find the minimum element 6, exchange positions with the fourth element 8, and the sequence becomes [3, 4, 5, 6, 8]; The fifth sorting: The sequence is in order and the sorting ends
.

The final ordered sequence is [3, 4, 5, 6, 8].

What is the use of direct selection sorting?

Although direct selection sort is not the fastest sorting algorithm, it has the following advantages:

  1. Simple and intuitive : The implementation of direct selection sorting is very simple, easy to understand and implement.

  2. Small memory consumption : The memory consumption of the direct selection sorting algorithm is small and does not require additional space.

  3. High efficiency for sorting small-scale data : When the amount of data is small, direct selection sorting is more efficient because it only requires n-1 comparisons (n ​​is the number of data).

  4. Will not damage the stability of the original sequence : Direct selection sort will not change the relative position of two equal elements, so it is a stable sorting algorithm.

Implementation of direct selection sorting algorithm

void Swap(int* a, int* b)
{
    
    
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
//直接选择排序
void SelectSort(int* arr, int n)
{
    
    
	int i=0, j=0,mini=0;
	for (i = 0; i < n - 1; i++)//对n个元素排序,则需要进行n-1次排序
	{
    
    
		mini = i;//排完一次后最小值在最前面,最小值的位置随着i的变化而变化
		for (j = i+1; j < n; j++)//开始进行选择排序,因为[0 - i]已经有序了,所以从下标为i+1开始
		{
    
    
			if (arr[j] < arr[mini])
			{
    
    
				mini = j;//将较小值的下标赋给mini
			}
		}
		Swap(&arr[mini], &arr[i]);//将每次循环的较小值移到i位置,i从0开始,
		//当i=n-1时就结束循环,此时就已经排好序了
	}
}

Summary of direct selection sorting

To sum up, although direct selection sort is not the best sorting algorithm, it has the advantages of simplicity and intuitiveness, low memory consumption, high efficiency in sorting small-scale data, and does not damage the stability of the original sequence , so it is still a comparison Practical sorting algorithm.

Guess you like

Origin blog.csdn.net/originalHSL/article/details/131055900