排序算法集合 -1

1. 冒泡排序 Bubble Sort

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

// test_manda.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
    int a[] = {2, 1, 5, 6, 9, 6, 3};
    testSort(a, sizeof(a) / sizeof(int), budleSort);
    return 0;
}

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

    for (int i=0; i<n; i++) {
        for (int j=1; j<n-i; j++) {
            if (a[j]<a[j-1]) {
                int temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
            }
        }
    }
}

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


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

2. 选择排序 Selection Sort

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

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

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

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

    for (int i=0; i<n-1; i++) {
        int min=i; // find the smallest from index i;
        for (int 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;

    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 (int i=0; i<NumElement; i++, RightEnd--)
        a[RightEnd] = tmpArray[RightEnd];
}

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

void mergeSort(int *a, int len) {
    int tmpArray = new [sizeof(int) * len];

    if (NULL != tmpArray) {
        mergeSort(a, tmpArray, 0, len -1);
        delete []tmpArray;
    } else 
        printf("alloc error\n");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值