• C语言排序代码汇总测试



    注意:粘贴程序段时记得去掉标题
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int a[2000] ;
    int temp[2000];
    int *b = a;
    void output(int *);

    冒泡排序

    //-----------------冒泡排序--------------------
    void bubblesort()
    {
    int i, j,flag,temp;
    for (i = 0; i <= 1999; i++)
    {
    flag = 0;
    for (j = 0; j <= 1999 - i; j++)
    {
    if (b[j] > b[j + 1])
    {
    temp = b[j];
    b[j] = b[j + 1];
    b[j + 1] = temp;
    flag = 1;
    }
    }
    if (!flag) break;
    }
    }

    快速排序1

    //-----------------快速排序1--------------------

    void quicksort1(int left,int right)
    {
    int i, j, t, temp;
    if (left > right)
    return;
    i = left;
    j = right;
    temp = a[left];

        while (i!=j)
        {
                while (i < j&&a[j] >= temp)
                        --j;
                while (i < j&&a <= temp)
                        ++i;
                if (i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    }

    快速排序2

    //-----------------快速排序2--------------------

    int part(int left, int right)
    {
    int temp = a[left];
    int pivot = a[left];
    while (left < right)
    {
    while (left < right&&a[right] >= pivot) right–;
    a[left] = a[right];

                while (right > left&&a[left] <= pivot) left++;
                a[right] = a[left];
        }
    
        a[left] = temp;
        return left;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }
    void quicksort(int left, int right)
    {
    if (left < right)
    {
    int flag = part(left, right);
    quicksort(left, flag - 1);
    quicksort(flag+1,right);
    }

    }

    直接插入排序

    //-----------------直接插入排序--------------------
    void insertsort()
    {
    int i, j, temp;

        for (i = 1; i < 2000; i++)//a[0]作监视哨
        {
                temp = a; 
                j = i - 1;
                
                while (a[j]>temp && j >= 0)
                {
                        a[j + 1] = a[j];//后移
                        --j;
                }
    
                a[j + 1] = temp;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    }

    希尔排序

    //-----------------希尔排序--------------------
    void shellsort()
    {
    int i, j,temp,k;
    int d;//间隔

        for (d = 2000 / 2; d > 0;d/=2)
        {
                //直接插入排序
                for (k = 0; k < d; ++k)
                {
                        for (i = k+d; i < 2000; i += d)
                        {
                                temp = a;
                                j = i - d;
    
                                while (a[j]>temp && j >= k)
                                {
                                        a[j + d] = a[j];
                                        j -= d;
                                }
                                a[j + d] = temp;
                        }
                }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    }

    堆排序

    //-----------------堆排序--------------------
    void swap(int& x, int& y)
    {
    int temp = x;
    x = y;
    y = temp;
    }

    void HeapAdjust(int i,int n)
    {
    int j, temp;
    temp = a;
    j = 2 * i + 1;

        while (j=a[j])        break;                        
                
                a = a[j];
                i = j;
                j = 2 * i + 1;
        }
    
        a=temp;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    }

    void Heapsort()
    {
    int i;

        for (i = 2000/ 2-1; i >= 0; i--)
        {
                HeapAdjust(i,2000);
        }
    
        for (i = 1999; i > 0; i--)
        {
                swap(a[0],a);
                HeapAdjust(0,i-1);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    }

    归并排序

    //-----------------归并排序--------------------
    void merge(int*left,int left_length,int *right,int right_length)
    {
    int i, j, k,m;
    i = j = k = 0;

        while (i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    }
    void mergesort(int* x, int n)
    {
    if (n >= 2)
    {
    int *left = x;
    int left_length = n / 2;

                int *right = x + left_length;
                int right_length = n - left_length;
    
                mergesort(left, left_length);
                mergesort(right, right_length);
    
                merge(left, left_length, right, right_length);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    }

    //-----------------初始化------------------
    void init()
    {
    srand(((unsigned)time(0)));

        for (int i = 0; i < 2000; i++)
                a = rand()%10000;
    
    • 1
    • 2

    }

    //-----------------输出--------------------
    void output(int *p)
    {
    for (int i = 0; i < 2000; i++)
    cout < cout << endl;
    }
    int main()
    {
    double start, end;
    init();
    cout <<“排序前:” << endl;
    output(a);

    //-----------------冒泡排序--------------------
    cout << “冒泡排序后:” << endl;
    start = clock();
    bubblesort();
    end = clock();
    output(b);
    cout << “冒泡排序运行时间(ms):”
    << (double)(end - start) / CLOCKS_PER_SEC * 1000<

    //-----------------快速排序--------------------
    cout << “快速排序后:” << endl;
    start = clock();
    quicksort(0,1999);
    end = clock();
    output(a);
    cout << “快速排序运行时间(ms):”
    << (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;

    //-----------------直接插入排序-----------------
    cout << “直接插入排序后:” << endl;
    start = clock();
    insertsort();
    end = clock();
    output(a);
    cout << “直接插入排序运行时间(ms):”
    << (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;

    //-----------------希尔排序--------------------
    init();
    cout << “希尔排序后:” << endl;
    start = clock();
    shellsort();
    end = clock();
    output(a);
    cout << “希尔排序运行时间(ms):”
    << (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;

    //-----------------堆排序--------------------
    init();
    cout << “堆排序后:” << endl;
    start = clock();
    Heapsort();
    end = clock();
    output(a);
    cout << “堆排序运行时间(ms):”
    << (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;

    //-----------------归并排序--------------------
    init();
    cout << “归并排序后:” << endl;
    start = clock();
    mergesort(a,2000);
    end = clock();
    output(a);
    cout << “归并排序运行时间(ms):”
    << (double)(end - start) / CLOCKS_PER_SEC * 1000 << endl;

        system("pause");
        return 0;
    
    • 1
    • 2

    }
    //
    #include
    //void sort(int arr[],int length);
    void maosort(int arr[],int length);
    int main()
    {
    int arr[] = {12,45,67,123,456,5,43,22,11,-9};
    int length = sizeof(arr)/sizeof(arr[0]);
    //sort(arr,length);
    maosort(arr,length);
    int k;
    for(k = 0;k < length;k++){
    printf(“%d “,arr[k]);
    }
    printf(”\n”);
    return 0;
    }

    选择排序

    /*
    选择排序
    /
    void sort(int arr[],int length)
    {
    int i,j;
    for(i = 0;i < length-1;i++)
    {
    for(j = i+1;j < length;j++){
    if(arr[i]>arr[j]){
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    }
    }
    }
    }
    /

    冒泡排序
    */
    void maosort(int arr[],int length)
    {
    int i,j;
    for(i = 0;i < length-1;i++)
    {
    for(j = 0;j < length-i-1;j++){
    if(arr[j]>arr[j+1]){
    int temp = arr[j];
    arr[j] = arr[j+1];
    arr[j+1] = temp;
    }
    }
    }
    }

  • 相关阅读:
    责任链模式(设计模式)
    DM8数据守护集群安装部署_手动切换
    Go:定 n 对括号,来生成格式正确的括号的所有组合(附完整源码)
    蓝桥杯刷题(十一)
    tcpdump抓包实现过程
    Java扩展Nginx之五:五大handler(系列最核心)
    [Dubbo3.0.8源码解析系列]-25-为调用器对象增加容错和过滤器等功能
    基于Restful的WebService
    Spring Security 登录获取用户信息流程分析
    Java:使用StopWatch统计程序执行耗时
  • 原文地址:https://blog.csdn.net/xunxue1523/article/details/126622663