1、 插入排序
(1)直接插入排序
//a[]从0起,时间复杂度为:O(n^2)
void insert_sort(int a[], int length)
{
int i, j;
int temp;
for(i = 1; i < length; i++)
{
temp = a[i];
j = i - 1;
while(j >= 0 && temp < a[j])
{
a[j + 1] = a[j];
j--;
}
a[j+1] = temp;
}
}
(2)希尔排序
//a[]从0起,时间复杂度为:O(nlogn)
void shell_sort(int a[], int length)
{
int i, j, d = length / 2;
int temp;
while(d > 0)
{
for(i = d; i < length; i++)
{
j = i - d;
while(j >= 0 &&a[j] > a[j + d])
{
temp = a[j];
a[j] = a[j + d];
a[j + d] = temp;
j = j - d;
}
}
d = d / 2;
}
}
2、 交换排序
(1) 冒泡排序
//a[]从0起, 时间复杂度为:O(n^2)
void bubble_sort(int a[], int length)
{
int i, j, flag = 1;
int temp;
for(i = 0; i < N - 1; i++)
{
flag = 0;
for(j = N - 1; j > i; j--)
{
if(a[j] < a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
flag = 1;
}
}
if(flag == 0)
{
return;
}
}
}
(2) 快速排序
// a[]从0起, 时间复杂度为:O(nlogn)
int partition(int a[], int low, int high)
{
int key;
key = a[low];
while(low < high)
{
while(low < high && a[high] >= key)
{
--high;
}
a[low] = a[high];
while(low < high && a[low] <= key)
{
++low;
}
a[high] = a[low];
}
a[low] = key;
return low;
}
void quick_sort(int a[], int low, int high)
{
int piv;
if(low < high)
{
piv = partition(a, low, high);
quick_sort(a, low, piv - 1);
quick_sort(a, piv + 1,high);
}
}
3、 选择排序
(1) 直接选择排序
// a[]从0起, 时间复杂度为:O(n^2)
void select_sort(int a[], int length)
{
int i, j, k;
int temp;
for(i = 0; i < N - 1; i++)
{
k = i;
for(j = i + 1; j < N; j++)
{
if(a[j] < a[k])
{
k = j;
}
}
if(k != i)
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
(2)堆排序
// a[]从1起, 时间复杂度为:O(nlogn)
void heap_adjust(int a[], int low, inthigh)
{
int i = low, j = 2 * i;
int temp = a[i];
while(j <= high)
{
if(j < high && a[j] < a[j + 1])
{
j++;
}
if(temp < a[j])
{
a[i] = a[j];
i = j;
j = 2 * i;
}
else
{
break;
}
}
a[i] = temp;
}
void heap_sort(int a[], int length)
{
int i;
int temp;
for(i = length / 2; i >= 1; i--)
{
heap_adjust(a, i, length);
}
for(i = length; i >= 2; i--)
{
temp = a[1];
a[1] = a[i];
a[i] = temp;
heap_adjust(a, 1, i - 1);
}
}
4、 归并排序
//递归形式,时间复杂度为:O(nlogn)
void merge(int a[], int low, int mid, inthigh)
{
int *a1;
int i = low, j = mid + 1, k = 0;
a1 = (int *)malloc((high - low + 1) * sizeof(int));
while(i <= mid && j <= high)
{
if(a[i] <= a[j])
{
a1[k] = a[i];
i++;
k++;
}
else
{
a1[k] = a[j];
j++;
k++;
}
}
while(i <= mid)
{
a1[k] = a[i];
i++;
k++;
}
while(j <= high)
{
a1[k] = a[j];
j++;
k++;
}
for(k = 0, i = low; i <= high; k++, i++)
{
a[i] = a1[k];
}
}
void merge_pass(int a[], int length, int n)
{
int i;
for(i = 0; i + 2 * length - 1 < n; i = i + 2 * length)
{
merge(a, i, i + length - 1, i + 2 * length - 1);
}
if(i + length - 1 < n)
{
merge(a, i, i + length - 1, n - 1);
}
}
void merge_sort(int a[], int n)
{
int length;
for(length = 1; length < n; length = 2 * length)
{
merge_pass(a, length, n);
}
}
//非递归形式
void merge(int a[], int h, int m, int r)
{
int p = 0;
int i = h, j = m+1;
while(i <= m && j <= r)
{
if(a[i] > a[j])
{
t[p++] = a[j++];
}
else
{
t[p++] = a[i++];
}
}
while(i <= m)
{
t[p++] = a[i++];
}
while(j < r)
{
t[p++] = a[j++];
}
for(i = 0; i < p; i++)
{
a[h + i] = t[i];
}
}
void merge_sort(int a[], int h, int r)
{
int m;
if(h < r)
{
m = (h + r) / 2;
merge_sort(a, h, m);
merge_sort(a, m + 1, r);
merge(a, h, m, r);
}
}
5、 基数排序
//
#defineMAXE 20 /*线性表中最多元素个数*/
#defineMAXR 10 /*基数的最大取值*/
#defineMAXD 8 /*关键字位数的最大取值*/
typedefstruct node
{ char data[MAXD]; /*记录的关键字定义的字符串*/
struct node *next;
}RecType;
voidRadixSort(RecType *&p,int r,int d)
/*p为待排序序列链表指针,r为基数,d为关键字位数*/
{
RecType *head[MAXR],*tail[MAXR],*t; //定义各链队的首尾指针
int i,j,k;
for (i=d-1;i>=0;i--) /*从低位到高位做d趟排序*/
/*123以“321”存储*/
{ for(j=0;j<r;j++) /*初始化各链队首、尾指针*/
head[j]=tail[j]=NULL;
while (p!=NULL) /*对于原链表中每个结点循环*/
{
k=p->data[i]-'0'; /*找第k个链队*/
if (head[k]==NULL)
/*进行分配,即采用尾插法建立单链表*/
{
head[k]=p; tail[k]=p;
}
else
{
tail[k]->next=p;
tail[k]=p;
}
p=p->next; /*取下一个待排序的元素*/
}
p=NULL;
for(j=0;j<r;j++) /*对于每一个链队循环进行收集*/
if(head[j]!=NULL)
{
if (p==NULL)
{ p=head[j];
t=tail[j];
}
else
{
t->next=head[j];
t=tail[j];
}
}
t->next=NULL; /*最后一个结点的next域置NULL*/
}
}