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

来源:互联网 发布:匹克模考tpo软件 编辑:程序博客网 时间:2024/06/11 04:58

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

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;}
0 0
原创粉丝点击