[Data structure] Insertion, Hill, selection, bubble four sorting algorithms

1. The concept of sorting and its application

1.1 The concept of sorting algorithm

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 of the keywords.
Stability : Assume that there are multiple records with the same keyword in the sequence of records to be sorted . If sorted, the relative order of these records remains unchanged , 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], the sorting algorithm is said to be stable; otherwise it is called unstable .
Internal sorting : A sorting in which all data elements are placed in memory .
External sorting : Too many data elements cannot be placed in memory at the same time, and the sorting of data cannot be moved between internal and external memory according to the requirements of the sorting process.
 

1.2 Application of sorting algorithm:

For example, when buying something again, it is the price of the product, the number of good reviews, and the score ranking of the students after the college entrance examination, the ranking of the school, etc.;

Sorting algorithms are used very frequently in daily life, so this is an algorithm we must master

1.3 Common sorting algorithms

 2. Implementation of the sorting algorithm

2.1 Direct insertion sort

Direct insertion sorting is a simple insertion sorting, the basic idea of ​​which is:

Insert the records to be sorted into an ordered sequence one by one according to the size of their key values, until all records are inserted, and a new ordered sequence is obtained

 When inserting the i-th (i>=1) element, the previous array[0], array[1],...,array[i-1] have been sorted, and at this time use the sort code of array[i] and Array[i-1], array[i-2],... compare the order of the sorting codes, find the insertion position and insert array[i], and the order of the elements at the original position is moved backward

When sorting in ascending order, insertion sorting is equivalent to saving the selected data, and then moving the data that is larger than it to the back. When there is no data in front of it, or the data in front is smaller than it, the number is taken, and then sorted A number, one after the other, and finally the picture is finished

void Insert(int* a, int n)
{
	for (int i = 0; i < n-1; i++)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			if (tmp < a[end])
			{
				a[end + 1] = a[end];
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;
	}
}

Summary of direct insertion sort features:

1. The closer the element set is to order, the higher the time efficiency of the direct insertion sorting algorithm

2. The time complexity is (N^2) , when the collection is close to order, the time complexity is (N)

3. The space complexity is (1)

4. Stability: stable

 2.2 Hill sort

The Hill sorting method is also known as the shrinking increment method. The basic idea of ​​the Hill sorting method is: first select an integer gap, divide all the records in the file to be sorted into n groups, and group all records with a distance of gap into the same group, and sort the records in each group . Then take gap/2 in turn, and repeat the above grouping and sorting work. When gap=1 is reached, all records are sorted in the same group.

 When the gap is divided by two in turn and the gap is equal to 1, the Hill sort at this time is equivalent to the direct insertion sort . The Hill sort is similar to the direct insertion sort, that is, an array is divided into several groups, pre-sorted, and finally When the gap is equal to one, the group of arrays is close to order, and then repeat the above method and arrange it again to be OK

void Shellsort(int* a, int n)
{
	int gap = n;
	while (gap > 0)
	{
		gap/=2;
		// i<n-gap防止越界!!
		for (int i = 0; i < n-gap; i ++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (tmp < a[end])
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}
		
	}
}

Summary of the characteristics of Hill sorting:

1. Hill sort is an optimization of direct insertion sort

2. When gap>1, it is pre-sorted. The purpose is to make the array close to order. When the array is close to order, it will be very fast, so that the overall optimization effect is achieved.

3. The time complexity of Hill sorting is not easy to calculate, because the value of gap is not uniform (about O(n^1.25)~~O(1.6*n^1.25) )

4. Stability: Unstable

2.3 Selection sort

The idea of ​​selection sorting: Each time the smallest number is selected and the corresponding number at the starting position is exchanged, and so on.

When traversing the array, you can find that the smallest number can be selected, and the largest number can also be selected together. So you can select the smallest number and the largest number each time, and then let the smallest number and the starting position The number corresponding to the first subscript is exchanged, and then the largest number is exchanged with the number corresponding to the last subscript, and then the range is narrowed down one by one to cycle

void Swap(int* p1, int* p2)
{
		int tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
}

void Selectsort(int* a, int n)
{
	int left = 0, right = n - 1;
	while (left < right)
	{
		int maxi = left, mini = left;
		for (int i = left + 1; i <= right; i++)
		{
			if (a[maxi] < a[i])
			{
				maxi = i;
			}
			if (a[mini] > a[i])
			{
				mini = i;
			}
		}
			Swap(&a[left],&a[mini]);

			if (maxi == left)//为了防止最大的数的下标在起点位置,交换乱序
			{
				maxi = mini;
			}
			Swap(&a[right], &a[maxi]);
			left++;
			right--;
	}
}

A summary of the properties of selection sort:

1. Time complexity: O(N^2)

2. Space complexity: O(1)

3. Stability: Unstable

2.4 Bubble sort

Bubble sorting is a kind of exchange sorting, the core is bubbling, the largest one in the array is bubbled up, and the process of bubble is to exchange with its adjacent elements. Repeatedly visit the sequence to be sorted, and compare the sorting codes of adjacent records by pairwise comparison. During the sorting process, a maximum value is taken from front to back each time , and the final position of a number in the sequence can be determined each time. If the reverse order occurs, it is exchanged; there are two ways to bubble, one is to bubble the small element to the front first, and the other is to bubble the large element to the back, the following code is to bubble the large element first soak to the back

void Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

void bubbleosrt(int* a, int n)
{
	int end = n;
	while (end > 0)
	{
		int flag = 0;
		for (int i = 0; i < end-1; i++)
		{
			if (a[i + 1] < a[i])  //小于就交换
			{
				flag = 1;
				Swap(&a[i + 1], &a[i]);
			}
		}
		if (flag == 0)break;
		end--;
	}
}

Summary of Bubble Sort

1. Time complexity: O(N^2)

2. Space complexity: O(1)

3. Stability: stable

2.5 Heap sort

Heap sort Go to this   heap sort implementation

Guess you like

Origin blog.csdn.net/m0_72532428/article/details/129958960