/*
** 常见排序算法比较
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define N 10
#define Demo 1
void BubbleSort(int arr[], int n);
void SelectSort(int arr[], int n);
void QuickSort(int arr[], int n);
void PrintArray(int arr[], int n);
void GenerateArray(int arr[], int n);
int main(int argc, char *argv[])
{
int arr[N];
GenerateArray(arr, N);
#if Demo
printf("Before the bubble sort------------------------\n");
PrintArray(arr, N);
#endif
printf("Start Bubble sort----------------------\n");
clock_t start_time1=clock(); //开始计时
BubbleSort(arr, N);
clock_t end_time1=clock(); // 结束计时
printf("Running time is: %lf ms\n", (double)(end_time1-start_time1)/CLOCKS_PER_SEC*1000); //输出运行时间
#if Demo
printf("After the bubble sort------------------------\n");
PrintArray(arr, N);
#endif
printf("-----------------------------------------------------------\n");
sleep(1000); // 单位是毫秒即千分之一秒
GenerateArray(arr, N);
#if Demo
printf("Before the selection sort------------------------\n");
PrintArray(arr, N);
#endif
printf("Start selection sort----------------------\n");
clock_t start_time2=clock(); //开始计时
SelectSort(arr, N);
clock_t end_time2=clock(); // 结束计时
printf("Running time is: %lf ms\n", (double)(end_time2-start_time2)/CLOCKS_PER_SEC*1000); //输出运行时间
#if Demo
printf("After the selection sort------------------------\n");
PrintArray(arr, N);
#endif
printf("-----------------------------------------------------------\n");
sleep(1000); // 单位是毫秒即千分之一秒
GenerateArray(arr, N);
#if Demo
printf("Before the quick sort------------------------\n");
PrintArray(arr, N);
#endif
printf("Start quick sort----------------------\n");
clock_t start_time3=clock(); //开始计时
QuickSort(arr, N);
clock_t end_time3=clock(); // 结束计时
printf("Running time is: %lf ms\n", (double)(end_time3-start_time3)/CLOCKS_PER_SEC*1000); //输出运行时间
#if Demo
printf("After the quick sort------------------------\n");
PrintArray(arr, N);
#endif
system("PAUSE");
return 0;
}
// 产生随机列表
void GenerateArray(int arr[], int n)
{
int i;
srand((unsigned)time(0));
for(i = 0; i <N; i++)
{
arr[i] = rand(); // 生成随机数 范围在0-32767之间
}
}
// 打印列表
void PrintArray(int arr[], int n)
{
int i = 0;
for(i = 0; i < n; i++)
printf("%6d", arr[i]);
printf("\n");
}
// 经典冒泡排序
void BubbleSort(int arr[], int n)
{
int i = 0, j =0;
for(i = 0; i < n; i++)
for(j = 0; j < n - 1 - i; j++)
{
if(arr[j] > arr[j + 1])
{
arr[j] = arr[j] ^ arr[j+1];
arr[j+1] = arr[j] ^ arr[j+1];
arr[j] = arr[j] ^ arr[j+1];
}
}
}
// 快速排序的递归实现
void QuickSort(int arr[], int n)
{
if(n <= 1)
return;
int i =0 , j = n - 1;
int key = arr[0];
int index = 0;
while(i < j)
{
// 从后向前搜索
while(j > i && arr[j] > key)
j--;
if(j == i)
break;
else
{
//交换 a[j] a[i]
arr[j] = arr[j] ^arr[i];
arr[i] = arr[j] ^arr[i];
arr[j] = arr[j] ^arr[i];
index = j;
}
// 从前向后搜索
while(i < j && arr[i] <key)
i++;
if(i == j)
break;
else
{
// 交换 a[i] a[j]
arr[j] = arr[j] ^arr[i];
arr[i] = arr[j] ^arr[i];
arr[j] = arr[j] ^arr[i];
index = i;
}
}
QuickSort(arr, index);
QuickSort(arr + index + 1, n - 1 - index);
}
// 选择排序
void SelectSort(int arr[], int n)
{
int i, j;
int min;
for(i = 0; i < n - 1; i++)
{
int index = 0;
min = arr[i];
for(j = i + 1; j < n; j++) //找出 i+1 - n 无序区的最小者与arr[i]交换
{
if(arr[j] < min)
{
min = arr[j];
index = j;
}
}
if(index != 0) //表明无序区有比arr[i]小的元素
{
arr[i] = arr[i]^arr[index];
arr[index] = arr[i]^arr[index];
arr[i] = arr[i]^arr[index];
}
}
}
转载:http://www.cnblogs.com/herbert/archive/2011/01/20/1940392.html