Chinaunix首页 | 论坛 | 博客
  • 博客访问: 98371
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-17 11:22
文章分类

全部博文(22)

文章存档

2015年(6)

2014年(16)

我的朋友

分类: C/C++

2014-11-16 22:10:12

原文地址:排序算法集合 -1 作者:mandagod

1. 冒泡排序 Bubble Sort
    冒泡排序的执行时间和空间复杂度: 平均情况与最差情况为O(n2), 存储空间为O(1)。
  1. // test_manda.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"

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

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

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

  15.     for (int i=0; i<n; i++) {
  16.         for (int j=1; j<n-i; j++) {
  17.             if (a[j]<a[j-1]) {
  18.                 int temp = a[j];
  19.                 a[j] = a[j-1];
  20.                 a[j-1] = temp;
  21.             }
  22.         }
  23.     }
  24. }

  25. 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)次交换,在数据元素的移动操作与比较操作相比开销更大的情况下,选择排序可能比其他算法更好。选择排序是一个原地排序算法,典型的排序算法不是稳定的。

点击(此处)折叠或打开

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

  4.     for (int i=0; i<n-1; i++) {
  5.         int min=i; // find the smallest from index i;
  6.         for (int j=i+1; j<n; j++) {
  7.             if (a[j] < a[min])
  8.                 min = j;
  9.         }

  10.         if (min != i) { // swap
  11.             int temp = a[min];
  12.             a[min] = a[i];
  13.             a[i] = temp;
  14.         }
  15.     }
  16. }

点击(此处)折叠或打开

  1. // Method 2
  2. void swap(int *a, int index1, int index2) {
  3.     if (index1 != index2) {
  4.         int tmp = a[index1];
  5.         a[index1] = a[index2];
  6.         a[index2] = tmp;
  7.     }
  8. }

  9. // Find the position of minimum value at the start from
  10. int findMinimum(int *a, int start, int len) {
  11.     int minPos = start;
  12.     for (int i=start+1; i<len; i++) {
  13.         if (a[i] < a[minPos])
  14.             minPos = i;
  15.     }

  16.     return start;
  17. }

  18. // Start a subset of the array starting at the given index
  19. void selectionSort(int *a, int start, int len) {
  20.     if (start < len-1) {
  21.         swap(a, start, findMinimum(a, start, len));
  22.         selectionSort(a, start+1, len);
  23.     }
  24. }



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

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

  6.     while (Lpos<=LeftEnd && Rpos<=RightEnd) {
  7.         if (a[Lpos]<=a[Rpos])
  8.             tmpArray[TmpPos++] = a[Lpos++];
  9.         else
  10.             tmpArray[TmpPos++] = a[Rpos++];
  11.     }

  12.     while (Lpos<=LeftEnd)
  13.         tmpArray[TmpPos++] = a[Lpos++];
  14.     while (Rpos<=RightEnd)
  15.         tmpArray[TmpPos++] = a[Rpos++];

  16.     // copy tmpArray back
  17.     for (int i=0; i<NumElement; i++, RightEnd--)
  18.         a[RightEnd] = tmpArray[RightEnd];
  19. }

  20. void mergeSort(int *a, int tmpArray[], int Left, int Right) {
  21.     if (Left < Right) {
  22.         int Center = (Left + Right) / 2;
  23.         mergeSort(a, tmpArray, Left, Center);
  24.         mergeSort(a, tmpArray, Center+1, Right);
  25.         merge(a, tmpArray, Left, Center+1, Right);
  26.     }
  27. }

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

  30.     if (NULL != tmpArray) {
  31.         mergeSort(a, tmpArray, 0, len -1);
  32.         delete []tmpArray;
  33.     } else
  34.         printf("alloc error\n");
  35. }
阅读(1230) | 评论(0) | 转发(0) |
0

上一篇:BIOS与CMOS区别

下一篇:排序算法集合 -2

给主人留下些什么吧!~~