Selection sort and radix sort of the eight major sorts of data structure

1. Selection sort

1.1 Selection sort introduction

Just like stock trading, some people like to speculate in short-term, constantly buying and selling to make profits through price differences, but frequent buying and selling will also benefit less because of frequent handling fees and a series of fees; some people , constantly observe and judge, and when the time comes, buy or sell decisively. This kind of person trades less often, but ultimately gains a lot; as we said, the first type of person is similar to the bubble sort in sorting , while the second type of person can be understood as:When sorting, find the appropriate keyword and then do the exchange, and only exchange once to complete the sorting of the corresponding keyword; This is what we call selection sort.

1.2 Basic idea and algorithm analysis of selection sort

Basic idea: scan the sequence from beginning to end, find the smallest element, exchange it with the first element, and then continue this selection and exchange method from the remaining elements, and finally get an ordered sequence

Analysis of Algorithms:

  1. Step 1: Find the smallest number among the unsorted n numbers (a[0] ~a[n- 1]), exchange it with a[0];
  2. Step 2: Find the smallest number among the remaining unsorted n- 1 numbers (a[1] ~a[n- 1]), exchange it with a[1];
  3. Step n-1: Find the smallest number among the remaining 2 unsorted numbers (a [n-2] ~a [n- 1] ), exchange it with a [n-2];
  4. Get a sorted sequence.

1.3 Example description

Taking 12, 32, 2, 60, 42, 98 as an example, the sorting process is as follows:
Please add a picture description

  • Numbers with a horizontal line under them are sorted
  • n values ​​can be arranged n-1 times
  • Find a minimum value every time and put it in front

1.4 Code implementation

code show as below:

void SelectSort(int arr[], int len)
{
    
    
	for (int i = 0; i < len - 1; i++)//趟数
	{
    
    
		int min_index = i;
		for (int j = i + 1; j < len; j++)//控制找最小值
		{
    
    
			if (arr[j] < arr[min_index])
			{
    
    
				min_index = j;
			}
		}

		//当内层for循环跑完,此时min_index保存是就是当前待排序序列中最小值的下标
		if (min_index != i)//如果找到的最小值下标  不等于 待排序序列的第一个值的下标  则才有交换的必要性
		{
    
    
			int tmp = arr[i];
			arr[i] = arr[min_index];
			arr[min_index] = tmp;
		}
	}
}

1.5 Performance Analysis

  1. Time complexity: O(n^2).
  2. Space complexity: O(1).
  3. Stability: Unstable.

Although the time complexity of bubble sort is O(n^2), the performance of selection sort is slightly better than bubble sort.

2. Radix sorting

2.1 Basic idea and algorithm steps of radix sorting

Basic idea: Divide the integer into different numbers according to the number of digits, then compare each digit separately, and finally combine the results.

Algorithm steps:

  1. Unify all the values ​​to be compared to the same digit length, and zero-pend the number with shorter digits;
  2. Starting from the lowest bit, sort them one by one;
  3. After sorting from the lowest bit to the highest bit, the integrated sequence becomes an ordered sequence.

2.2 Example description

Taking 12, 32, 2, 620, 42, 98, 122, 289, 987, 37, 56, 90 as an example, the sorting process is as follows:
1. Run a trip by single digit:
insert image description here
the final result of single digit sorting:
620, 90, 12, 32, 2, 42, 122, 56, 987, 27, 98, 289
(These data are ordered only if they only look at the single digit)

2. Take a trip in tens:
insert image description here
the final result of tens sorting: 2, 12, 620, 122
, 27, 32, 43, 56 , 987, 289, 90, 98
(These data are in order only by looking at ten digits)

3. Take a trip in hundreds:
insert image description here
the final result of hundreds of sorting:
2, 12, 27, 32, 43, 56, 90, 98, 122, 289, 620, 987
(data is fully ordered)

2.3 Code implementation

code show as below:

//获取数组中最大值的位数
int Get_figure(int* arr, int len)
{
    
    
	int max = 0;
	for (int i = 0; i < len; i++)
	{
    
    
		if (arr[i] > max)
		{
    
    
			max = arr[i];
		}
	}

	int count = 0;
	while (max != 0)
	{
    
    
		count++;
		max /= 10;
	}

	return count;
}
//这个函数告诉我传进来的参数n的,对应fin位是多少
//1234,2 -> 2    345,1 ->4    0078,3 -> 0     56789,4 -> 5
int Get_Num(int n, int fin)
{
    
    
	for (int i = 0; i < fin; i++)//这里代表需要n 先丢几位最低位
	{
    
    
		//n = n/10;
		n /= 10;
	}

	return n % 10;//此时获取剩余属于的最低位即可
}
//一趟桶排序    fin代表这一趟是根据哪个位进行排序(个,十,百......)   0->个位  1->十位...
void Radix(int* arr, int len, int fin)//时间复杂度O(n)
{
    
    
	//先将10个桶申请好
	int bucket[10][100] = {
    
     0 };
	int num[10] = {
    
     0 };  //num[1] 代表1号桶中有多少个有效值

	//将所有数据从左向右向对应的桶中存放
	for (int i = 0; i < len; i++)
	{
    
    
		int index = Get_Num(arr[i], fin);
		bucket[index][num[index]] = arr[i];
		num[index]++;
	}


	//按照0->9号桶的顺序,依次遵循先进先出的规则将所有值取出来
	int k = 0;
	for (int i = 0; i <= 9; i++)//0->9号桶依次取
	{
    
    
		for (int j = 0; j < num[i]; j++)//对应的桶内,从上到下依次取值
		{
    
    
			arr[k++] = bucket[i][j];//取出来的值 从前向后放到arr中
		}
	}
}
//基数排序(桶排序)  时间复杂度(d*n)(假设最大值的位数是d) 空间复杂度O(d*n) 稳定性:稳定
void RadixSort(int* arr, int len)
{
    
    
	//assert
	//1.首先需要知道 数据中最大值有多少位
	int count = Get_figure(arr, len);

	for (int i = 0; i < count; i++) //D
	{
    
    
		Radix(arr, len, i);
	}
}

2.5 Performance Analysis

Assume the number of digits of the maximum value is d

  1. Time complexity: O(d*n).
  2. Space complexity: O(d*n).
  3. Stability: Stable.

Guess you like

Origin blog.csdn.net/weixin_56935264/article/details/123855553