Commonly used sorting algorithms for arrays in C language

Table of contents

1. Some algorithms for arrays in C language

1.1 Bubble sort

1.2 Selection sorting

1.3 Insertion sort

1.4 Quick sort


1. Some algorithms for arrays in C language

Arrange the data in order from small to large or from large to small

There are many algorithms: bubble sort, selection sort, insertion sort, quick sort, counting sort, heap sort.....

There are four commonly used ones:

1.1 Bubble sort

The main idea:

A total of n-1 rounds of comparisons are required

Each round compares the current element and the following elements in sequence. If the current element is larger than the following elements, swap their positions.

After one round, the largest element is placed at the end of the array.

int a[10] = {50,23,80,18,100,5,10,58,30,2};

第一轮:

23,50,18,80,5,10,58,30,2,100

第二轮:

23,18,50,5,10,58,30,2,80,100

......

for(i=0;i<10-1;i++)//比较10-1轮

{

for(j=0;j<10-1-i;j++)

{

if(a[j] > a[j+1])

{

//交换

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

1.2 Selection sorting

A total of n-1 rounds of comparisons are required

Each round of comparison only exchanges data once at most (putting the largest number at the end or the smallest number at the front)

 

 

#include<stdio.h>

int main()

{

    int a[10] = {50,23,80,18,100,5,10,58,30,2};

    

    int i,j;

    int temp;

   

    for(i=0;i<10-1;i++)//进行n-1轮比较

    {

        int max = a[0];//假设最大的数字是a[0]

        int index = 0;//用来保存最大值的下标

        for(j=0;j<10-i;j++)//每一轮比较把最大的数字放在最后面

        {

            if(a[j] > max)

            {

                max = a[j];

                index = j;

            }

        }

        

        //至此我们已经最大值为 max, 他的下标为index ,交换 a[index] 所在元素和 a[9-i]

        if(index != 9-i)

        {

            temp = a[index];

            a[index] = a[9-i];

            a[9-i] = temp;

        }

    }

    for(i=0;i<10;i++)

    {

        printf("%d ",a[i]);

    }

    printf("\n");

    return 0;

 }

Similarly, put the smallest last:

#include<stdio.h>

int main()

{

    int a[10] = {50,23,80,18,100,5,10,58,30,2};

    

    int i,j;

    int temp;

   

    for(i=0;i<10-1;i++)

    {

        //每一轮比较把最小的数字放在最前面

        int min = a[9];

        int index = 9;

        for(j=0+i;j<10;j++)

        {

            if(a[j]<min)

            {

                min = a[j];

                index = j;

            }

        }

        

        //至此我们已知最小值为 min ,他的下标为index ,交换 a[index] 所在元素和 a[i]

        if(index != i)

        {

            temp = a[index];

            a[index] = a[i];

            a[i] = temp;

        }

    }

    

    for(i=0;i<10;i++)

    {

        printf("%d ",a[i]);

    }

    printf("\n");

    return 0;

}

1.3 Insertion sort

Algorithm idea: Direct insertion sort is to insert an unordered sequence into an ordered sequence. It is usually assumed that a[0] is an already sorted subsequence, and then the remaining unordered sequences are inserted into the ordered subsequence one by one. It is suitable for situations where the data is basically ordered and the amount of data is not large.

 

#include<stdio.h>

#include<math.h>



int main()

{

    int a[10] = {999,10,15,18,5,30,80,26,345,-10};

    

    int i,j;

    for(i=1;i<10;i++)//总共需要插入9轮

    {

        //把 a[i] 插入到前面的有序集合中,使之仍然有序

        int data = a[i];

        for(j=i-1;j>=0;j--)

        {

            if(a[j]>data)

            {

                a[j+1] = a[j];

            }

            else

            {

                a[j+1] = data;

                break;

            }

        }

        

        if(j==-1)

        {

            a[0] = data;

        }

    }

        

    

    for(i=0;i<10;i++)

    {

        printf("%d ",a[i]);

    }

    printf("\n");

    

}

1.4 Quick sort

1. First select a number from the array as the reference point, which can be selected randomly;

2. Place the items in the array that are larger than the base point to the right of the base point, and the items that are smaller than the base point to the left of the base point;

3 Quickly sort the left and right arrays.

Code example:

#include <stdio.h>

//快速排序 有左右两边 因此我需要传进来左右的下标

void FastSort(int a[],int left,int right)

{

    //当左边比右边不得小 我们就没有必要排序了

    if(left >= right)

        return;

    int l = left;

    int r = right;

    //设置基准点 我这里设置的是第一个

    int temp = a[left];

    //将我们的数组进行一次快排

    //将temp的左边变得都比temp小,右边都比temp大

    while(l < r)

    {

        //由于你的基准点是在左边第一个  因此首先就要从右边找到左边

        //将右边小于基准点的元素弄到左边去

        for(;r > l;r--)

        {

            if(temp > a[r])

            {

                //将这个小一点的数弄到左边去

                a[l] = a[r];

                break;

            }

        }

        

        //然后从左边找到右边去 找到比基准点大的 放在右边去

        for(;r > l;l++)

        {

            if(temp < a[l])

            {

                //将大的数弄到右边去

                a[r] = a[l];

                break;

            }

        }        

    }

    a[l] = temp;//恢复基准点

    

    //以相同的规则将左边的排序

    FastSort(a,left,l - 1);

    

    //以相同的规则将右边排序

    FastSort(a,r + 1,right);

    

    

}





//打印数组

void PrintArr(int arr[],int n)

{

    for(int i = 0;i < n;i++)

        printf("%d ",arr[i]);

    printf("\n");

    

}



int main()

{

    int a[] = {12378,34,412,453,34,25,4,432,5,43};

    

    FastSort(a,0,sizeof(a) / sizeof(a[0]) - 1);

    

    

    PrintArr(a,sizeof(a) / sizeof(a[0]));

    

    

    return 0;

}

 

Guess you like

Origin blog.csdn.net/qq_73698300/article/details/129827025