# 数据结构与算法之排序大全

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE (1)
#define FALSE (0)
#define SIZE  (10)
#define KEY_NUM  (10)
typedef unsigned char boolean;
static void swap(void *a, void *b, int len);
static void swap1(int *a, int *b);

int print_arr(int *arr, int len);
void bubble_sort(int *arr, int len);
void  bubble_sort1(int *arr, int len);
void bubble_sort2(int *arr, int len);
void insert_sort(int *array, int length);    //插入排序, 时间复杂度O（n^2），稳定
void shell_sort(int *arr, int len);
void select_sort(int *arr, int len);
void quick_sort(int *arr, int l, int r);
boolean merge_sort(int *arr, int len);
void radix_sort(int *arr, int len);
void count_sort(int *arr, int len, int max);
void min_heap_sort_order_asc_to_arr(int *arr, int len);

static void swap(void *a, void*b, int len)
{
void *temp = malloc(len);

memcpy(temp, a, len);
memcpy(a, b, len);
memcpy(b, temp, len);

free(temp);
}
static void swap1(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
int print_arr(int *arr, int len)
{
int  i = 0;

if (arr != NULL && len > 0){
while (len != i){
printf("%d ", arr[i]);
++i;

}
printf("\n");
}

return (TRUE);

}
//冒泡排序
void bubble_sort(int *arr, int len)
{
int i = 0;
int j = 0;

for (i = 0; i < len -1; i++){
for (j = 1; j < len; ++j){
if (arr[j] < arr[j - 1]){
swap(&arr[j], &arr[j - 1], sizeof(arr[j]));
}
}
}
}
void  bubble_sort1(int *arr, int len)
{
int i = 0;
int j = len;
int flag = TRUE;

while (flag)
{
flag = FALSE;
for (i = 1; i < j; ++i){
if (arr[i] < arr[i - 1]){
swap(&arr[i], &arr[i - 1], sizeof(arr[i]));
flag = TRUE;
}
}
--j;
}
}
void bubble_sort2(int *arr, int len)
{
int i = 0;
int k = 0;
int flag = len;

while (flag > 0 )
{
k = flag;
flag = 0;
for (i = 1; i < k; ++i){
if (arr[i] < arr[i - 1]){
swap(&arr[i], &arr[i - 1], sizeof(arr[i]));
flag = i;
}
}
}

}
//插入排序
void insert_sort(int *array, int length)    //插入排序, 时间复杂度O（n^2），稳定
{
int i = 0;
int j = 0;

for (i = 1; i < length; ++i){
for (j = i - 1; j >= 0  && array[j] > array[j + 1]; --j){
swap(&array[j], &array[j + 1], sizeof(length));
}
}
}
//shell排序
void shell_sort(int *arr, int len)
{
int i = 0;
int j = 0;
int gap = len >> 1;

for (; gap > 0; gap >>= 1){
for (i = gap; i < len; ++i){
for (j = i - gap; j >= 0 && arr[j] >arr[j + gap]; j -= gap){
//swap(&arr[j], &arr[j + gap], sizeof(arr));
swap1(&arr[j], &arr[j + gap]);
}
}
}
}
//快速排序
void quick_sort(int *arr, int l,int r)
{
if (l < r){
int i = l;
int j = r;
int x = arr[i];
while (i < j )
{
//从右往左找第一个小于x的数,
while (i < j && arr[j] >= x)
{
--j;
}
if (i <j)
{
arr[i++] = arr[j];
}
//从左向右找第一个大于等于x的数
while (i < j && arr[i]  < x)
{
++i;
}
if (i < j){
arr[j--] = arr[i];
}

arr[i] = x;
quick_sort(arr, l, i - 1);
quick_sort(arr, i + 1, r);
}

}
}

static void merge_arr(int *arr, int first, int mid, int last, int *temp)
{
int i = first;
int j = mid;
int m = mid + 1;
int n = last;
int k = 0;

while (i <=j && m<= n)
{
if (arr[i] <= arr[m]){
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[m++];
}
}

while (i <= j)
{
temp[k++] = arr[i++];
}

while (m <= n)
{
temp[k++] = arr[m++];
}

for (i = 0; i < k; ++i){
arr[first + i] = temp[i];
}
}
static void Merge_sort(int *arr, int first, int last,int *temp)
{
int mid = 0;
if (first < last){
mid = first + ((last - first) >> 1);
Merge_sort(arr, first, mid, temp);
Merge_sort(arr, mid + 1, last, temp);
merge_arr(arr, first, mid, last, temp);
}
}
//归并排序
boolean merge_sort(int *arr, int len)
{
if (NULL != arr && len >= 2){
int *temp = (int *)malloc(len * (sizeof(arr[0])));
if (NULL == temp){
return (FALSE);
}

Merge_sort(arr, 0, len - 1, temp);
free(temp);
temp = NULL;
}

return (TRUE);
}
static int get_num_in_ptr(int value, int ptr)
{
int num = 1;
while (ptr-- > 1)
{
num *= 10;
}

return (value / num) % 10;
}
//基数排序
void radix_sort(int *arr, int len)
{
int *bracket[SIZE];
int i = 0;
int j = 0;
int k = 0;
int ptr = 1;
int num = 0;
int index = 0;

for (; i < SIZE; ++i){
bracket[i] = (int *)malloc((len + 1) * sizeof(arr[0]));
bracket[i][0] = 0;
}
for (; ptr <= KEY_NUM; ++ptr)
{
for (i = 0; i < len; ++i){
num = get_num_in_ptr(arr[i], ptr);
bracket[num][0]++;
index = bracket[num][0];
bracket[num][index] = arr[i];
}
for (i = 0, j = 0; i < SIZE; ++i){
for (k = 1; k <= bracket[i][0]; ++k){
arr[j++] = bracket[i][k];
}
bracket[i][0] = 0;
}
}

for (i = 0; i < SIZE; ++i){
free(bracket[i]);
}

}
//计数排序

void count_sort(int *arr, int len, int max)
{
int *count = NULL;
int i = 0;
int j = 0;

count = (int *)malloc(sizeof(arr[0]) * (max + 1));
if (NULL == count){
return ;
}
memset(count, 0, sizeof(arr[0])*(max + 1));

for (i = 0; i < len; ++i){
count[arr[i]]++;
}

for (i = 0, j = 0; i <= max; ++i){
while (count[i]-- > 0)
{
arr[j++] = i;
}
}
free(count);
count = NULL;
}
//选择排序
void select_sort(int *arr, int len)
{
int i = 0;
int j = 0;
int min = 0;

for (; i < len; ++i){
min = i;
for (j = i + 1; j < len; ++j){
if (arr[j] < arr[min]){
min = j;
}
}
swap1(&arr[i],&arr[min]);

}

}
static void min_heap_fix_down(int *arr, int i, int n)
{
int j = 0;
int temp = 0;

temp = arr[i];
j = (i << 1) + 1;
while (j < n)
{
//寻找左右孩子中最小的节点
if (j + 1 < n && arr[j + 1] < arr[j]){
++j;
}
if (arr[j] >= temp){
break;
}

//把较小的节点往上移动，替换其父节点
arr[i] = arr[j];
i = j;
j = (i << 1)+1;
}
arr[i] = temp;
}
void min_heap_sort_order_desc_to_arr(int *arr, int len)
{
for (int i = len - 1; i >= 1; --i){
swap1(&arr[i], &arr[0]);
min_heap_fix_down(arr, 0, i);
}
}

int main(int argc, char **argv)
{
int arr[] = { 12, 34, 46, 57, 35, 67, 13, 68, 24, 98, 23 };
int len = sizeof(arr) / sizeof(arr[0]);
int max = 100;
printf("before sorting：");
print_arr(arr, len);
//bubble_sort(arr, len);
//bubble_sort1(arr, len);
//bubble_sort2(arr, len);
//insert_sort(arr, len);
//shell_sort(arr, len);
//quick_sort(arr, 0, len - 1);
//merge_sort(arr, len);
//count_sort(arr, len, max);
min_heap_sort_order_desc_to_arr(arr, len);
printf("after sorting：");
print_arr(arr, len);

system("pause");
return (TRUE);
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：数据结构与算法之排序大全 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)