Select Sorting details

1, Concept and principle

Select Sorting: select the record with the smallest keyword from the records to be sorted each time, and place it at the end of the sorted record sequence until all the records are sorted.

2, Applicable instructions

1. Time complexity

The time complexity of simple selection sorting is O(n^2).

2. Space complexity

Only when two records are exchanged, an auxiliary space is required, so the space complexity is O(1).

3. Algorithm features

  1. As far as the selection sorting method itself is concerned, it is a stable sorting method. Stability or instability depends on the strategy of "exchanging records" adopted by the calculation. According to this, you can judge whether the selection sorting you write is stable.
  2. Can be used for chained storage structures
  3. The fewer records are moved, and when each record occupies more space, this method is faster than direct insertion sorting.

4. Animation demonstration

3, Double loop implementation code demonstration

1. Non optimized code implementation

#include <stdio.h>
#include <stdlib.h>

void selectSort(int *array, int len)
{
	int i = 0, j = 0;
	for (i = 0; i < len - 1; ++i)     //That is, the number of times to sort, and the number of times to arrange can be arranged
	{
		int temp=i;                       //Initially, the first element will be defaulted to the smallest element
		for (j = i+1; j < len; ++j)   //Times of comparison pay attention to who to compare with
		{
			if (array[j] < array[temp])       //This condition is to find a smaller number than the default minimum element in the array and record the position
			{
				temp = j;
			}
		}
		if (temp != i)
		{
			int ptemp1 = array[temp];          //At this point, after the above loop, a smaller number than the default has been found and the swap operation is performed
			array[temp] = array[i];
			array[i] = ptemp1;
		}
	}
}

void printArray(int *array,int len)
{
	for (int i = 0; i < len; ++i)
	{
		printf("%4d", array[i]);
	}
}

int main(void)
{
	int array[10] = { 78,15,99,53,84,27,39,1,41,67 };
	int len = sizeof(array) / sizeof(int);

	//Output array before sorting
	printf("Array before sorting\n");
	printArray(array, len);
	putchar('\n');

	//Call the select sort function to sort
	selectSort(array, len);

	//Output sorted array
	printf("Sorted array\n");
	printArray(array, len);
	putchar('\n');

	system("pause > 0");
	return 0;
}

The running results of the code are as follows:

2. Optimized code implementation

Optimizing the selective sorting of double-layer loops
Find the maximum number and the minimum number through a cycle, and put the maximum number on the right and the minimum number on the left
Note: there is a special case at this time, that is, when the maximum number I want to exchange is just exchanged by the minimum number, it is exchanged. At this time, we need to add a judgment. After optimization, only half the efficiency is directly improved

#include <stdio.h>
#include <stdlib.h>

void optimizeSelectSort(int* array, int len)
{
	int i = 0, j = 0;				//Define cycle factor
	int iLeft = 0;					//Record the leftmost element
	int iRight = len - 1;			//Record the rightmost element
	int iMax = 0, iMin = 0;

	while (iLeft < iRight)
	{
		iMax = iMin = iLeft;
		for (i = iLeft; i <= iRight; ++i)
		{
			//Find the smallest number
			if (array[i] < array[iMin])
				iMin = i;               //Remember the minimum number position
			//Find the maximum number
			if (array[i] > array[iMax])
				iMax = i;             //Remember the location of the maximum number
		}
		//A swap operation is required
		//Put the smallest number to the left
		if (iMin != iLeft)
		{
			int ptemp = array[iLeft];
			array[iLeft] = array[iMin];
			array[iMin] = ptemp;
		}
		//Here we need to judge if the maximum number is exchanged just when the minimum number is exchanged
		if (iMax==iLeft)
		{
			iMax = iMin;
		}
		//Put the maximum number to the right
		if (iMax != iRight)
		{
			int ptemp = array[iRight];
			array[iRight] = array[iMax];
			array[iMax] = ptemp;
		}
		iLeft++;
		iRight--;
	}
}

void printArray(int* array, int len)
{
	for (int i = 0; i < len; ++i)
	{
		printf("%4d", array[i]);
	}
}

