Selection and sorting principle and C++ source code implementation


1. Principle

For the first time, the smallest (or largest) element is selected from the data elements to be sorted and stored at the beginning of the sequence, and then the smallest (or largest) element is searched from the remaining unsorted elements, and then placed Sort the end of the column, and so on, until the number of all data elements to be sorted is zero.


2. Thinking

First find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, and then continue to find the smallest element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on, until all the elements are sorted.

Example:
Select and sort the data [1, 4, 2, 6, 7, 3, 5, 10, 9] (from small to large).

  1. The first sorting: 1 is compared with 4, 2, 6, 7, 3, 5, 10, 9. The minimum value is 1, no exchange is required, and the array [1, 4, 2, 6, 7, 3, 5, 10, 9]
  2. The second sorting: 4 is compared with 2, 6, 7, 3, 5, 10, 9, and the minimum is 2, 4 and 2 exchange positions to get the array [1, 2, 4, 6, 7, 3, 5 , 10, 9]
  3. The third sorting: 4 is compared with 6, 7, 3, 5, 10, 9. The minimum is 3, 4 and 3 exchange positions, and the array [1, 2, 3, 6, 7, 4, 5, 10 is obtained , 9]
  4. The fourth sorting: 6 is compared with 7, 4, 5, 10, 9, and the minimum is 4, 6 and 4 exchange positions to get the array [1, 2, 3, 4, 7, 6, 5, 10, 9 ]
  5. Fifth sorting: 7 is compared with 6, 5, 10, 9, the minimum is 5, and 7 is exchanged with 5 to get the array [1, 2, 3, 4, 5, 6, 7, 10, 9]
  6. The sixth sorting: 6 is compared with 7, 10, 9, and the minimum value is 6, without swapping, the array [1, 2, 3, 4, 5, 6, 7, 10, 9]
  7. Seventh sorting: Compare 7 with 10, 9, and the minimum is 7, without swapping, get the array [1, 2, 3, 4, 5, 6, 7, 10, 9]
  8. The eighth sort: 10 is compared with 9, the minimum is 9, and 10 is exchanged with 9 to get the array [1, 2, 3, 4, 5, 6, 7, 9, 10]

Three, the characteristics of selection and sorting

  • Time complexity
    Number of exchanges: select the sorting requirement to sort from left to right, from small to large: if the original array is in positive order, the number of exchanges is 0, if the original array is in other order (non-reverse order), the maximum number of exchanges can be n -1.
    Insert picture description here
    Comparison times: N = (n-1)+(n-2)+…+1=n(n-1)/2
    Because the CPU time required for exchange is more than the CPU time required for comparison, based on the final sort time It is the sum of the number of comparisons and exchanges, and the total time complexity is O(n2).

Insert picture description here

  • Stability The
    selection sort is an unstable sort.

    For example, A 80 B 80 C 70 These three papers are sorted from small to large. The
    first step will change the exchange of C and A into CBA. The
    second and third steps do not need to be exchanged. So the sort is CBA
    but the stable sort should be CAB


Four, C++ source code implementation

Brief summary: Two loops are done.


void selectionSort(int arr[], int len)
{
    
    
	int i, j, temp;
	for(i = 0; i < len-1; i++)
	{
    
    
		int smallest = i;
		for(j =i+1; j < len; j++)
		{
    
    
			if(arr[j] < arr[smallest])
			{
    
    
				smallest = j;
			}
		}
		if(i != smallest)    //不相等表示已经最小值位置有变,做交换
		{
    
    
			temp = arr[i];
			arr[i] = arr[smallest];
			arr[smallest] = temp;
		}
	}
}

Guess you like

Origin blog.csdn.net/locahuang/article/details/110173998
Recommended