From 3cd45104287369703c1f5cec1c73f7afb2117b59 Mon Sep 17 00:00:00 2001 From: lwy Date: Wed, 12 Apr 2023 01:56:32 +0800 Subject: [PATCH] add InsertSort.c --- Sort/InsertSort.c | 15 +++++++++++ main.c | 63 ++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 Sort/InsertSort.c diff --git a/Sort/InsertSort.c b/Sort/InsertSort.c new file mode 100644 index 0000000..eadc1cf --- /dev/null +++ b/Sort/InsertSort.c @@ -0,0 +1,15 @@ +#include +//直接插入排序 +void insertSort(int arr[], int n) { + int i, j, temp; + for (i = 1; i < n; i++) { + if (arr[i] < arr[i - 1]) { + temp = arr[i]; + for (j = i - 1; j >= 0 && arr[j] > temp; --j) { + arr[j + 1] = arr[j]; + } + arr[j + 1] = temp; + } + } + printf("Over\n"); +} diff --git a/main.c b/main.c index 22d135d..fdee038 100644 --- a/main.c +++ b/main.c @@ -1,42 +1,53 @@ #include +#include //#include "LinearList/SeqList.c" //#include "LinearList/LinkList.c" //#include "Stack/SeqStack.c" //#include "Stack/LinkStack.c" //#include "Queue/SeqQueue.c" -#include "Queue/LinkQueue.c" +//#include "Queue/LinkQueue.c" +#include "Sort/InsertSort.c" int main() { + int arr[] = { 21, 3, 92, 6, 9}; + + int n = sizeof(arr) / sizeof(arr[0]); + printf("%d\n",n); + insertSort(arr,n); + for (int i = 1; i < n; i++) { + printf("%d ", arr[i]); + } + return 0; // int a =20; // int *p =&a; // printf("%d--",*p); - LinkQueue queue; // 定义一个链队列 - initQueue(&queue); // 初始化队列 - - // 入队操作,向队列中添加元素 - enQueue(&queue, 1); - enQueue(&queue, 2); - enQueue(&queue, 3); - - // 判断队列是否为空 - if (queueEmpty(&queue)) { - printf("Queue is empty\n"); - } else { - printf("Queue is not empty\n"); - } - - // 出队操作,从队列中删除元素 - int x = deQueue(&queue); - printf("Delete element: %d\n", x); - - // 再次判断队列是否为空 - if (queueEmpty(&queue)) { - printf("Queue is empty\n"); - } else { - printf("Queue is not empty\n"); - } +// LinkQueue queue; // 定义一个链队列 +// initQueue(&queue); // 初始化队列 +// +// // 入队操作,向队列中添加元素 +// enQueue(&queue, 1); +// enQueue(&queue, 2); +// enQueue(&queue, 3); +// +// // 判断队列是否为空 +// if (queueEmpty(&queue)) { +// printf("Queue is empty\n"); +// } else { +// printf("Queue is not empty\n"); +// } +// +// // 出队操作,从队列中删除元素 +// int x = deQueue(&queue); +// printf("Delete element: %d\n", x); +// +// // 再次判断队列是否为空 +// if (queueEmpty(&queue)) { +// printf("Queue is empty\n"); +// } else { +// printf("Queue is not empty\n"); +// } //顺序队列 // SeqQueue seqQueue;