• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏吧

21小时前 8次浏览

C语言实现九大排序算法

直接插入排序

``````#include <stdio.h>

void insertion_sort(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
int data = arr[i];
int j = 0;
while (arr[j] < arr[i]) {
j++;
}
for (int k = i; k >= j + 1; k--) {
arr[k] = arr[k - 1];
}
arr[j] = data;
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int arr[7] = {8, 2, 6, 0, 5, 7, 4};
insertion_sort(arr, 7);
print_array(arr, 7);
return 0;
}
``````

折半插入排序

``````#include <stdio.h>

void binary_insertion_sort(int arr[], int array_length) {
int i, j, low = 0, high = 0, mid;
int temp = 0;
for (i = 1; i < array_length; i++) {
low = 0;
high = i - 1;
temp = arr[i];
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > temp) {
high = mid - 1;
} else {
low = mid + 1;
}
}
for (j = i; j > low; j--) {
arr[j] = arr[j - 1];
}
arr[low] = temp;
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int brr[5] = {2, 6, 0, 5, 7};
binary_insertion_sort(brr, 5);
print_array(brr, 5);
return 0;
}
``````

希尔排序

``````#include <stdio.h>

void shell_sort(int arr[], int array_length) {
int step = array_length / 2;
while (step >= 1) {
for (int i = 0; i < array_length; i += step) {
int data = arr[i];
int j = 0;
while (arr[j] < arr[i]) {
j++;
}
for (int k = i; k >= j + 1; k--) {
arr[k] = arr[k - 1];
}
arr[j] = data;
}
step = step / 2;
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int crr[10] = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81};
shell_sort(crr, 10);
print_array(crr, 10);
return 0;
}
``````

冒泡排序

``````#include <stdio.h>

void bubble_sort(int arr[], int array_length) {
for (int i = 0; i < array_length - 1; ++i) {
for (int j = 0; j < array_length - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int drr[7] = {8, 2, 6, 0, 5, 7, 4};
bubble_sort(drr, 7);
print_array(drr, 7);
return 0;
}
``````

快速排序

``````#include <stdio.h>

int getStandard(int arr[], int low, int high) {
int flag = arr[low];
while (low < high) {
while (low < high && arr[high] >= flag) {
high--;
}
if (low < high) {
arr[low] = arr[high];
}
while (low < high && arr[low] <= flag) {
low++;
}
if (low < high) {
arr[high] = arr[low];
}
}
arr[low] = flag;
return low;
}

void quick_sort(int arr[], int low, int high) {
if (low < high) {
int pos = getStandard(arr, low, high);
quick_sort(arr, low, pos - 1);
quick_sort(arr, pos + 1, high);
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int err[10] = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81};
quick_sort(err, 0, 9);
print_array(err, 10);
return 0;
}
``````

直接选择排序

``````#include <stdio.h>

void select_sort(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
int min_pos = i;
for (int j = i; j < array_length; ++j) {
if (arr[min_pos] > arr[j])
min_pos = j;
}
int temp = arr[min_pos];
arr[min_pos] = arr[i];
arr[i] = temp;
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int frr[7] = {8, 2, 6, 0, 5, 7, 4};
select_sort(frr, 7);
print_array(frr, 7);
return 0;
}
``````

堆排序

``````#include <stdio.h>

void heap_adjust(int arr[], int n) {
for (int i = n / 2; i >= 1; i--) {
if (arr[i - 1] < arr[2 * i - 1]) {
int temp = arr[i - 1];
arr[i - 1] = arr[2 * i - 1];
arr[2 * i - 1] = temp;
}
if (arr[i - 1] < arr[2 * i] && (2 * i) < n) {
int temp = arr[i - 1];
arr[i - 1] = arr[2 * i];
arr[2 * i] = temp;
}
}
}

void heap_sort(int arr[], int array_length) {
int n = array_length;
do {
int temp = arr[0];
arr[0] = arr[n - 1];
arr[n - 1] = temp;
} while (n--);
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int grr[7] = {8, 2, 6, 0, 5, 7, 4};
heap_sort(grr, 7);
print_array(grr, 7);
return 0;
}
``````

归并排序

``````#include <stdio.h>
#include <malloc.h>

void merge(int arr[], int start, int mid, int end) {
int *new_array = (int *) malloc(sizeof(int) * (end - start + 1));
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end) {
if (arr[i] < arr[j]) {
new_array[k++] = arr[i++];
} else {
new_array[k++] = arr[j++];
}
}
while (i <= mid) {
new_array[k++] = arr[i++];
}
while (j <= end) {
new_array[k++] = arr[j++];
}
for (int l = 0; l < k; ++l) {
arr[start + l] = new_array[l];
}
free(new_array);
}

void merge_sort(int arr[], int start, int end) {
int mid = (start + end) / 2;
if (start >= end) {
return;
}
merge_sort(arr, start, mid);
merge_sort(arr, mid + 1, end);
merge(arr, start, mid, end);
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int hrr[10] = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81};
merge_sort(hrr, 0, 9);
print_array(hrr, 10);
return 0;
}
``````

基数排序

``````#include <stdio.h>
#include <malloc.h>

int get_num(int number, int pos) {
int num = 0;
while (pos--) {
num = number % 10;
number = number / 10;
}
return num;
}

void radix_sort(int arr[], int array_length) {
int *bucket[10];
for (int i = 0; i < 10; ++i) {
bucket[i] = (int *) malloc(sizeof(int) * array_length + 1);
bucket[i][0] = 0;//桶的第一位保存桶中元素个数
}
for (int b = 1; b <= 31; ++b) {
for (int i = 0; i < array_length; ++i) {
int num = get_num(arr[i], b);//计算每个位上的数字（个位、十位、百位...）
int index = ++bucket[num][0];//计算下标
bucket[num][index] = arr[i];//保存到桶中
}
for (int i = 0, k = 0; i < 10; i++) {
for (int j = 1; j <= bucket[i][0]; ++j) {
arr[k++] = bucket[i][j];//从桶里面按顺序取出来
}
bucket[i][0] = 0;//下标清零
}
}
}

void print_array(int arr[], int array_length) {
for (int i = 0; i < array_length; ++i) {
printf("%d ", arr[i]);
}
printf("n");
}

int main() {
int irr[10] = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81};