五种排序算法
- 冒泡排序
unsigned int length = 10;
for (i = 0; i < length; i++)
{
for (int j = i; j < length; j++)
{
if (arr[i] > arr[j])
{
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
- 选择排序
unsigned int length = 10;
for (i=0;i<length;i++){
int lower = arr[i];
int location;
for (int j=i;j<length;j++){
if (lower>arr[j]){
lower = arr[j];
location = j;
}
}
if (lower != arr[i]){
int temp;
temp = arr[i];
arr[i] = lower;
arr[location] = temp;
}
}
- 插入排序
for (i=1;i<length;i++){
int temp = arr[i];
int j = i - 1;
// 是否大于前面一个数
// 是否到头了
while (temp < arr[j] && j >= 0){
// 向前移动一位
arr[j+1] = arr[j];
arr[j] = temp;
j --;
}
}
- 希尔排序
- 快速排序