用c语言编写实现顺序表的简单排序、直接插入排序、冒泡排序 快速排序、两路合并排序以及堆排序
时间: 2023-11-24 09:07:36 浏览: 65
直接插入、折半插入、冒泡、快速、简单选择等排序方法 用c语言实现
以下是用C语言实现顺序表的简单排序、直接插入排序、冒泡排序、快速排序、两路合并排序以及堆排序的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
// 简单排序
void simpleSort(SqList *L) {
int i, j;
for (i = 0; i < L->length - 1; i++) {
for (j = i + 1; j < L->length; j++) {
if (L->data[i] > L->data[j]) {
int tmp = L->data[i];
L->data[i] = L->data[j];
L->data[j] = tmp;
}
}
}
}
// 直接插入排序
void insertSort(SqList *L) {
int i, j;
for (i = 1; i < L->length; i++) {
int tmp = L->data[i];
for (j = i - 1; j >= 0 && L->data[j] > tmp; j--) {
L->data[j + 1] = L->data[j];
}
L->data[j + 1] = tmp;
}
}
// 冒泡排序
void bubbleSort(SqList *L) {
int i, j;
for (i = 0; i < L->length - 1; i++) {
for (j = 0; j < L->length - 1 - i; j++) {
if (L->data[j] > L->data[j + 1]) {
int tmp = L->data[j];
L->data[j] = L->data[j + 1];
L->data[j + 1] = tmp;
}
}
}
}
// 快速排序
int partition(SqList *L, int low, int high) {
int pivotkey = L->data[low];
while (low < high) {
while (low < high && L->data[high] >= pivotkey) {
high--;
}
L->data[low] = L->data[high];
while (low < high && L->data[low] <= pivotkey) {
low++;
}
L->data[high] = L->data[low];
}
L->data[low] = pivotkey;
return low;
}
void quickSort(SqList *L, int low, int high) {
if (low < high) {
int pivotloc = partition(L, low, high);
quickSort(L, low, pivotloc - 1);
quickSort(L, pivotloc + 1, high);
}
}
// 两路合并排序
void merge(int *a, int *b, int left, int mid, int right) {
int i = left, j = mid + 1, k = left;
while (i <= mid && j <= right) {
if (a[i] <= a[j]) {
b[k++] = a[i++];
} else {
b[k++] = a[j++];
}
}
while (i <= mid) {
b[k++] = a[i++];
}
while (j <= right) {
b[k++] = a[j++];
}
for (i = left; i <= right; i++) {
a[i] = b[i];
}
}
void mergeSort(int *a, int *b, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(a, b, left, mid);
mergeSort(a, b, mid + 1, right);
merge(a, b, left, mid, right);
}
}
// 堆排序
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void adjustHeap(int *a, int i, int len) {
int j, tmp;
tmp = a[i];
for (j = 2 * i; j <= len; j *= 2) {
if (j < len && a[j] < a[j + 1]) {
j++;
}
if (tmp >= a[j]) {
break;
}
a[i] = a[j];
i = j;
}
a[i] = tmp;
}
void heapSort(int *a, int len) {
int i;
for (i = len / 2; i > 0; i--) {
adjustHeap(a, i, len);
}
for (i = len; i > 1; i--) {
swap(&a[1], &a[i]);
adjustHeap(a, 1, i - 1);
}
}
int main() {
SqList L = {{5, 3, 8, 4, 2, 7, 1, 6}, 8};
// 简单排序
simpleSort(&L);
printf("simple sort: ");
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
// 直接插入排序
insertSort(&L);
printf("insert sort: ");
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
// 冒泡排序
bubbleSort(&L);
printf("bubble sort: ");
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
// 快速排序
quickSort(&L, 0, L.length - 1);
printf("quick sort: ");
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
// 两路合并排序
int a[] = {5, 3, 8, 4, 2, 7, 1, 6};
int b[8];
mergeSort(a, b, 0, 7);
printf("merge sort: ");
for (int i = 0; i < 8; i++) {
printf("%d ", a[i]);
}
printf("\n");
// 堆排序
int c[] = {0, 5, 3, 8, 4, 2, 7, 1, 6};
heapSort(c, 8);
printf("heap sort: ");
for (int i = 1; i <= 8; i++) {
printf("%d ", c[i]);
}
printf("\n");
return 0;
}
```
阅读全文