[Selection sort] Direct selection sort and heap sort

Table of contents

1. The concept of sorting:

2. Basic idea of ​​selection sorting

3. Directly select and sort

4. Heap sort


1. The concept of sorting:

Sorting: The so-called sorting is the operation of arranging a string of records in increasing or decreasing order according to the size of one or some keywords in it.

Stability: Assume that there are multiple records with the same keyword in the record sequence to be sorted, that is, in the original sequence, r[i]=r[j] , and r[i] is before r[j], and in the sorted sequence, r[i] is still before r[j], then this sorting algorithm is called stable; otherwise it is called unstable.

Internal sorting: sorting in which all data elements are placed in memory.

External sorting: There are too many data elements that cannot be placed in the memory at the same time. According to the requirements of the sorting process, the data cannot be moved between internal and external memory.

2. Basic idea of ​​selection sorting

Each time, the smallest (or largest) element is selected from the data elements to be sorted and stored at the beginning of the sequence until all the data elements to be sorted are arranged.

3. Directly select and sort

  • Select the data element with the largest (smallest) key code in the element set array[i]--array[n-1]
  • If it is not the last (first) element in the set, swap it with the last (first) element in the set.
  • In the remaining array[i]--array[n-2] (array[i+1]--array[n-1]) set, repeat the above steps until there is 1 element left in the set

Illustration of selection sorting: This animation selects the smallest number at the back and exchanges it with the front.

We can also optimize. If it is in ascending order, each traversal selects the smallest element and the largest element , and exchanges them with the previous and subsequent data respectively.

Code:

//交换函数
void Swap(int* p1, int* p2)
{
	int t = *p1;
	*p1 = *p2;
	*p2 = t;
}

// 选择排序 升序
void SelectSort(int* arr, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int maxi = begin;
		int mini = begin;
		for (int i = begin; i <= end; i++)
		{
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
		}
		Swap(&arr[mini], &arr[begin]);
		if (begin == maxi)
		{
			maxi = mini;
		}
		Swap(&arr[maxi], &arr[end]);
		begin++;
		end--;
	}
}

Summary of features of direct selection sort:

  1. Direct selection sort thinking is very easy to understand, but the efficiency is not very good. Rarely used in practice
  2. Time complexity: O(N^2)
  3. Space complexity: O(1)
  4. Stability: Unstable

4. Heap sort

We need to understand the structure of the heap first. If you don’t understand, you can read my previous article [Data Structure] What is this heap ?

After we understand the structure of the heap, we can start to learn heap sorting. 

Heapsort refers toa sorting algorithm designed using a data structure such as a stacked tree (heap)
. It is a type of selection sort. It selects data through the heap . It should be noted that a large heap should be built in ascending order, and a small heap should be built in descending order .

  1. First we need to build a heap,
  2. Then exchange the top element of the heap with the last element, and treat the last position element as not being in the heap.
  3. Then adjust the heap by adjusting downward. Sort by looping

Illustration:

 To build a heap, you can use the upward adjustment method to build a heap and the downward adjustment method to build a heap. These two methods are explained in detail in [Data Structure] What is a Heap  . The heap building time complexity of the upward adjustment method is O(N*logN), but the time complexity of the downward adjustment method is low, O(N) . So here we use the downward adjustment method to build the heap.

Code:

//向下调整法
void AdjustDown(int* arr, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && arr[child + 1] > arr[child])
		{
			child++;
		}
		if (arr[parent] < arr[child])
		{
			Swap(&arr[parent], &arr[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
//堆排序
void HeapSort(int* arr, int n)
{
	//建堆
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(arr, n, i);
	}
	//排序
	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[end], &arr[0]);
		AdjustDown(arr, end, 0);
		end--;
	}
}

Summary of features of heap sort:

  1. Heap sort uses a heap to select numbers, which is much more efficient.
  2. Time complexity: O(N*logN)
  3. Space complexity: O(1)
  4. Stability: Unstable

This article is over, let’s learn about it in the next article: [Exchange Sort] Bubble Sort and Quick Sort.

Guess you like

Origin blog.csdn.net/qq_72916130/article/details/132160760