复杂度
1,冒泡排序
void bubble_sort(int* arr, int len)
{
int t;
for (int i = 0; i <= len-2; i++)//轮数
{
for (int j = 0; j <= len-2-i; j++)//每一轮j交换范围
{
if (arr[j] > arr[j + 1])
{
t=arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
travel(arr, NUM);
}
/*
具体实现:
before:31 74 56 89 24 86 12 64 20 11//初始
before:31 56 74 24 86 12 64 20 11 89//第一轮
before:31 56 24 74 12 64 20 11 86 89
before:31 24 56 12 64 20 11 74 86 89
before:24 31 12 56 20 11 64 74 86 89
before:24 12 31 20 11 56 64 74 86 89
before:12 24 20 11 31 56 64 74 86 89
before:12 20 11 24 31 56 64 74 86 89
before:12 11 20 24 31 56 64 74 86 89
before:11 12 20 24 31 56 64 74 86 89
after: 11 12 20 24 31 56 64 74 86 89//结束
*/
}
2,选择排序
void select_sort(int* arr, int len)
{
int idx;//当前下标
int t;
for (int i = 0; i < len - 1; i++)//len-1轮
{
//范围是 i ~ len-1
//从数据范围中找出最小的和arr[i]交换
idx = i;
for (int j = i; j < len; j++)//找最小 arr[idx]
{
idx = (arr[idx] < arr[j]) ? idx : j;
}
t = arr[idx];
arr[idx] = arr[i];
arr[i] = t;
travel(arr, NUM);
}
/*
实际效果:
before:31 74 56 89 24 86 12 64 20 11//起始
before:11 74 56 89 24 86 12 64 20 31//第一轮
before:11 12 56 89 24 86 74 64 20 31
before:11 12 20 89 24 86 74 64 56 31
before:11 12 20 24 89 86 74 64 56 31
before:11 12 20 24 31 86 74 64 56 89
before:11 12 20 24 31 56 74 64 86 89
before:11 12 20 24 31 56 64 74 86 89
before:11 12 20 24 31 56 64 74 86 89
before:11 12 20 24 31 56 64 74 86 89
after: 11 12 20 24 31 56 64 74 86 89//结束
*/
}
3,插入排序
void insert_sort(int* arr, int len)
{
int temp;
int j;
for (int i = 1; i < len; i++)//待插数据arr[i] i的范围在1~len-1
{
temp = arr[i];//先保存一下待插数据
j = i - 1;//从待插数据的前一个开始
//while (1)//写法1
//{
// if (j < 0)break;
// if (arr[j] > temp)
// {
// arr[j + 1] = arr[j];//往后覆盖
// }
// else
// {
// break;
// }
// j--;
//}
//写法2
//while (j >= 0 && arr[j] > temp)
//{
// arr[j + 1] = arr[j];
// j--;
//}
//写法3
for (j = i - 1; j >= 0 && arr[j] > temp; j--)
{
arr[j + 1] = arr[j];
}
//待插数据 插入
arr[j + 1] = temp;
travel(arr, NUM);
}
/*
实际效果:
before:31 74 56 89 24 86 12 64 20 11//初始
before:31 74 56 89 24 86 12 64 20 11//第一轮
before:31 56 74 89 24 86 12 64 20 11
before:31 56 74 89 24 86 12 64 20 11
before:24 31 56 74 89 86 12 64 20 11
before:24 31 56 74 86 89 12 64 20 11
before:12 24 31 56 74 86 89 64 20 11
before:12 24 31 56 64 74 86 89 20 11
before:12 20 24 31 56 64 74 86 89 11
before:11 12 20 24 31 56 64 74 86 89
after: 11 12 20 24 31 56 64 74 86 89//结束
*/
}
4,希尔排序
void shell_sort(int* arr, int len)
{
int step = len >> 1;//步长除2
int temp;//临时保存待插数据
int j;//下标
while (step)//按照步长变化来循环
{
//每次都按照步长分组,组内做插入排序
for (int i = step; i < len; i++)//待插数据arr[i] i的范围在1~len-1
{
temp = arr[i];//先保存一下待插数据
for (j = i - step; j >= 0 && arr[j] > temp; j-=step)
{
arr[j + step] = arr[j];
}
//待插数据 插入
arr[j + step] = temp;
}
step >>= 1;
travel(arr, NUM);
}
/*
实际效果:
before:31 74 56 89 24 86 12 64 20 11//起始
before:31 12 56 20 11 86 74 64 89 24//第一轮
before:11 12 31 20 56 24 74 64 89 86
before:11 12 20 24 31 56 64 74 86 89
after: 11 12 20 24 31 56 64 74 86 89//结束
*/
}
1~4排序算法比较:
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#define NUM 100000
//初始化数组
void init(int* arr, int len);
//冒泡排序
void bubble_sort(int* arr, int len);
//选择排序
void select_sort(int* arr, int len);
//插入排序
void insert_sort(int* arr, int len);
//希尔排序
void shell_sort(int* arr, int len);
int main()
{
int arr[NUM];
ULONGLONG oldTime, newTime;
srand((unsigned)time(NULL));
init(arr, NUM);
//获取排序前当前时间
oldTime = GetTickCount64();
bubble_sort(arr, NUM);
//获取排序后当前时间
newTime = GetTickCount64();
printf("bubble_sort:%llu\n",newTime-oldTime);
init(arr, NUM);
//获取排序前当前时间
oldTime = GetTickCount64();
select_sort(arr, NUM);
//获取排序后当前时间
newTime = GetTickCount64();
printf("select_sort:%llu\n", newTime - oldTime);
init(arr, NUM);
//获取排序前当前时间
oldTime = GetTickCount64();
insert_sort(arr, NUM);
//获取排序后当前时间
newTime = GetTickCount64();
printf("insert_sort:%llu\n", newTime - oldTime);
init(arr, NUM);
//获取排序前当前时间
oldTime = GetTickCount64();
shell_sort(arr, NUM);
//获取排序后当前时间
newTime = GetTickCount64();
printf("shell_sort:%llu\n", newTime - oldTime);
/*
实际效果:
bubble_sort:22782
select_sort:7578
insert_sort:4000
shell_sort:15
*/
return 0;
}
void init(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
arr[i] = rand() % 1000000;
}
}
void bubble_sort(int* arr, int len)
{
int t;
for (int i = 0; i <= len - 2; i++)
{
for (int j = 0; j <= len - 2 - i; j++)
{
if (arr[j] > arr[j + 1])
{
t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
}
void select_sort(int* arr, int len)
{
int idx;
int t;
for (int i = 0; i < len - 1; i++)//len-1轮
{
idx = i;
for (int j = i; j < len; j++)
{
idx = (arr[idx] < arr[j]) ? idx : j;
}
t = arr[idx];
arr[idx] = arr[i];
arr[i] = t;
}
}
void insert_sort(int* arr, int len)
{
int temp;
int j;
for (int i = 1; i < len; i++)
{
temp = arr[i];
j = i - 1;
for (j = i - 1; j >= 0 && arr[j] > temp; j--)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
void shell_sort(int* arr, int len)
{
int step = len >> 1;
int temp;
int j;
while (step)
{
for (int i = step; i < len; i++)
{
temp = arr[i];
for (j = i - step; j >= 0 && arr[j] > temp; j -= step)
{
arr[j + step] = arr[j];
}
arr[j + step] = temp;
}
step >>= 1;
}
}