int main(void)
{
	int array[10] = { 78,15,99,53,84,27,39,1,41,67 };
	int len = sizeof(array) / sizeof(int);

	//Output array before sorting
	printf("Array before sorting\n");
	printArray(array, len);
	putchar('\n');

	//Call the select sort function to sort
	optimizeSelectSort(array, len);

	//Output sorted array
	printf("Sorted array\n");
	printArray(array, len);
	putchar('\n');

	system("pause > 0");
	return 0;
}

The running results of the code are as follows:

4, Recursive implementation code demonstration

1. Non optimized code implementation

#include <stdio.h>
#include <stdlib.h>

void recursionSelectSort(int *array, int len)
{
	if (len < 2)                      //Recursive exit   
		return;

	int i = 0;
	int temp = 0;
	for (i = 1; i < len; ++i)        //Recursion is different from double-layer loop, and the conditions for judging exchange are different
	{
		if (array[i] < array[temp])       //This condition is to find a smaller number than the default minimum element in the array and record the position
			temp = i;
	}
	if (temp != 0)                          //At this time, the exchange is different from non recursion. You need to exchange the found number with the number at the first position
	{
		int ptemp1 = array[temp];          //At this point, after the above loop, a smaller number than the default has been found and the swap operation is performed
		array[temp] = array[0];
		array[0] = ptemp1;
	}
	recursionSelectSort(array + 1, --len);
}

void printArray(int *array,int len)
{
	for (int i = 0; i < len; ++i)
	{
		printf("%4d", array[i]);
	}
}

int main(void)
{
	int array[10] = { 78,15,99,53,84,27,39,1,41,67 };
	int len = sizeof(array) / sizeof(int);

	//Output array before sorting
	printf("Array before sorting\n");
	printArray(array, len);
	putchar('\n');

	//Call the recursive selection sort function to sort
	recursionSelectSort(array, len);

	//Output sorted array
	printf("Sorted array\n");
	printArray(array, len);
	putchar('\n');

	system("pause > 0");
	return 0;
}

The running results of the code are as follows:

2. Optimized code implementation

Optimizing the selective sorting of recursive loops
Find the maximum number and the minimum number through a cycle, and put the maximum number on the right and the minimum number on the left
Note: there is a special case at this time, that is, when the maximum number I want to exchange is just exchanged by the minimum number, it is exchanged. At this time, we need to add a judgment. After optimization, only half the efficiency is directly improved

#include <stdio.h>
#include <stdlib.h>

void optimizeRecursionSelectSort(int* array, int len)
{
	if (len < 2)                      //There is no need to sort the two elements in the recursive exit array
	{
		return;
	}
	int i = 0;
	int iLeft = 0;
	int iRight = len - 1;
	int iMax = 0, iMin = 0;
	for (i = iLeft; i <= iRight; i++)
	{
		if (array[i] < array[iMin])
		{
			iMin = i;
		}
		if (array[i] > array[iMax])
		{
			iMax = i;
		}
	}
	//A swap operation is required
	//Put the smallest number to the left
	if (iMin != iLeft)
	{
		int ptemp = array[iLeft];
		array[iLeft] = array[iMin];
		array[iMin] = ptemp;
	}
	//Here we need to judge if the maximum number is exchanged just when the minimum number is exchanged
	if (iMax == iLeft)
	{
		iMax = iMin;
	}
	//Put the maximum number to the right
	if (iMax != iRight)
	{
		int ptemp = array[iRight];
		array[iRight] = array[iMax];
		array[iMax] = ptemp;
	}
	len = len - 2;

	optimizeRecursionSelectSort(array + 1, len);
}

void printArray(int* array, int len)
{
	for (int i = 0; i < len; ++i)
	{
		printf("%4d", array[i]);
	}
}

int main(void)
{
	int array[10] = { 78,15,99,53,84,27,39,1,41,67 };
	int len = sizeof(array) / sizeof(int);

	//Output array before sorting
	printf("Array before sorting\n");
	printArray(array, len);
	putchar('\n');

	//Call the recursive selection sort function to sort
	optimizeRecursionSelectSort(array, len);

	//Output sorted array
	printf("Sorted array\n");
	printArray(array, len);
	putchar('\n');

	system("pause > 0");
	return 0;
}

The running results of the code are as follows:

Tags: Algorithm

Posted on Sat, 20 Nov 2021 22:22:31 -0500 by CONTEMAN