C 语言实现常见的五种数组排序算法

五种排序算法

  1. 冒泡排序
    unsigned int length = 10;
    for (i = 0; i < length; i++)
    {
        for (int j = i; j < length; j++)
        {
            if (arr[i] > arr[j])
            {
                int temp;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
  1. 选择排序
    unsigned int length = 10;
    for (i=0;i<length;i++){
        int lower = arr[i];
        int location;
        for (int j=i;j<length;j++){
            if (lower>arr[j]){
                lower = arr[j];
                location = j;
            }
        }
        if (lower != arr[i]){
            int temp;
            temp = arr[i];
            arr[i] = lower;
            arr[location] = temp;
        }
    }
  1. 插入排序
    for (i=1;i<length;i++){
        int temp = arr[i];
        int j = i - 1;
        // 是否大于前面一个数
        // 是否到头了

        while (temp < arr[j] && j >= 0){
            // 向前移动一位
            arr[j+1] = arr[j];
            arr[j] = temp;
            j --;
        }
    }
  1. 希尔排序
  2. 快速排序

猜你喜欢

转载自blog.csdn.net/weixin_42290927/article/details/107845419