Bucket Sort, Bubble Sort, Selection Sort, Quick Sort Review

bucket sort


  1. When I first learned about bucket sorting, it was a topic in a C language textbook. The question probably means to rank the scores of 30,000 students on a scale from 0 to 100. The time complexity of bucket sort is O(M+N). Therefore, you can apply for an array of type int with a size of 100, then initialize the array to 0, and then regard the subscript of the array as a score, and use the value stored in the array element to correspond to the number of people who obtained the score, so that the score is I have a ranking in the array, and finally use the loop to output in turn, but when outputting, it depends on how many people have obtained the score, and then repeat the output as many times.

  2. The implementation code is as follows


#include<stdio.h>
int main()
{
    int score;
    int a[100];

    /*初始化数组 */
    for(int i=0; i<100; i++)
    {
        a[i] = 0;
    }

    /*输入学生成绩,加入有十个人的成绩*/
    for(int i=0; i<10; i++)
    {
        scanf("%d",&score);
        a[score]++;
    }

    for(int i=0; i<100; i++)
    {
        if(a[i]!=0)
        {
            for(int j=0; j<a[i]; j++)
            {
                printf("%d ",i);
            }
        }
    }
    printf("\n");
    return 0;
}

Summary:
Bucket sorting is the fastest and simplest sorting, and the time complexity is only O(m+n), but bucket sorting is a waste of space and has limitations (for example, only integers can be output, and bucket sorting cannot be used for decimals)

Bubble Sort


  1. The basic idea of ​​bubble sort is to compare two adjacent elements and swap them if the order is wrong. If there are 10 numbers that need to be sorted, then the large loop will be sorted n-1 times (because only one number can be determined each time), so the small loop needs to compare adjacent numbers according to the number after the reset, it is necessary to Compare n-1-i times. The time complexity is O(n^2). The core part of the code is a double loop.

  2. code

#include<stdio.h>
int main()
{
int t;
    int a[10];
    for(int i=0; i<10; i++)
    {
        scanf("%d",&a[i]);
    }
    int n = 10;
    /*冒泡排序*/

    for(int i=0; i<n-1; i++)
    {
        for(int j=0; j<n-i-1; j++)
        {
            if(a[j] > a[j+1])
            {
                t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }
        }
    }

    for(int i=0; i<n; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

Summary:
Bubble sort can only return one number per large pass, so when there are n numbers, (n-1) numbers need to be reset. The time complexity is O(N^2).

selection sort

  1. The principle of selection sort is: first find the smallest (largest) element in the unsorted sequence, put it at the beginning of the sequence, and then find the smallest (largest) element from the remaining elements and put it at the end of the sequence. Loop in sequence until the sorting is complete.

    Selection sort swaps between 0 and (n - 1) times. The comparison operation of selection sort is between n (n - 1) / 2 times. The assignment operation for selection sort is between 0 and 3 (n - 1) times.
    The number of comparisons is O(n^2). The number of comparisons has nothing to do with the initial state of the keyword. The total number of comparisons is N=(n-1)+(n-2)+…+1=n*(n-1)/2 . The number of exchanges is O(n). The best case is that it has been ordered, and the exchange is 0 times; the worst case is n-1 times, and the reverse order is exchanged n/2 times. The number of swaps is much less than that of bubble sort. Since swap requires more CPU time than comparison, selection sort is faster than bubble sort when n is small.

  2. code


#include<stdio.h>
int main()
{
    int a[10];
int n,min,t;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=0; i<n-1; i++)
    {
        min = i;
        for(int j=i+1; j<n; j++)
        {
            if(a[j] < a[min])
            {
                min = j;
            }
        }
        if(min != i)
        {
            t = a[min];
            a[min] = a[i];
            a[i] = t;
        }
    }

    for(int i=0; i<n; i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
}

Summary:
Selection sort has fewer swaps than bubbling, so it's faster than bubbling.

quick sort


  1. Quicksort has been on the data structure before, but there is no actual hand code. In quicksort, the comparison span is larger and it is faster than bubbling. Quick sort needs to find a radix, and then retrieve from the front and back of a bunch of data, put the numbers smaller than the radix on the left, and the numbers larger than the radix on the right (if it is sorted from small to large). Compared with bubble sort, quick sort jumps every exchange, and bubble is just a comparison of two adjacent numbers. The average time complexity is O(NlogN). The idea of ​​quicksort is based on the dichotomy.
  2. If the sorted number column is 6 23 45 98 7 10, then the first number is the base, and the base is 6, and then start to probe from both ends, starting from 10 to the left to search for numbers less than 6 until it is found, then start from 6 Probe to the right, find a number greater than 6, and then exchange, until the mark point detected from left to right meets the mark point from right to left, the first round of detection is over, and then the second round, With 6 as the dividing point, the sequence is divided into two and the second round of detection is performed at the same time, until all the numbers are returned, and the sorting ends.
  3. code

#include<stdio.h>
int a[1000];
void quicksort(int left, int right)
{
    /*递归结束*/
    if(left > right)
    {
        return;
    }
    int temp,t,i,j;
    temp = a[left];
    i = left;
    j = right;
    while(i != j)
    {
        //从最右边开始,与基数相比,要找出比基数小的,排在基数的左边,若比基数大,继续往左边检索
        while(a[j] >= temp && i < j)
        {
            j--;
        }
        //j检索结束后,i再开始检索比基数大的数字,比基数大的数字排在基数右边,若比基数小,继续向右边检索
        while(a[i] <= temp && i < j)
        {
            i++;
        }
        if(i < j)
        {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }

    /*当i与j相遇的时机,基数归位结束,此时要交换基数的位置,temp相当于中间交换变量*/
    a[left] = a[i];
    a[i] = temp;

    /*再以二分法的思想,进行再次分别排序,利用递归*/
    quicksort(left,i-1);
    quicksort(i+1,right);
}

int main()
{
    int n;
    printf("please intput the count:\n");
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    quicksort(0,n-1);
    for(int i=0; i<n; i++)
    {
        printf("%d\t",a[i]);
    }
    printf("\n");
    return 0;
}

Summarize:

The code implementation is achieved through recursion, so pay attention to the conditions for the end of the recursion.


There are quite a lot of sorting methods, I hope I can slowly comprehend it from my thoughts and codes. . . .

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361844&siteId=291194637