数据结构-数组排序(冒泡 选择 插入 归并(合并有序数组))-C语言

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cyzyfs/article/details/78150639
  • 冒泡排序
  • 选择排序
  • 插入排序
  • 归并排序(合并有序数组)
//
// Created by 范兴国 on 2017/9/24.
//
/*
 *
 * 数组排序 冒泡排序 选择排序 插入排序 二路归并合并数组
 *
 *
 * */
#include <stdio.h>
#include <stdlib.h>
#define N 10
main(){
    int arr[N];
    int temp;
    for(int i=0;i<N;i++){
        arr[i]=rand()%10;
    }
    printf("原内容:");
    for (int k = 0; k <N ; k++) {
        printf("%d\t",arr[k]);
    }
    printf("\n");
    printf("排序后:\n");

    /*
     * 冒泡排序
     * 轮数:数组长度-1
     * 第i论次数:数组长度-i-1
     * 调用 j j+1
     *
     * */
    /*
    时间复杂度分析:
    Comparison and swap require time that is bounded by a constant c.

    There are two nested loops in (the standard) Bubble Sort.

    The outer loop runs for exactly N iterations.
    But the inner loop runs get shorter and shorter:
    When i=0, (N−1) iterations (of comparisons and possibly swaps),
    When i=1, (N−2) iterations,
    ...,
    When i=(N−2), 1 iteration,
    When i=(N−1), 0 iteration.
    Thus, the total number of iterations = (N−1)+(N−2)+...+1+0 = N*(N−1)/2
    Total time = c*N*(N−1)/2 = O(N^2)

     */
//    for(int i=0;i<N-1;i++){
//        for (int j = 0; j <N-i-1; j++) {
//            if(arr[j]>arr[j+1]){
//                temp=arr[j];
//                arr[j]=arr[j+1];
//                arr[j+1]=temp;
//            }
//        }
//    }

    /*

     选择排序

     步骤:(找到最小的元素,并前面的交换)
        1.Find the position of the smallest item X, in the range of [L...N−1],
        2.Swap X with the L-th item,
        3.Increase the lower-bound L by 1 and repeat Step 1 until L = N-2.
    时间复杂度:
        Total: O(N2) — To be precise, it is similar to Bubble Sort analysis.
     * */
//    int min=arr[0];
//    int index;
//    for(int i=0;i<N-1;i++){
//        min=arr[i+1];
//        index=i+1;
//        for (int j = i+1; j < N; j++) {
//            if(min>arr[j]){
//                min=arr[j];
//                index=j;
//            }
//        }
//        temp=arr[i];
//        arr[i]=arr[index];
//        arr[index]=temp;
//    }


    /*
     *
     * 插入排序
     *
     * 方式一:
     *步骤:1->N遍历,从头开始比较,若比之大(或小)则交换
     *
     * */
//    for(int i=1;i<N;i++){
//        for (int j = 0; j < i; j++) {
//            if(arr[i]<arr[j]){
//                temp=arr[i];
//                arr[i]=arr[j];
//                arr[j]=temp;
//            }
//        }
//    }

    /*
     *
     * 方式二:后插入
     * cp: 备份第i个
     * i从后往前移
     *
     * 时间复杂度:
     * Thus, the best-case time is O(N × 1) = O(N) and the worst-case time is O(N × N) = O(N2).
     *
     * */
//    int cp;
//    for(int i=1;i<N;i++){
//        cp=arr[i];
//        for(int j=i-1;j>=0&&arr[j]>cp;j--){
//            arr[j+1]=arr[j];
//            arr[j]=cp;
//        }
//    }


    /*
     *
     * 二路归并 之 有序数组合并
     *
     * a={11,13,15,17,19}
     * b={2,4,6,8,20}
     * c=a+b={11,13,15,17,19,2,4,6,8,20}
     * 对c数组进行排序
     *
     * 时间复杂度:扫描的各个数组都只有1次,故为O(N)
     *
     *
     * */

//    int c[10]={2,4,6,8,20,11,13,15,17,19};
//
//    //准备工作
//    int L=0,R=5;//左起始点  右起始点
//    int RightEnd=9;//右终止点
//    int LeftEnd=R-1;//左终止点
//    int NumElements=RightEnd-L-1;//元素总数
//    int Tmp;//临时数组指针
//    int TmpA[10];//临时数组
//
//    //合并
//    while(L<=LeftEnd&&R<=RightEnd){
//        if (c[L]<=c[R]) TmpA[Tmp++]=c[L++];
//        else TmpA[Tmp++]=c[R++];
//    }
//    while (L<=LeftEnd){
//        TmpA[Tmp++]=c[L++];
//    }
//    while (R<=LeftEnd){
//        TmpA[Tmp++]=c[R++];
//    }
//
//    //TmpA-->c
//    for(int i=0;i<NumElements;i++,RightEnd--){
//        c[RightEnd]=TmpA[RightEnd];
//    }
//
//    //显示结果
//    printf("归并排序结果:");
//    for(int i=0;i<10;i++){
//        printf("%d\t",c[i]);
//    }


    printf("排序后结果:");
    for (int k = 0; k <N ; k++) {
        printf("%d\t",arr[k]);
    }

}

猜你喜欢

转载自blog.csdn.net/cyzyfs/article/details/78150639