数据结构之排序算法(一)

本文深入探讨数据结构中的排序算法,重点讲解了插入排序的三种实现:直接插入排序、折半插入排序和希尔排序。希尔排序通过缩小增量提高效率,优于直接插入排序。文章还提供了相关代码实现。

这里向大家介绍以下数据结构中的排序算法之插入排序,插入排序建立在一个已经排好序的子集上,基本思想是:每一步将下一个待排序的元素插入到一个已经排好序的子集中,直到结束。在此介绍三种插入排序:直接插入排序、折半插入排序、希尔排序

1、直接插入排序
这是最基本的插入排序方法,这里就不详细介绍了,直接上代码吧

/**
 * insert sort
 * @param a :array to sort
 * @param n :length of array
 */
void insertSort(int a[], int n) {
    int i, j, temp;
    for (i = 0; i < n; i++) {
        // as a guard
        temp = a[i];
        // find location and move other elements back
        for (j = i - 1; j >= 0 && temp < a[j]; --j)
            a[j + 1] = a[j];
        // insert into correct place
        a[j + 1] = temp;
    }
}

2、折半插入排序
类比与折半查找的思想,可以将折半查找用于查找元素要插入的位置,它的好处是减少了关键字的比较次数,但并没有改变移动元素的时间耗费,因此时间复杂度仍为O(n2)

/**
 * binary insert sort
 * @param a
 * @param n
 */
void binaryInsertSort(int a[], int n) {
    int temp, low, high, mid;
    for (int i = 0; i < n; ++i) {
        temp = a[i];
        low = 0;
        high = i - 1;
        while (low <= high) {
            mid = (low + high) / 2;
            if (temp < a[mid])
                high = mid - 1;
            else
                low = mid + 1;
        }
        for (int j = i - 1; j >= low; --j)
            a[j + 1] = a[j];
        a[low] = temp;
    }
}

3、希尔排序
希尔排序也叫做缩小增量排序,主要思想是将待排元素分成若干个小的子序列,然后对每个子序列进行排序。希尔排序的效率比直接插入排序的效率高的多

void shellSort(int a[], int n) {
    int group = n / 2;
    int temp, j;

    while (group >= 1) {
        for (int i = group; i < n; ++i) {
            temp = a[i];
            j = i - group;// the element before i in same group
            while (j >= 0 && a[j] > temp) {
                a[j + group] = a[j];// move back in the same group
                j = j - group;
            }
            a[j + group] = temp;// store data
        }
        group /= 2;// reduce the count of one group;
    }
}

4、附上完整代码
包括一个生成的随机数组的函数,打印数组的函数

//
// Created by qcq on 2017/3/20.
//

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>

/**
 * generate a random array for sorting
 * @param arr : store the result of array
 * @param n : the length of array
 * @param min : min number the random generate
 * @param max : max number the random generate
 * @return
 */
int generateRandomArray(int arr[], int n, int min, int max) {
    int i, j, flag;
    srand(time(NULL));
    if (max - min + 1 < n) // range of the max and min can not provide enough number
        return 0;
    for (i = 0; i < n; ++i) {
        do {
            arr[i] = (max - min + 1) * rand() / (RAND_MAX + 1 + min);

            flag = 0;

            // make sure do not have same number in the array
            for (j = 0; j < i; j++)
                if (arr[i] == arr[j])
                    flag = 1;
        } while (flag);
    }
    return 1;
}

/**
 * insert sort
 * @param a :array to sort
 * @param n :length of array
 */
void insertSort(int a[], int n) {
    int i, j, temp;
    for (i = 0; i < n; i++) {
        // as a guard
        temp = a[i];
        // find location and move other elements back
        for (j = i - 1; j >= 0 && temp < a[j]; --j)
            a[j + 1] = a[j];
        // insert into correct place
        a[j + 1] = temp;
    }
}

/**
 * binary insert sort
 * @param a
 * @param n
 */
void binaryInsertSort(int a[], int n) {
    int temp, low, high, mid;
    for (int i = 0; i < n; ++i) {
        temp = a[i];
        low = 0;
        high = i - 1;
        while (low <= high) {
            mid = (low + high) / 2;
            if (temp < a[mid])
                high = mid - 1;
            else
                low = mid + 1;
        }
        for (int j = i - 1; j >= low; --j)
            a[j + 1] = a[j];
        a[low] = temp;
    }
}

void shellSort(int a[], int n) {
    int group = n / 2;

    int temp, j;
    while (group >= 1) {
        for (int i = group; i < n; ++i) {
            temp = a[i];
            j = i - group;// 一组中的前一个序号
            while (j >= 0 && a[j] > temp) {
                a[j + group] = a[j];// move back in the same group
                j = j - group;
            }
            a[j + group] = temp;// store data
        }
        group /= 2;// reduce the count of one group;
    }
}

void printArray(int a[], int n) {
    for (int i = 0; i < n; ++i) {
        printf("%d ", a[i]);
    }
}

int main() {
    int a[10];
    for (int i = 0; i < 10; ++i) {
        a[i] = 0;
    }
    if (!generateRandomArray(a, 10, 10, 100)) {
        printf("failure to generate array");
        getch();
        return 1;
    }
    printf("data before sort:");
    printArray(a, 10);

    printf("\n");

    shellSort(a, 10);
    printf("data  after sort:");
    printArray(a, 10);

    getch();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值