1、希尔排序
#include <stdio.h>
void swap (int *a, int i, int j)
{
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void printfA (int *a, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf ("%4d", a[i]);
}
printf ("\n");
}
// 希尔排序方法一
int main1()
{
int a[10] = {2,5,1,9,8,3,7,4,6,0};
int len = sizeof(a)/sizeof(a[0]);
int get; //抓牌
int i,j;
int d = len; //代表每一次的步长
do
{
d = d / 3 + 1;
for (i = d; i < len; i++)
{
get = a[i]; //抓牌
j = i-d;
while (j >= 0 && a[j] > get)
{
a[j+d] = a[j];
j -= d;
}
a[j+d] = get;
}
}while (d > 1);
printfA (a, len);
return 0;
}
// 希尔排序方法二
int main()
{
int a[10] = {2,5,1,9,8,3,7,4,6,0};
int len = sizeof(a)/sizeof(a[0]);
int get; //抓牌
int i,j;
int d = 0;
while (d < len)
{
d = d*3+1; //代表每一次的步长 1 4 13
}
while (d >= 1)
{
for (i = d; i < len; i++)
{
get = a[i]; //抓牌
j = i-d;
while (j >= 0 && a[j] > get)
{
a[j+d] = a[j];
j -= d;
}
a[j+d] = get;
}
d = (d -1)/3; // 4 1 0
}
printfA (a, len);
return 0;
}
#include <stdio.h>
void swap (int *a, int i, int j)
{
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void printfA (int *a, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf ("%4d", a[i]);
}
printf ("\n");
}
// 分区操作,返回基准值的下标
int partition (int *a, int left, int right)
{
int pivot = a[right];
int index = left; // 如果找到比基准值小的元素,与下标为index的元素交换位置
int i;
for(i = left; i < right; i++)
{
if (a[i] < pivot)
{
swap (a, i, index);
index++;
}
}
swap (a, index, right);
return index; //返回基准值所在下标
}
// 快速排序
void qSort (int *a, int left, int right)
{
if (left < right)
{
int pivot = partition (a, left, right);
//进行分区,找基准值下标
qSort (a, left, pivot-1);
qSort (a, pivot+1, right);
}
}
int main()
{
int a[] = {2,5,1,9,8,3,7,4,6,0};
int len = sizeof(a)/sizeof(a[0]);
int tmp[10];
qSort (a, 0, len-1);
printfA (a, len);
return 0;
}
3、堆排序
#include <stdio.h>
void swap (int *a, int i, int j)
{
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void printfA (int *a, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf ("%4d", a[i]);
}
printf ("\n");
}
//将堆调整为最大堆
void heapify (int *a, int i, int len)
{
int left = 2 * i + 1; //左孩子的结点下标
int right = 2 * i + 2; //右孩子的结点下标
int max = i; //三个结点中最大元素的下标
if (left < len && a[left] > a[max])
max = left;
if (right < len && a[right] > a[max])
max = right;
if (max != i) // 当前父结点不是所有结点中最大元素,需要进行调整
{
swap (a, i, max);
heapify (a, max, len); //因为max与父结点互换数据,所以,max也需要调整自己的子结点
}
}
void heapsort (int *a, int len)
{
int i;
//建堆
// len/2-1 为最后一个非叶结点
for (i = len/2-1; i >= 0; i--)
{
heapify (a, i, len);
}
//排序
for (i = len-1; i > 0; i--)
{
swap (a, i, 0);
len--;
heapify (a, 0, len);
}
}
int main()
{
int a[] = {2,5,1,9,8,3,7,4,6,0};
int len = sizeof(a)/sizeof(a[0]);
heapsort (a, len);
printfA (a, len);
return 0;
}
4、归并排序
#include <stdio.h>
void swap (int *a, int i, int j)
{
int tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void printfA (int *a, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf ("%4d", a[i]);
}
printf ("\n");
}
void merge (int *a, int left, int mid, int right, int *tmp)
{
int i = left;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= right)
{
if (a[i] > a[j])
tmp[k++] = a[j++];
else
tmp[k++] = a[i++];
}
while (i <= mid)
tmp[k++] = a[i++];
while (j <= right)
tmp[k++] = a[j++];
k = 0;
for (i = left; i <= right; i++)
{
a[i] = tmp[k++];
}
}
void mergesort (int *a, int left, int right, int *tmp)
{
if (left >= right)
return;
int mid = (left + right) / 2;
mergesort (a, left, mid, tmp);
mergesort (a, mid+1, right, tmp);
merge (a, left, mid, right, tmp);
}
int main()
{
int a[] = {2,5,1,9,8,3,7,4,6,0};
int len = sizeof(a)/sizeof(a[0]);
int tmp[10];
mergesort (a, 0, len-1, tmp);
printfA (a, len);
return 0;
}