十大经典排序算法

86 阅读1分钟

十大经典排序算法

冒泡排序

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXSIZE 10void initArr(int array[], int length)
{
    for (int i = 0; i < length; i++)
    {
        array[i] = rand() % 20;
    }
}
//统一调用接口
void PrintfSort(int array[], int length)
{
    for (int i = 0; i < length; i++)
    {
        printf("%d ", array[i]);
    }
    printf("\n------------------\n");
}
//统一调用接口
//*******************冒泡排序*********************
void BubbleSort(int array[], int length)
{
    int flag = 1;//优化
    while (length--&&flag/*y优化*/) {
         flag = 0;
        for (size_t i = 0; i < length; i++) //下一个位置的值小于当前的值就交换
        {
            if (array[i+1] < array[i]) {
                int temp = array[i + 1];
                array[i + 1] = array[i];
                array[i] = temp;
                flag = 1;/*y优化*/
             }
        }
​
    }
}
​
int main()
{
​
    srand((unsigned int)time(NULL));
    int array[MAXSIZE];
    initArr(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
​
    BubbleSort(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
    system("pause");
    return 0;
}
​

选择排序

其实选择排序的基本思想是冒泡排序。每一轮找到序列中的最小值后和第一个元素进行交换,以此类推。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>#define MAXSIZE 10
​
void initArr(int array[], int length) {
    for (int i = 0; i < length; i++) {
        array[i] = rand() % 20;
    }
}
​
//统一调用接口
void PrintfSort(int array[], int length) {
    for (int i = 0; i < length; i++) {
        printf("%d ", array[i]);
    }
    printf("\n------------------\n");
}
​
//统一调用接口
//*******************冒泡排序*********************
void SelectSort(int array[], int length) {
    for (size_t i = 0; i < length; i++) {
        int k = i;
        for (int j = i + 1; j < length; j++) {
            if (array[j] < array[k])
                k = j;
        }
        int temp = array[i];
        array[i] = array[k];
        array[k] = temp;
    }
}
​
int main() {
​
    srand((unsigned int) time(NULL));
    int array[MAXSIZE];
    initArr(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
​
    SelectSort(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
    system("pause");
    return 0;
}
​

插入排序

优点是:当原始序列已经基本有序时再将一个新的数据插入进来比较方便,也比较高效率

#include <stdio.h>
#include <time.h>
#include <stdlib.h>#define MAXSIZE 10void initArr(int array[], int length) {
    for (int i = 0; i < length; i++) {
        array[i] = rand() % 20;
    }
}
​
//统一调用接口
void PrintfSort(int array[], int length) {
    for (int i = 0; i < length; i++) {
        printf("%d ", array[i]);
    }
    printf("\n------------------\n");
}
​
//统一调用接口
//*******************冒泡排序*********************
void InsertSort(int array[], int length) {
    for (int i = 0; i < length; i++) {
        for (int j = i; j >= 1 && array[j] < array[j - 1]; j--) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j - 1] = temp;
        }
    }
}
​
int main() {
​
    srand((unsigned int) time(NULL));
    int array[MAXSIZE];
    initArr(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
​
    InsertSort(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
    system("pause");
    return 0;
}
​

希尔排序

优化版的插入排序。优化的地方就是步长增大了。原来的插入排序是的步长是1,而希尔排序的步长可以很大,然后逐渐减小直到1形成插 入排序。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>#define MAXSIZE 20void initArr(int array[], int length) {
    for (int i = 0; i < length; i++) {
        array[i] = rand() % 20;
    }
}
​
//统一调用接口
void PrintfSort(int array[], int length) {
    for (int i = 0; i < length; i++) {
        printf("%d ", array[i]);
    }
    printf("\n------------------\n");
}
​
//统一调用接口
//*******************冒泡排序*********************
void InsertSort(int array[], int length) {
    int h = 4;
    while (h >= 1) {
        for (int i = h; i < length; i++) {
            for (int j = i; j >= h && array[j] < array[j - h]; j -= h/*j--*/) {
                int temp = array[j];
                array[j] = array[j - h];
                array[j - h] = temp;
            }
        }
        h /= 2;
    }
}
​
int main() {
​
    srand((unsigned int) time(NULL));
    int array[MAXSIZE];
    initArr(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
​
    InsertSort(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
    system("pause");
    return 0;
}
​

快排(越乱越快)

冒泡排序的优化版本。核心思想:使用轴,每一轮左右递归后,把轴放到中间,使得轴的左边都比轴小,轴的右边都比轴大。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>#define MAXSIZE 1000
​
void initArr(int array[], int length) {
    for (int i = 0; i < length; i++) {
        array[i] = rand() % 300;
    }
}
​
//统一调用接口
void PrintfSort(int array[], int length) {
    for (int i = 0; i < length; i++) {
        printf("%d ", array[i]);
    }
    printf("\n------------------\n");
}
​
//统一调用接口
//****************************************
void quickSort(int array[], int left, int right) {
    if (left > right)
        return;
    int i = left;
    int j = right;
    int pivot = array[i];
    while (i < j) {
        while (i < j && array[j] >= pivot)
            j--;
        array[i] = array[j];
        while (i < j && array[i] <= pivot)
            i++;
        array[j] = array[i];
​
    }
    array[i] = pivot;
    quickSort(array, left, i - 1);
    quickSort(array, i + 1, right);
}
​
int main() {
​
    srand((unsigned int) time(NULL));
    int array[MAXSIZE];
    initArr(array, MAXSIZE);
    PrintfSort(array, MAXSIZE);
    quickSort(array, 0, MAXSIZE - 1);
    PrintfSort(array, MAXSIZE);
​
    system("pause");
    return 0;
}
​

归并排序

基于分而治之的思想。拿两个已经有序的序列重新组合成一个新的有序序列