排序算法集合 -1

1. 冒泡排序 Bubble Sort

冒泡排序的执行时间和空间复杂度: 平均情况与最差情况为O(n2), 存储空间为O(1)。

/*************************************************************************
    > File Name: bubbleSort.c
    > Author: 
    > Mail: 
    > Created Time: 2016年09月04日 星期日 11时52分20秒
 ************************************************************************/

#include<stdio.h>

void bubbleSort(int *a, int n);
void testSort(int *a, int len, void (*sort)(int*, int));

int main()
{
    int a[] = {2, 1, 5, 6, 9, 6, 3};
    testSort(a, sizeof(a) / sizeof(int), bubbleSort);
    return 0;
}

void bubbleSort(int *a, int n) {
    if (NULL == a || n <= 1)
        return;
    int i, j, temp;

    for (i = 0; i < n - 1; i++) { // n个数需要n-1趟
        for (j = 1; j < n-i; j++) { // 从最开始到最后一位未归位
            if (a[j] < a[j-1]) {
                temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;            
            }    
        }    
    }
}

void testSort(int *a, int len, void (*sort)(int*, int)) {
    int i;
    printf("Sort Before: ");
    for (i=0; i < len; i++) { 
        printf("%d ", a[i]);
    }
    printf("\n");

    sort(a, len);

    printf("Sort After: ");
    for (i=0; i < len ; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

输出结果:

Sort Before: 2 1 5 6 9 6 3 
Sort After: 1 2 3 5 6 6 9 

2. 选择排序 Selection Sort

选择排序的执行时间和空间复杂度: 平均情况与最差情况为O(n2), 存储空间为O(1)。

简单而低效, 线性逐一扫描数组元素,从中选出最小的元素,将它移到最前面(也就是与最前面的元素交换)。然后再次线性扫描数组,找到第二小的元素,并且移到前面。如此反复,直到全部元素各归其位。

有一些优势,最多只需要(n-1)次交换,在数据元素的移动操作与比较操作相比开销更大的情况下,选择排序可能比其他算法更好。选择排序是一个原地排序算法,典型的排序算法不是稳定的。

void bubbleSort(int *a, int n);
void selectionSort(int *a, int n);
void testSort(int *a, int len, void (*sort)(int*, int));

int main() {
    int a[] = {2, 1, 5, 6, 9, 6, 3};
    //testSort(a, sizeof(a) / sizeof(int), bubbleSort);
    testSort(a, sizeof(a) / sizeof(int), selectionSort);
    return 0;
}

void selectionSort(int *a, int n) {
    if (NULL==a || n<=1)
        return;

    int i, j, min;
    for (i=0; i<n-1; i++) {
        min=i; // find the smallest from index i;
        for (j = i+1; j<n; j++) {
            if (a[j] < a[min])
            min = j;           
        }

        if (min != i) { // swap
            int temp = a[min];
            a[min] = a[i];
            a[i] = temp;
        }      
    }
}

// 另一种简化规整的实现

// Method 2
void swap(int *a, int index1, int index2) {
    if (index1 != index2) {
        int tmp = a[index1];
        a[index1] = a[index2];
        a[index2] = tmp;
    }
}

// Find the position of minimum value at the start from
int findMinimum(int *a, int start, int len) {
    int minPos = start;
    for (int i=start+1; i<len; i++) {
        if (a[i] < a[minPos])
            minPos = i;
    }

    return start;
}

// Start a subset of the array starting at the given index
void selectionSort(int *a, int start, int len) {
    if (start < len-1) {
        swap(a, start, findMinimum(a, start, len));
        selectionSort(a, start+1, len);
    }
}

3. 归并排序 Merge Sort

归并排序的执行时间和空间复杂度: 平均情况与最差情况为O(nlog(n)), 存储空间看情况而定。

// Lpos = start of left half, Rpos = start of right half
void merge(int a[], int tmpArray[], int Lpos, int Rpos, int RightEnd) {
    int LeftEnd = Rpos - 1;
    int NumElement = RightEnd - Lpos + 1;
    int TmpPos = Lpos;
    int i;

    while (Lpos<=LeftEnd && Rpos<=RightEnd) {
        if (a[Lpos]<=a[Rpos])
            tmpArray[TmpPos++] = a[Lpos++];
        else
            tmpArray[TmpPos++] = a[Rpos++];
    }

    while (Lpos<=LeftEnd)
        tmpArray[TmpPos++] = a[Lpos++];
    while (Rpos<=RightEnd)
        tmpArray[TmpPos++] = a[Rpos++];

    // copy tmpArray back
    for (i=0; i<NumElement; i++, RightEnd--)
        a[RightEnd] = tmpArray[RightEnd];
}

void mergeSortSUB(int *a, int tmpArray[], int Left, int Right) {
    int Center;
    if (Left < Right) {
        Center = (Left + Right) / 2;
        mergeSortSUB(a, tmpArray, Left, Center);
        mergeSortSUB(a, tmpArray, Center+1, Right);
        merge(a, tmpArray, Left, Center+1, Right);
    }
}

void mergeSort(int *a, int len) {
    int* tmpArray = (int*)malloc(sizeof(int) * len);

    if (NULL != tmpArray) {
        mergeSortSUB(a, tmpArray, 0, len -1);
        free(tmpArray);
    } else 
        printf("alloc error\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值