Sorting Algorithms - 2 Simple Selection Sort Algorithms

Select sort:

Time complexity: No matter what sort of data is sorted, the time complexity is O(n²).

Advantages: Compared with bubble sorting, the number of times to exchange mobile data is quite small, only one exchange per trip, and does not occupy additional memory space.

The main idea: Scan the sequence from the beginning to the 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.

Algorithm process example:

Original sequence: 45, 24, 75, 66, 32

1) In the first pass, assign the subscript of the first array element 45 to min, compare it with the following elements one by one, and find that 24 is smaller than 45, then the current smallest element is 24, and assign the array subscript of 24 to min , continue to compare backwards, and find that the smallest element in the entire sequence is 24, then exchange 24 with 45;

a[0]=45, a[1]=24, min=0; a[0]>a[1], then min=1...min always represents the subscript of the smallest element, Finally exchange a[0] and a[min];

The first comparison: { 24 }, {45, 75, 66, 32} is preceded by a sorted sequence, followed by an unsorted sequence

2) In the second pass, assign the subscript of the second element 45 to min, compare with the following elements one by one, and find that 75 is larger than 45, then 45 is still the smallest element, continue to compare, and finally find that 32 is smaller than 45, then 32 is the smallest element of the current sequence, assign the 32 element subscript to min, and finally exchange 45 with 32;

a[1]=45,a[2]=75,min=1;a[1]<a[2],min=1..........a[4]=32,a[ 1]>a[4]=32, min=4, exchange a[1] and a[4]

Second comparison: { 24, 32 }, { 75, 66, 45 }

3) Continue to repeat the above steps for a total of N-1 comparisons

original sequence 45 24 75 66 32 Current sequence minimum 24
first trip comparison 24 45 75 66 32 The remaining sequence minimum value is 32
Second comparison 24 32 75 66 45 The remaining sequence minimum value is 45
third comparison 24 32 45 66 75 The minimum value of the remaining sequence is 66
Fourth comparison 24 32 45 66 75 Sort complete

A total of Ni comparisons are performed, and each time the smallest element is first found, and after the smallest element subscript is obtained, it is exchanged with the unsorted first element

Specific procedures:

#include<stdio.h>
void swap(int a[],int b[])  //将交换部分单独写成函数
{
    int temp=*a;
    *a = *b;
    *b = temp;
}

void selection_sort(int a[],int len)  //选择排序
{
    int i,j;
    for(i=0;i<len-1;i++)   //控制趟数
    {
        int min=i;    //将当前未排序元素的下标赋给min
        for(j=i+1;j<len;j++)  //找出未排序序列中最小元素的下标
        {
            if(a[j]<a[min])
                min=j;   //将当前最小元素下标赋给min
        }
        swap(&a[i],&a[min]);  //交换当前趟次比较找出的最小元素
    }}

int main()
{
    int i,m;
    int a[]={45,24,75,66,32};
    m=sizeof(a)/sizeof(a[0]); //计算数组元素的个数
    printf("排序前:");
    for(i=0;i<m;i++)
    {
        printf("%d ",a[i]);
    }
    selection_sort(a,m);
    printf("\n排序后:");
    for(i=0;i<m;i++)
    {
        printf("%d ",a[i]);
    }
    while(1);
}

operation result:

Guess you like

Origin blog.csdn.net/Healer19/article/details/117440779