排序算法合集

来源:互联网 发布:淘宝有打钢珠的复合弓 编辑:程序博客网 时间:2024/04/28 06:02


快速排序


#define DeBUG#include <iostream>#include <algorithm>#include <time.h>using namespace std ;#define zero {0}int partition(int A[], int p, int r){    int x = A[r];    int i = p - 1;    int t;    for (int j = p; j < r; j++)    {        if (A[j] <= x)        {            i++;            // swap(A[i],A[j]);            t = A[i];            A[i] = A[j];            A[j] = t;        }    }    // swap(A[i+1],A[r]);    t = A[i + 1];    A[i + 1] = A[r];    A[r] = t;    return i + 1;}void quick_sort(int A[], int p, int r){    int q;    if (p < r)    {        q = partition(A, p, r);        quick_sort(A, p, q - 1);        quick_sort(A, q + 1, r);    }}void MySort(int *af, int *ae){    int l = ae - af;    quick_sort(af, 0, l - 1);}int main(){#ifdef DeBUG    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif    int A[200005] = {9, 2, 3, 4, 1, 3, 4, 3, 4, 0};    int B[200005] = zero;    int i = 0;    MySort(A, A + 10);    printf("MySort test:\n");    for (int i = 0; i < 10; i++)    {        printf("%d ", A[i]);    }    printf("\n");    printf("200000 data test:\n");    while (scanf("%d", &A[i]) + 1)    {        B[i] = A[i];        i++;    }    double now = (double)(1000 * clock() / CLOCKS_PER_SEC);    MySort(A, A + 200000);    printf("my sort used time=");    printf("%.0lf ms\n", (double)(1000 * clock() / CLOCKS_PER_SEC) - now);    now = (double)(1000 * clock() / CLOCKS_PER_SEC);    sort(B, B + 200000);    printf("STL sort used time=");    printf("%.0lf ms\n", (double)(1000 * clock() / CLOCKS_PER_SEC) - now);    return 0;}


插入排序

#include <iostream>#include <cstdio>using namespace std;void Insert(int *small, int *big)   //递归插入{    if (*small <= *big)        return;    else    {        swap(*small, *big);        Insert(small - 1, small);    }}void InsertSort(int a[], int n){    for (int i = 1; i < n; i++)    {        Insert(&a[i - 1], &a[i]);    }}int main(){    int b[10] = {1, 4, 8, 7, 6, 2, 4, 5, 4, 3};    int a[10] = {1, 4, 8, 7, 6, 2, 4, 5, 4, 3};    int n = 10;    int key;    printf("普通插入排序:");    for (int j = 1; j < n; j++)    {        key = a[j];        int i = j - 1;        while (i >= 0 && a[i] > key)        {            a[i + 1] = a[i];            i--;        }        a[i + 1] = key;    }    for (int i = 0; i < n; i++)        printf("%d ", a[i]);    printf("\n");    printf("递归插入排序:");    memcpy(a, b, sizeof(b));    InsertSort(a, n);    for (int i = 0; i < n; i++)        printf("%d ", a[i]);    return 0;}


归并排序


#define DeBUG#include <iostream>#include <time.h>#include <algorithm>using namespace std ;#define zero {0}void mergeArray(int a[], int f, int m, int e){    int temp[e - f + 1];    int i = f;    int j = m + 1;    int ti = 0;    while (i <= m && j <= e)    {        if (a[i] < a[j])        {            temp[ti++] = a[i++];        }        else        {            temp[ti++] = a[j++];        }    }    while (i <= m)    {        temp[ti++] = a[i++];    }    while (j <= e)    {        temp[ti++] = a[j++];    }    ti = 0;    for (i = f; i <= e; i++)    {        a[i] = temp[ti++];    }}void mergeSort(int a[], int front, int end){    if (front < end)    {        int mid = (front + end) / 2;        mergeSort(a, front, mid);        mergeSort(a, mid + 1, end);        mergeArray(a, front, mid, end);    }}template <typename T>bool MergeSort(T *first, T *end){    int n = (end - first);    mergeSort(first, 0, n - 1);    return true;}int main(){#ifdef DeBUG    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif    int a[100] = {1, 3, 5, 9, 2, 1, 3, 4, 0, -1, 3, 5};    printf("测试12个元素排列:\n");    MergeSort(a,a+12);    // sort(a,a+12);    for (int i = 0; i < 12; i++)    {        printf("%d ", a[i]);    }    printf("\n");    printf("测试20万数据用时:\n");    int test[200005] = zero;    int k = 0;    while (scanf("%d", &test[k]) !=EOF)    {        k++;    }    double nowtime = (double)(1000 * clock() / CLOCKS_PER_SEC);    // sort(test, test + 200000);     MergeSort(test, test + 200000);    printf("Time used = %.0lf ms\n", (double)(1000 * clock() / CLOCKS_PER_SEC) - nowtime);     return 0;}



0 0