1. 快速排序
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define array_size 100
void quicksort(int* a, int q, int r);
int partition(int* a, int m, int n);
void ele_swap(int* a, int e1, int e2);
int main()
{
srand(time(0));
int a0[array_size] = { 0 };
printf("before sorting:\n");
for (int i = 0; i < array_size; i++)
{
a0[i] = rand() % 300;
printf("%d\t", a0[i]);
}
printf("\n");
quicksort(a0, 0, array_size - 1);
printf("after quick_sorting:\n");
for (int i = 0; i < array_size; i++)
printf("%d\t", a0[i]);
printf("\n");
return 0;
}
void quicksort(int* a, int q, int r)
{
if (q != r)
{
int p = partition(a, q, r);
if (p - 1 >= q)
quicksort(a, q, p - 1);
if (p + 1 <= r)
quicksort(a, p + 1, r);
}
}
int partition(int* a, int m, int n)
{
int l = m;
int r = n;
srand(time(0));
int q = (rand() % (r - l)) + l;
while (l != r)
{
while (l < q && a[l] <= a[q])
l++;
ele_swap(a, l, q);
q = l;
while (r > q && a[r] >= a[q])
r--;
ele_swap(a, r, q);
q = r;
}
return l;
}
void ele_swap(int* a, int e1, int e2)
{
int temp = a[e1];
a[e1] = a[e2];
a[e2] = temp;
}
2. 堆排序
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define array_size 20
#define LEFT(x) (x<<1)
void ele_swap(int* a, int e1, int e2);
void max_heapify(int* a, int i);//假设两个子树都是最大堆的情况下,将a[0]下沉使之符合最大堆的性质
void bulid_max_heap(int* a); //构造最大堆
void heapsort(int* a);
int heap_size = 0;
int main()
{
srand(time(0));
int a1[array_size] = { 0 };
for (int i = 0; i < array_size; i++)
a1[i] = rand() % 200;
printf("before heapsorting:\n");
for (int i = 0; i < array_size; i++)
printf("%d\t", a1[i]);
printf("\n");
heapsort(a1);
printf("after heapsorting:\n");
for (int i = 0; i < array_size; i++)
printf("%d\t", a1[i]);
printf("\n");
return 0;
}
void heapsort(int* a)
{
bulid_max_heap(a);
for (int i = heap_size; i >= 2; i--)
{
ele_swap(a, 0, i - 1);
heap_size--;
max_heapify(a, 0);
}
}
void ele_swap(int* a, int e1, int e2)
{
int temp = a[e1];
a[e1] = a[e2];
a[e2] = temp;
}
void max_heapify(int* a, int i)
{
int i1 = i + 1;
int l = LEFT(i1) - 1;
int r = l + 1;
int largest = 0;
if (l<heap_size && a[l] > a[i])
largest = l;
else
largest = i;
if (r<heap_size && a[r]>a[largest])
largest = r;
if (largest != i)
{
ele_swap(a, i, largest);
max_heapify(a, largest);
}
}
void bulid_max_heap(int* a)
{
heap_size = array_size;
for (int i = (heap_size / 2) - 1; i >= 0; i--)
max_heapify(a, i);
}
3. 插入排序
void Insertion_Sort(int* a, int length)
{
int key;
int j;
int i;
for (j = 1; j < length; j++)
{
i = j - 1;
key = a[j];
while (i >= 0 && a[i] > key)
{
a[i + 1] = a[i];
i -= 1;
a[i + 1] = key;
}
}
}
4. 归并排序
归并排序的过程如图:
//这里使用了3个buf来控制比牌的过程,
//具体是将原数组拷贝到同等大小的temp数组作为输入堆(由buf1,buf2对应两堆牌),原数组作为输出堆(buf对应)
//Merge_sort()完成的是第一步分解的过程
//Merge()完成的是解决和合并的过程(当待排序的序列长度为1时,递归开始回升)
#define N 10
int temp[N];
void Merge(int* a, int p, int q, int r)
{
for (int i = p; i <= r; i++)
temp[i] = a[i];
int buf_1 = p;
int buf_2 = q + 1;
int buf = p;
while (buf_1 <= q && buf_2 <= r)
{
if (temp[buf_1] > temp[buf_2])
a[buf++] = temp[buf_2++];
else
a[buf++] = temp[buf_1++];
}
if (buf_1 > q)
{
for (; buf_2 <= r; buf_2++)
a[buf++] = temp[buf_2];
}
else if (buf_2 > r)
{
for (; buf_1 <= q; buf_1++)
a[buf++] = temp[buf_1];
}
return;
}
void Merge_Sort(int* a, int p, int r)
{
if (p >= r)
return;
int q = (r - p) / 2 + p;
Merge_Sort(a, p, q);
Merge_Sort(a, q + 1, r);
Merge(a, p, q, r);
}