Files
BlogPosts/YueQian/Homework/2-4.md

123 lines
3.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
tags:
- 作业
- C语言
aliases: empty
日期: 2026/2/4
---
# 2-4作业
## 定义一个长度为20的整型数据生成随机数对这个数组进行初始化编写排序函数对这个数据中的数据进行从小到大排序
- 要求编写五种排序函数:冒泡排序、选择排序、插入排序、快速排序、希尔排序
- 提示:[排序算法参考](https://www.cnblogs.com/onepixel/p/7674659.html)
```c
#include <stdio.h>
void bubble_sort(int *arr,int len)
{
    for(int i=0;i<len;i++){
        for(int j=0;j<len-i-1;j++){
            if(arr[j] > arr[j+1]){
                arr[j] ^= arr[j+1];
                arr[j+1] ^= arr[j];
                arr[j] ^= arr[j+1];
            }
        }
    }
}
void insertion_sort(int *arr,int len)
{
    int point=0,temp=0;
    for(int i=1;i<len;i++){
        if(arr[i+1] < arr[i]){
            point = i+1;
            for(int j=point;j>0;j--){
                if(arr[j-1] > arr[j]){
                    temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}
void selection_sort(int *arr, int len)
{
    int min_val,min_index,temp = 0;
    for (int i = 0; i < len - 1; i++){
        min_index = i;
        min_val = arr[i];
        for (int j = i + 1; j < len; j++){
            if (arr[j] < min_val){
                min_val = arr[j];
                min_index = j;
            }
        }
        temp = arr[i];
        arr[i] = min_val;
        arr[min_index] = temp;
    }
}
void quick_sort(int *arr, int left, int right)
{
    if (left > right)
        return;
    int tmp = arr[left];
    int i = left;
    int j = right;
    while (i != j){
        while (arr[j] >= tmp && j > i)
            j--;
        while (arr[i] <= tmp && j > i)
            i++;
        if (j > i){
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
    }
    arr[left] = arr[i];
    arr[i] = tmp;
    quick_sort(arr, left, i - 1);
    quick_sort(arr, i + 1, right);
}
void shell_sort(int *arr, int len)
{
}
int main(int argc, char const *argv[])
{
    int arr[20] = {3,44,28,5,47,36,27,26,89,58,11,31,14,51,54,67,76,68,89,45};
    int len = sizeof(arr)/sizeof(arr[0]);
    //quick_sort(arr,0,len-1);
    for(int i=0;i<len;i++)
        printf("%d ",arr[i]);
    printf("\n");
    return 0;
}
```
## 制作一个图书管理系统,要求:
1. 输入1增加书籍名称可以连续添加多本
2. 输入2删除数书籍名称删除一本或全部删除
3. 输入3修改书籍名称
4. 输入4查找书籍名称模糊查找或精确查找
5. 输入5显示所有书籍
6. 输入0退出系统
只要系统未退出则可以继续重复进行,直至系统退出。
**提示:**`char *book_name[1000]; `// 表示表示最多可存放1000书书名的长度自己设计。
```c
```