Three basic sorting algorithms bubble, select, and fast

The basic idea of ​​bubble sorting is to compare two adjacent elements each time, and exchange them if they are in the wrong order.

 

Icon:

Bubble sort core program

void M_Sort(int arr[], int num)   //冒泡  也可以这样写void M_sort(int *arr,int num)   因为数组名就是数组的首地址

{

    for (int i = 0; i < num - 1; i++)//n个数排序,只用进行num-1趟

    {

        for (int j = 0; j < num - i - 1; j++)//从第0位开始比较直到最后一个尚未归位的数,想一想为什么到n-i-1就可以了(如果还想不懂就看下那个动态图)

        {

            if (arr[j] > arr[j + 1])//这里控制的是排序后的数组是从大到小还是从小到大

            {

                int Temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = Temp;

            }

        }

    }

}

 

 

 

Select sort

Direct selection sort (Selection Sort), which is a simple and intuitive sorting algorithm. It first finds the smallest (large) element in the unsorted sequence, stores it at the beginning of the sorted sequence, and then continues to find the smallest (large) element from the remaining unsorted sequence elements, and then puts it at the end of the sorted sequence . And so on, until all elements are sorted.

 

Operation as shown

 

Code (not compiled on VC6.0 written in C++11 standard)

eg:

#include <iostream>

#include <algorithm>

using namespace std;

void SelectSort(int *a, int n)

{

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

    {

        for (int j = i+1; j < n; j++)

        {

            if (a[j] < a[i])

            {

                swap(a[j], a[i]);//swap函数没写是因为algorithm算法库里有这个方法,就没自己写了

            }

        }

    }

}

int main()

{

    int a[] = { 2,1,6,9,5,7,3 };

    SelectSort(a, 7);

    for (auto &s : a)//改成for(int i=0;i<7;i++) cout<<a[i]<<" "; cout<<endl; 在VC6.0上就能编译了

    {

        cout << s << " ";

    }

    cout << endl;

    system("pause");//在VC6.0上编译的时候把这个去掉,这是VS2015编译时为了防止点击运行时黑色对话框一闪就关掉

    return 0;

}

 

Quick sort effect diagram:

 

Quick Sort After reading Aha Lei's "Aha Algorithm", a foreign algorithmic book "Algorithm Illustration" and the vernacular classic algorithm series on CSDN, the sixth quick sort. I thought that the CDSN was simple, so I copied it. Quick sort on CDSN

(From http://blog.csdn.net/morewindows/article/details/6684558 )

Basic ideas: 1. First take a number from the number sequence as the reference number.

2. In the process of zoning, put all the numbers larger than the reference number to the right of it———————— digging and filling number + divide and conquer method

All numbers less than or equal to the base number are placed to the left of it.

3. Repeat 2 for the left and right intervals until there is only one number in each interval

 

Take an array as an example, and take the first number in the interval as the base number.

0 1 2 3 4 5 6 7 8 9
72 6 57 88 60 42 83 73 48 85

Initially, i = 0; j = 9; X = a[i] = 72

Since the number in a[0] has been stored in X, it can be understood that a hole has been dug in the array a[0], and other data can be filled here.

Find a number smaller than X or equal to X starting from j. When j=8, the condition is met, a[8] is dug out and then filled into the last pit a[0]. a[0]=a[8]; i++; Such a pit a[0] is fixed, but a new pit a[8] is formed. What should I do? Simple, find a number to fill in the pit of a[8]. This time, starting from i and looking for a number greater than X, when i=3, meet the conditions, dig out a[3] and fill it in the last pit a[8]=a[3]; j--;

 

The array becomes:

0 1 2 3 4 5 6 7 8 9
48 6 57 88 60 42 83 73 88 85

 i = 3;   j = 7;   X=72

Repeat the above steps again, first looking from back to front, and then from front to back.

Start looking forward from j, when j=5, meet the conditions, dig out a[5] and fill it in the previous pit, a[3] = a[5]; i++;

Starting from i and looking backward, when i=5, exit because i==j.

At this time, i = j = 5, and a[5] happens to be the pit dug last time, so fill X into a[5].

 

The array becomes:

0 1 2 3 4 5 6 7 8 9
48 6 57 42 60 72 83 73 88 85

It can be seen that the numbers before a[5] are all less than it, and the numbers after a[5] are all greater than it. Therefore, repeat the above steps for the two sub-intervals of a[0...4] and a[6...9].

 

 

Summarize the number of digging and filling

1. i = L; j = R; The benchmark number is dug out to form the first pit a[i].

2. j--Find the number smaller than it from back to front, and dig out this number to fill the previous hole a[i] after finding it.

3. i++ searches for a number larger than it from front to back, and digs out this number and fills it in the previous pit a[j].

4. Repeat steps 2 and 3 until i==j, and fill in the reference number in a[i]

 

The code for digging and filling is as follows

int AdjustArray(int s[], int l, int r) //返回调整后基准数的位置

{

    int i = l, j = r;

    int x = s[l]; //s[l]即s[i]就是第一个坑

    while (i < j)

    {

        // 从右向左找小于x的数来填s[i]

        while(i < j && s[j] >= x) 

            j--;  

        if(i < j) 

        {

            s[i] = s[j]; //将s[j]填到s[i]中,s[j]就形成了一个新的坑

            i++;

        }



        // 从左向右找大于或等于x的数来填s[j]

        while(i < j && s[i] < x)

            i++;  

        if(i < j) 

        {

            s[j] = s[i]; //将s[i]填到s[j]中,s[i]就形成了一个新的坑

            j--;

        }

    }

    //退出时,i等于j。将x填到这个坑中。

    s[i] = x;



    return i;

}

 

 

Code for divide and conquer:

void quick_sort1(int s[], int l, int r)

{

    if (l < r)

    {

        int i = AdjustArray(s, l, r);//先成挖坑填数法调整s[]

        quick_sort1(s, l, i - 1); // 递归调用 

        quick_sort1(s, i + 1, r);

    }

}

 

 

The combination is as follows:

//快速排序

void quick_sort(int s[], int l, int r)

{

    if (l < r)

    {

        //Swap(s[l], s[(l + r) / 2]); //将中间的这个数和第一个数交换 参见注1

        int i = l, j = r, x = s[l];

        while (i < j)

        {

            while(i < j && s[j] >= x) // 从右向左找第一个小于x的数

                j--;  

            if(i < j) 

                s[i++] = s[j];

             

            while(i < j && s[i] < x) // 从左向右找第一个大于等于x的数

                i++;  

            if(i < j) 

                s[j--] = s[i];

        }

        s[i] = x;

        quick_sort(s, l, i - 1); // 递归调用 

        quick_sort(s, i + 1, r);

    }

}

 

Guess you like

Origin blog.csdn.net/qq_31702609/article/details/81293023