一、直接插入排序
void insertion_sort(int a[],const int start,const int end)
{
int tmp;
int i,j;
for(i = start + 1;i < end;i++)
{
tmp = a[i];
for(j = i - 1; j >= start && tmp < a[j];j--)
a[j+1] = a[j];
a[j+1] = tmp;
}
}
二、折半插入排序
void binary_insertion_sort(int a[],const int start,const int end)
{
int i,j;
for(i = start + 1;i < end;i++)
{
int tmp = a[i];
int left = start;
int right = i - 1;
while(left <= right)
{
int mid = left + (right-left>>1);
if(tmp < a[mid])
right = mid - 1;
else
left = mid + 1;
}
for(j = i - 1; j >= left;j--)//left为第一个大于tmp的位置
a[j+1] = a[j];
a[left] = tmp;
}
}
三、希尔排序
//目前最快的增量序列为Sedgewick序列:1,5,19...且实践中快于堆排序
void shell_sort(int a[],const int start,const int end)
{
int tmp,gap;
int i,j;
for(gap = (end-start>>1);gap > 0;gap >>= 1)
for(i = start+gap;i < end;i++)
{
tmp = a[i];
for(j = i - gap;j >= start && tmp < a[j];j-=gap)
a[j+gap] = a[j];
a[j+gap] = tmp;
}
}
四、冒泡排序
void bubble_sort(int a[],const int start,const int end)
{
int i,j;
int tmp,exchange;
for(i = start; i < end - 1;i++)
{
exchange = 0;
for(j = end - 1; j > i;j--)
if(a[j-1] > a[j])
{
tmp = a[j-1];
a[j-1] = a[j];
a[j] = tmp;
exchange = 1;
}
if(!exchange) //本趟无逆序,停止处理
return ;
}
}
五、选择排序
void selection_sort(int a[],const int start,const int end)
{
int tmp;
int i,j,k;
for(i = start; i < end;i++)
{
k = i;
for(j = i+1;j < end;j++)
if(a[j] < a[k])
k = j;
if(k != i)
{
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
}
六、归并排序
//tmp数组与a等长,全程排序只需这一个辅助数组
void merge(int a[],int tmp[],const int start,const int mid,const int end)
{
int i,j,k;
for(i = 0; i < end;i++)
tmp[i] = a[i];
i = start,j = mid,k = start;
while(i < mid && j < end)
if(tmp[i] < tmp[j])
a[k++] = tmp[i++];
else
a[k++] = tmp[j++];
//其中之一有剩余,两者只执行一个
while(i < mid)
a[k++] = tmp[i++];
while(j < end)
a[k++] = tmp[j++];
}
void merge_sort(int a[],int tmp[],const int start,const int end)
{
if(start < end - 1)
{
const int mid = start + (end-start>>1);
merge_sort(a,tmp,start,mid);
merge_sort(a,tmp,mid,end);
merge(a,tmp,start,mid,end);
}
}
七、堆排序
#define LeftChild(i) (2*(i) + 1) //从0开始
void percolate_down(int a[],int i,int n)
{
int tmp,child;
for(tmp = a[i];LeftChild(i) < n;i = child)
{
child = LeftChild(i);
if(child != n-1 && a[child+1] > a[child])
child++;
if(tmp < a[child])
a[i] = a[child];
else
break;
}
a[i] = tmp;
}
void heap_sort(int a[],int n)
{
int i;
for(i = n/2;i >= 0;i--) //build heap
percolate_down(a,i,n);
for(i = n-1;i > 0;i--)
{
swap(a[0],a[i]); //delete max
percolate_down(a,0,i);
}
}
八、计数排序
void counting_sort(int *ini_arr, int *sorted_arr, int n)
{
int *count_arr = (int *)malloc(sizeof(int) * 100);
int i;
for(i = 0; i < 100; i++)
count_arr[i] = 0;
for(i = 0; i < n; i++)
count_arr[ini_arr[i]]++;
for(i = 1; i < 100; i++)
count_arr[i] += count_arr[i-1];
for(i = n; i > 0; i--) //稳定的关键是从后往前
{
sorted_arr[count_arr[ini_arr[i-1]]-1] = ini_arr[i-1];
count_arr[ini_arr[i-1]]--;
}
free(count_arr);
}
九、桶排序
Bucket-Sort(A) //最简单的情况是一个桶一个整数
let B[0..n-1] be a new array
n = A.lenghtS
for i = 0 to n - 1
make B[i] an empty list
for i = 1 to n
insert A[i] into list B[nA[i]]
for i = 0 to n - 1
sort list B[i] with insertion sort
concatenate the lists B[0], B[1]....B[n - 1] togather in order

十、基数排序
#define MAX 100
#define BASE 10
//基于计数排序
void radix_sort(int *a, int n)
{
int i, b[MAX], m = a[0], exp = 1;
for (i = 1; i < n; i++)
if (a[i] > m)
m = a[i];
while (m / exp > 0)
{
int bucket[BASE] = { 0 };
for (i = 0; i < n; i++)
bucket[(a[i] / exp) % BASE]++;
for (i = 1; i < BASE; i++)
bucket[i] += bucket[i - 1];
for (i = n - 1; i >= 0; i--)
b[--bucket[(a[i] / exp) % BASE]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= BASE;
}
}
十一、快速排序
//以最后一个元素为pivot
int partition(int a[],const int start,const int end)
{
const int pivot = a[end-1];
int i = start - 1,j;
for(j = start;j < end-1;j++)
if(a[j] <= pivot)
{
i++;
swap(a[i],a[j]);
}
swap(a[i+1],a[end-1]);
return i+1;
}
void quick_sort(int a[],int start,int end)
{
if(start < end - 1) //至少两个元素
{
const int pos = partition(a,start,end);
quick_sort(a,start,pos);
quick_sort(a,pos+1,end);
}
}
//随机化pivot
int rand_partition(int a[],const int start,const int end)
{
int i = random(start,end); //[start,end)
swap(a[i],a[end-1]);
return partition(a,start,end);
}
//以第一个元素为pivot,同时减少交换
int partition(int a[],const int start,const int end)
{
int i = start,j = end - 1;
const int pivot = a[i];
while(i < j)
{
while(i < j && a[j] >= pivot)
j--;
a[i] = a[j];
while(i < j && a[i] <= pivot)
i++;
a[j] = a[i];
}
a[i] = pivot;
return i;
}
void quick_sort(int a[],const int start,const int end)
{
if(start < end - 1) //至少两个元素
{
const int pos = partition(a,start,end);
quick_sort(a,start,pos);
quick_sort(a,pos+1,end);
}
}
int median3(int a[],int left,int right)
{
int mid = left + (right-left>>1);
if(a[left] > a[mid])
swap(a[left],a[mid]);
if(a[left] > a[right])
swap(a[left],a[right]);
if(a[mid] > a[right])
swap(a[mid],a[right]);
swap(a[mid],a[right-1]); //hide pivot
return a[right-1];
}
void insertion_sort(int a[],const int n)
{
int tmp;
int i,j;
for(i = 1; i < n; i++)
{
tmp = a[i];
for(j = i - 1; j >= 0 && tmp < a[j]; j--)
a[j+1] = a[j];
a[j+1] = tmp;
}
}
//三数取中值法,同时小数组有插入排序完成
//[left,right],方便起见
#define Cutoff (3)
void quick_sort(int a[],const int left,const int right)
{
int i,j;
int pivot;
if(left + Cutoff <= right)
{
pivot = median3(a,left,right);
i = left,j = right - 1;
for(; ;)
{
while(a[++i] < pivot){}
while(a[--j] > pivot){}
if(i < j)
swap(a[i],a[j]);
else
break;
}
swap(a[i],a[right-1]); //restore pivot
quick_sort(a,left,i-1);
quick_sort(a,i+1,right);
}
else
insertion_sort(a+left,right-left+1);
}