#include <stdio.h>
#include <windows.h>
#define MAXSIZE 10
#define SWAP(x, y) \
{ \
int temp = x; \
x = y; \
y = temp; \
} // 宏交换
typedef struct Node
{
int data;
struct Node *next;
} LinkNode, *Linklist; // 链表结构体
Linklist CreateLinklist(Linklist *L, int arr[], int n); // 链表(带头结点)创建函数
void LInsertSort(Linklist *L); // 链表直接插入排序
void InsertSort1(int arr[], int n); // 直接插入排序
void InsertSort2(int arr[], int n); // 直接插入排序(折半查找)
void ShellSort(int arr[], int n); // 希尔排序
void BubbleSort(int arr[], int n); // 冒泡排序
void DBubbleSort(int arr[], int n); // 双向冒泡排序
void LBubbleSort(Linklist *L); // 链表冒泡排序
int Partition(int arr[], int low, int high); // 快速排序划分函数
void QuickSort(int arr[], int low, int high); // 快速排序
void SelectSort(int arr[], int n); // 简单选择排序
void LSelectSort(Linklist *L); // 链表简单选择排序
void HeapAdjust(int arr[], int k, int n); // 堆调整函数
void HeapSort(int arr[], int n); // 堆排序
void Merge(int arr[], int temp[], int low, int mid, int high); // 归并函数
void MergeSort(int arr[], int temp[], int low, int high); // 归并排序
void showArr(int arr[], int n); // 此函数用于输出数组序列
void showLinkList(Linklist L); // 此函数用于输出链表序列
int main()
{
int arr[MAXSIZE] = {1, 3, 6, 7, 9, 2, 5, 4, 8, 10}; // 测试用例
int temp[MAXSIZE] = {0}; // 堆排序辅助数组
Linklist L = NULL;
CreateLinklist(&L, arr, MAXSIZE);
MergeSort(arr, temp, 0, MAXSIZE - 1);
// DBubbleSort(arr, MAXSIZE);
showArr(arr, MAXSIZE);
system("pause");
return 0;
}
Linklist CreateLinklist(Linklist *L, int arr[], int n)
{
// 链表初始化
*L = (Linklist)malloc(sizeof(LinkNode));
(*L)->data = n; // 用头结点的数据域保存链表长度
Linklist tail = *L;
Linklist p = NULL;
// 链表创建,尾插法
for (int i = 0; i < n; i++)
{
p = (Linklist)malloc(sizeof(LinkNode));
p->data = arr[i];
p->next = NULL;
tail->next = p;
tail = p;
}
return *L;
}
// 链表直接插入排序
void LInsertSort(Linklist *L)
{
Linklist q = (*L)->next;
Linklist p = q->next;
Linklist front = NULL;
Linklist rear = NULL;
Linklist temp = NULL;
for (int i = 1; i < (*L)->data; i++)
{
if (p->data < q->data)
{
front = (*L)->next;
rear = front;
while (front->data <= p->data)
{
rear = front;
front = front->next;
}
q->next = p->next;
temp = p;
p = p->next;
temp->next = front;
rear->next = temp;
}
else
{
q = p;
p = p->next;
}
}
}
// 直接插入排序
void InsertSort1(int arr[], int n)
{
int i, j, temp;
for (i = 1; i < n; i++)
{
if (arr[i - 1] > arr[i])
{
temp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > temp; j--)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
}
// 直接插入排序(改进型) 采用折半查找寻找插入位置
void InsertSort2(int arr[], int n)
{
int i, j, temp, low, high, mid;
for (i = 1; i < n; i++)
{
if (arr[i - 1] > arr[i])
{
temp = arr[i];
low = 0, high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] >= temp)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
for (j = i - 1; j >= low; j--)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
}
// 希尔排序
void ShellSort(int arr[], int n)
{
int i, j, temp, d;
for (d = n / 2; d >= 1; d /= 2)
{
for (int i = d; i < n; i += d)
{
if (arr[i - d] > arr[i])
{
temp = arr[i];
for (j = i - d; j >= 0 && arr[j] > temp; j -= d)
{
arr[j + d] = arr[j];
}
arr[j + d] = temp;
}
}
}
}
// 冒泡排序
void BubbleSort(int arr[], int n)
{
int i, j, temp, flag = 1;
for (i = 0; i < n - 1; i++)
{
for (j = n - 1; j > i; j--)
{
if (arr[j - 1] < arr[j])
{
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
flag = 0;
}
}
if (flag)
{
return;
}
}
}
void DBubbleSort(int arr[], int n)
{
int low = 0, high = n - 1;
int flag = TRUE;
while (low < high && flag)
{
// 标志变量flag设置为假,若没有调整则说明数组有序,停止多余步骤
flag = FALSE;
// 将小的关键字前移
for (int i = high; i > low; i--)
{
if (arr[i - 1] > arr[i])
{
SWAP(arr[i - 1], arr[i]);
flag = TRUE;
}
}
high--;
// 将大的关键字后移
for (int j = low; j < high; j++)
{
if (arr[j] > arr[j + 1])
{
SWAP(arr[j], arr[j + 1]);
flag = TRUE;
}
}
low++;
}
}
// 链表冒泡排序
void LBubbleSort(Linklist *L)
{
int i, j, temp;
Linklist p, q;
for (i = (*L)->data; i > 1; i--)
{
q = (*L)->next;
p = q->next;
for (j = 1; j < i; j++)
{
if (q->data > p->data)
{
temp = p->data;
p->data = q->data;
q->data = temp;
}
q = p;
p = p->next;
}
}
}
// 快速排序划分函数
int Partition(int arr[], int low, int high)
{
int pivot = arr[low];
while (low < high)
{
while (low < high && arr[high] >= pivot)
{
high--;
}
arr[low] = arr[high];
while (low < high && arr[low] <= pivot)
{
low++;
}
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
// 快速排序
void QuickSort(int arr[], int low, int high)
{
if (low < high)
{
int pivotpos = Partition(arr, low, high);
QuickSort(arr, low, pivotpos - 1);
QuickSort(arr, pivotpos + 1, high);
}
}
// 简单选择排序
void SelectSort(int arr[], int n)
{
int i, j, min, temp;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i; j < n - 1; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
}
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
// 链表简单选择排序
void LSelectSort(Linklist *L)
{
Linklist p, cur, min;
int temp;
p = (*L)->next;
for (int i = 1; i < (*L)->data - 1; i++)
{
cur = p;
min = p;
while (cur)
{
if (cur->data < min->data)
{
min = cur;
}
cur = cur->next;
}
temp = min->data;
min->data = p->data;
p->data = temp;
p = p->next;
}
}
// 堆调整函数
void HeapAdjust(int arr[], int k, int n)
{
int parent = k;
int son = 2 * parent + 1;
while (son < n)
{
if (son + 1 < n && arr[son] < arr[son + 1])
{
son++;
}
if (arr[parent] > arr[son])
{
break;
}
else
{
SWAP(arr[parent], arr[son]);
parent = son;
son = 2 * parent + 1;
}
}
}
// 堆排序
void HeapSort(int arr[], int n)
{
for (int i = n / 2; i >= 0; i--)
{
HeapAdjust(arr, i, n); // 建堆
}
SWAP(arr[0], arr[n - 1]);
for (int i = n - 1; i > 1; i--)
{
HeapAdjust(arr, 0, i - 1);
SWAP(arr[0], arr[i - 1]);
}
}
// 归并函数
void Merge(int arr[], int temp[], int low, int mid, int high)
{
int i, j, k;
for (k = low; k <= high; k++)
{
temp[k] = arr[k];
}
for (i = low, k = low, j = mid + 1; i <= mid && j <= high; k++)
{
if (temp[i] <= temp[j])
{
arr[k] = temp[i++];
}
else
{
arr[k] = temp[j++];
}
}
while (i <= mid)
{
arr[k++] = temp[i++];
}
while (j <= high)
{
arr[k++] = temp[j++];
}
}
// 归并排序
void MergeSort(int arr[], int temp[], int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
MergeSort(arr, temp, low, mid);
MergeSort(arr, temp, mid + 1, high);
Merge(arr, temp, low, mid, high);
}
}
// 此函数用于输出数组序列
void showArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
// 此函数用于输出链表序列
void showLinkList(Linklist L)
{
Linklist p = L->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
}
【C语言】排序算法代码
于 2023-11-05 00:10:18 首次发布