1. 冒泡排序 Bubble Sort
冒泡排序的执行时间和空间复杂度: 平均情况与最差情况为O(n
2), 存储空间为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)次交换,在数据元素的移动操作与比较操作相比开销更大的情况下,选择排序可能比其他算法更好。选择排序是一个原地排序算法,典型的排序算法不是稳定的。
-
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");
-
}
阅读(1267) | 评论(0) | 转发(0) |