当前位置:网站首页>Insert sort, select sort, bubble sort

Insert sort, select sort, bubble sort

2022-07-04 22:51:00 Winter snow is like spring

1. Insertion sort
The understanding principle of colloquialism : Think of the first number as ordered , Start with the second number and compare with the number before it ,( The default order is from small to large ) If this number is smaller than the previous one , Then take this number , In a temporary variable , Do the same thing , Knowing that the number of the previous coin is small, put this number behind the number smaller than him .

#include <stdio.h>

void InsertSort(int array[], int len)
{
    int i; 
    int j;
    int temp;

    for(i = 1; i < len; i++)
    {
        if(array[i] < array[i-1])
        {
            temp = array[i];

            for(j = i - 1; j >= 0 && array[j] > temp; j--)
            {
                array[j+1] = array[j];
            }
            
            array[j+1] = temp;
        }
    }
}

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

int main()
{
    int array[10] = {23,1,3,54,32,4,56,0,34,8};
    print(array, 10);
    printf("\n");
    InsertSort(array, 10);
    print(array, 10);

    return 0;
}

2. Selection sort
Selection sorting is to start with the first number of a group of data , In the array, the subscript is from 0 Start , Record the position of the first number , Compare with the following numbers one by one , If there is a number smaller than this number , Then replace the position of this number with the position of the previously recorded number , Keep comparing until the last number , Then put the smallest number behind the ordered data , One analogy . After ranking, there is no need to compare , Compare back from the position that has not been sorted .

#include <stdio.h>

void SelectionSort(int array[], int len)
{
    int i,j,k,temp;
    for(i = 0; i < len; i++)
    {
        k = i;
        for(j = i + 1; j < len; j++)
        {
            if(array[j] < array[k])
            {
                k = j;
            }
        }
        temp = array[k];
        array[k] = array[i];
        array[i] = temp;
    }
}

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

int main()
{
    int array[10] = {23,1,3,54,32,4,56,0,34,8};
    print(array, 10);
    printf("\n");
    SelectionSort(array, 10);
    print(array, 10);

    return 0;
}

3. Bubble sort

#include <stdio.h>

void BubbleSort(int array[], int len)
{
    
    int i,j,k,temp;
    for(i = 0; i < len; i++)
    {
    
        for(j = 0; j < len - i - 1; j++)
        {
    
            if(array[j] > array[j+1])
            {
    
                temp = array[j];
                array[j] = array[j+1];
                array[j+1] = temp;
            }
        }
      
    }
}

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

int main()
{
    
    int array[10] = {
    23,1,3,54,32,4,56,0,34,8};
    print(array, 10);
    printf("\n");
    BubbleSort(array, 10);
    print(array, 10);

    return 0;
}

原网站

版权声明
本文为[Winter snow is like spring]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041956187711.html