Sorting algorithm: non-comparison sort (counting sort)

Friends, guys, we meet again. In this issue, I will explain to you some relevant knowledge points about sorting algorithms . If you have some inspiration after reading it, then please leave your three links and I wish you all the best. It’s done!

C language column: C language: from entry to proficiency

Data Structure Column: Data Structure

Personal homepage: stackY、

Table of contents

1. Counting sorting

1.1 Basic logic:

1.2 Improved logic:

Code demo:

Test code:

1.3 Defects of counting sort:

 2. Summary of features


1. Counting sorting

The sorting algorithms we have come across before are all based on comparing two numbers and sorting on demand. In this issue, let’s take a look at a new sorting algorithm that can be sorted without comparison: counting sorting.

1.1 Basic logic:

1. Count the number of occurrences of each data in a set of arrays

2. Return according to the counted number of times 

There is a set of data, and there is also an array CountA that counts the number of times (all are 0 by default). Traverse this set of data from the beginning. Assuming that the first element of this set of data is 6, then the subscript in CountA needs to be 6. Add one to the elements, and so on, until the entire data is traversed.

Count the number of occurrences of each data, and copy the data that is not 0 to the original array in order according to the number of occurrences. In this way, the sorting is completed.

Then there is a problem here. Not every sorting is data within 10. So if the data is sorted between 100~109, do we also need to open an array as large as 0~109 and only use 100~ It is possible to perform data statistics in the 109 interval, but it is too wasteful of space, so we can improve it.

1.2 Improved logic:

First find the maximum and minimum values ​​in the entire data , find their difference, that is, the range size , then create an array CountA that counts the number of times, set all the values ​​in it to 0, and then it is time to count the number of times. When counting the number of occurrences, you need to subtract the minimum value from its original value , and then find the corresponding subscript in the created array CountA, and so on. After traversing the entire data, you need to copy the data to the original array according to the corresponding number of times. When copying back from CountA, the sorting can be completed by adding the minimum value to the subscript in ConutA and then copying it to the original array.

Code demo:
//非比较排序
//计数排序
void CountSort(int* a, int n)
{
	//找出最大值和最小值
	int min = a[0], max = a[0];
	for (int i = 0; i < n; i++)
	{
		if (a[i] < min)
		{
			min = a[i];
		}
		if (a[i] > max)
		{
			max = a[i];
		}
	}

	//计算出范围
	int range = max - min + 1;

	//开辟空间
	int* CountA = (int*)malloc(sizeof(int) * range);
	if (CountA == NULL)
	{
		perror("CountA malloc fial");
		exit(-1);
	}
	memset(CountA, 0, sizeof(int) * range);
	
	//统计次数
	for (int i = 0; i < n; i++)
	{
		CountA[a[i] - min]++;
	}

	//排序
	int k = 0;
	for (int j = 0; j < range; j++)
	{
		//根据出现的次数拷贝几次
		while (CountA[j]--)
		{
			a[k++] = j + min;
		}
	}
}
Test code:
void TestCountSort()
{
	int a[] = { 6,1,6,8,5,8,5,4,2,1 };
	PrintArry(a, sizeof(a) / sizeof(int));
	CountSort(a, sizeof(a) / sizeof(int));
	PrintArry(a, sizeof(a) / sizeof(int));
}

int main()
{
	TestCountSort();
	return 0;
}

1.3 Defects of counting sort:

1. Depends on the data range, applicable to arrays in the range set.

2. Can only be used for plastic surgery.

 2. Summary of features

1. Counting sorting is very efficient when the data range is concentrated, but its applicable scope and scenarios are limited.
2. Time complexity: O(MAX(N, range))   (Whichever is larger, N or range, is the one)
3. Space complexity: O(range)
4. Stability: Stable

 

Friends and guys, good times are always short. Our sharing in this issue ends here. Don’t forget to leave your precious three pictures after reading it. Thank you all for your support! 

Guess you like

Origin blog.csdn.net/Yikefore/article/details/132869836