《啊哈!算法》读书笔记--排序(快速排序,冒泡排序)

本文介绍了几种常见的排序算法,包括桶排序、冒泡排序、快速排序,并通过实例展示了这些算法的应用。此外,还讨论了排序算法在不同场景下的使用,如书籍购买排序、学生成绩排序等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

排序

最快最简单的排序—桶排序

给出0-10范围的数组,对其进行排序。
使用桶排序的方法,因为数的范围已经确定了0-10,那么,我们建立一个数组,记录每个数值出现的次数,再按顺序输出就好了。

代码

/*1.桶排序*/
void sort1_0(int nums[], int length);
/*测试代码*/
void test_sort1_0(std::string text_name)
{
    printf("====%s start====\n",text_name.c_str());
    int nums[5] = {2,5,3,5,8};
    sort1_0(nums,5);
    printf("\nend %s====\n", text_name.c_str());
}
int main()
{

    test_sort1_0("sort_01");
    system("pause");
    return 0;
}

void sort1_0(int nums[], int length)
{
    int all_num[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    for (int i = 0; i < length; i++)
    {
        all_num[nums[i]]++;
    }
    //print
    for (int i = 0; i < 11; i++)
    {
        if (all_num[i] > 0)
        {
            for (int j = 0; j < all_num[i]; j++)
            {
                printf("%d", i);
            }
        }
    }
}

冒泡排序

基本思想:每次比较两个相邻的元素,如果他们的顺序错误,就交换他们的位置。

code

/*2.冒泡排序*/
void bubble_sort_01(int nums[], int length);
void test_bubble_sort_01(const char * test_name)
{
    printf("====%s start====\n", test_name);
    int nums[5] = { 2, 5, 3, 5, 8 };
    bubble_sort_01(nums, 5);
    printf("\nend %s====\n", test_name);

}
int main()
{

    //test_sort1_0("sort_01");
    test_bubble_sort_01("bubble_sorle_01");
    system("pause");
    return 0;
}
void bubble_sort_01(int nums[], int length)
{
    for (int i = 0; i < length; i++)
    {
        for (int j = 0; j < length-i; j++)
        {
            if (nums[j] < nums[j + 1])
            {
                int tmp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = tmp;
            }
        }
    }
    for (int i = 0; i < length;i++)
    {
        printf("%d,",nums[i]);
    }
}

冒泡排序的使用2:

struct student_01
{
    std::string name;
    char score;
};//结构体存储姓名和分数
void bubble_sort_02(student_01 students[],int length);
void test_bubble_sort_02(const char * test_name)
{
    printf("====%s start====\n", test_name);
    struct student_01 students[5];
    students[0].name = "a";
    students[0].score = 2;
    students[1].name = "b";
    students[1].score = 5;
    students[2].name = "c";
    students[2].score = 3;
    students[3].name = "d";
    students[3].score = 5;
    students[4].name = "e";
    students[4].score = 8;
    bubble_sort_02(students, 5);
    printf("\nend %s====\n", test_name);
}
int main()
{

    //test_sort1_0("sort_01");
    //test_bubble_sort_01("bubble_sorle_01");
    test_bubble_sort_02("bubble_sorle_02");
    system("pause");
    return 0;
}
void bubble_sort_02(student_01 students[], int length)
{
    for (int i = 0; i < length - 1;i++)
    {
        for (int j = 0; j < length - i; j++)
        {
            if (students[j].score < students[j + 1].score)
            {
                struct student_01 tmp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = tmp;
            }
        }
    }

    for (int i = 0; i < length; i++)
    {
        printf("[%s,%d]", students[i].name.c_str(), students[i].score);
    }
}

快速排序

快速排序,三个重点,参考值,开始位置和结束位置。要先从右往左找,再从左往右找,因为,右边值的大小对左边的值有限定作用。

code

/*4.快速排序*/
int quick_arry[10] = { 4, 3, 5, 7, 9, 10, 2, 1, 6, 8 };
void quick_scort_0(int start, int end);
void test_quick_sort_1(const char * test_name)
{
    printf("====%s start====\n", test_name);
    printf("before sort:\n");
    for (int i = 0; i < 10; i++)
    {
        printf("%d,", quick_arry[i]);
    }
    quick_scort_0(0, 9);
    printf("\nafter sort:\n");
    for (int i = 0; i < 10;i++)
    {
        printf("%d,", quick_arry[i]);
    }
    printf("\nend %s====\n", test_name);
}
int main()
{

    //test_sort1_0("sort_01");
    //test_bubble_sort_01("bubble_sorle_01");
    //test_bubble_sort_02("bubble_sorle_02");
    test_quick_sort_1("quick_sort_01");
    system("pause");
    return 0;
}

void quick_scort_0(int start, int end)
{
    int aim = quick_arry[start];
    if (start > end)
        return;

    int tmp = quick_arry[start];
    int i = start;
    int j = end;
    while (i != j)
    {
        while (quick_arry[j] >= aim && i < j)
            j--;
        while (quick_arry[i]<= aim && i < j)
            i++;
        if (i < j)
        {
            int t = quick_arry[i];
            quick_arry[i] = quick_arry[j];
            quick_arry[j] = t;
        }
    }
    quick_arry[start] = quick_arry[i];
    quick_arry[i] = tmp;
    quick_scort_0(start, i-1);
    quick_scort_0(i + 1, end);
}

小哼买书

code

/*5.小哼买书*/
int book_ISBN[20] = { 1, 2, 2, 3, 1, 11, 22, 33, 44, 11, 22, 2, 33, 55, 66, 897, 234, 1223 ,1112,33234};
void buy_book_0(int start,int end);
void buy_book_0_1();
void buy_book_1();
void test_xhms(const char * test_name)
{
    printf("\n====start %s====\n", test_name);
    printf("\ntime:");
    time_t now;
    now = time(nullptr);
    std::cout << now << std::endl;
    //buy_book_0(0, 19);
    buy_book_1();
    for (int i = 0; i < 20; i++)
    {
        printf("%d,", book_ISBN[i]);
    }
    buy_book_0_1();
    printf("\n");
    time_t now_1;
    now_1 = time(nullptr);
    std::cout << now_1 << std::endl;
    printf("\nend %s====\n", test_name);
}
int main()
{

    //test_sort1_0("sort_01");
    //test_bubble_sort_01("bubble_sorle_01");
    //test_bubble_sort_02("bubble_sorle_02");
    //test_quick_sort_1("quick_sort_01");
    test_xhms("小哼买书");
    system("pause");
    return 0;
}
//快速排序
void buy_book_0(int start, int end)
{
    if (start>end)
    {
        return;
    }
    int aim = book_ISBN[start];
    int left = start;
    int right = end;
    while (left != right)
    {
        while (book_ISBN[right] >= aim && left < right)
            right--;
        while (book_ISBN[left] <= aim && left < right)
            left++;
        if (left < right)
        {
            int t = book_ISBN[left];
            book_ISBN[left] = book_ISBN[right];
            book_ISBN[right] = t;
        }
    }
    book_ISBN[start] = book_ISBN[left];
    book_ISBN[left] = aim;
    buy_book_0(start, left - 1);
    buy_book_0(left + 1, end);
}
void buy_book_0_1()
{
    int result[20];
    //初始化
    for (int i = 0; i < 20; i++)
    {
        result[i] = -1;
    }
    int cur_idx = 0;
    for (int i = 0; i < 20;i++)
    {
        int isbn = book_ISBN[i];
        bool is_have = false;
        int j = 0;
        while (j<cur_idx)
        {
            if (result[j] == isbn)
            {
                is_have = true;
                break;
            }
            j++;
        }
        if (!is_have)
        {
            result[cur_idx] = isbn;
            cur_idx++;
        }
    }
    printf("\n%d\n", cur_idx);
    for (int i = 0; i < cur_idx;i++)
    {
        printf("%d,", result[i]);
    }
}
//冒泡排序
void buy_book_1()
{
    for (int i = 0; i < 20; i++)
    {
        for (int j = 0; j < 20 - i; j++)
        {
            if (book_ISBN[j] > book_ISBN[j+1])
            {
                int t = book_ISBN[j];
                book_ISBN[j] = book_ISBN[j + 1];
                book_ISBN[j + 1] = t;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值