review heap sort

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

void swap(vector<int> & arr, int first, int second)
{
    int temp = arr[second];
    arr[second] = arr[first];
    arr[first] = temp;
}

void print_vec(vector<int> vec)
{
    for (auto it = vec.begin(); it != vec.end(); it++)
    {
        printf("%d ", *it);
    }
    printf("\n");
}

//[start, end]
void build_heap(vector<int>& arr, int start, int end)
{
    int size = end - start;
    if (size > 0)
    {
        for (int parent_index = size / 2 - 1; parent_index >= 0; parent_index--)
        {
            int left_child_index = 2 * parent_index + 1;
            int right_child_index = 2 * parent_index + 2;
            int larger_child_index = left_child_index;
            if (right_child_index <= end)
            {
                larger_child_index = (arr[right_child_index] > arr[left_child_index]) ? right_child_index : left_child_index;
            }
            if (arr[parent_index] < arr[larger_child_index])
            {
                swap(arr, parent_index, larger_child_index);
            }
        }
    }
}

void sift_down(vector<int>& arr, int sift_index, int last_index)
{
    int parent_index = sift_index;
    while (parent_index >= 0 && parent_index <= last_index)
    {
        int larger_child_index = -1;
        int left_child_index = 2 * parent_index + 1;
        if (left_child_index <= last_index)
        {
            larger_child_index = left_child_index;
            int right_child_index = 2 * parent_index + 2;
            if (right_child_index <= last_index)
            {
                larger_child_index = (arr[right_child_index] > arr[left_child_index]) ? right_child_index : left_child_index;
            }
            if (larger_child_index >= 0 && arr[parent_index] < arr[larger_child_index])
            {
                swap(arr, parent_index, larger_child_index);
                parent_index = larger_child_index;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}


void heap_sort(vector<int> & arr)
{
    int size = arr.size();
    if (size > 0)
    {
        //1.build max heap
        build_heap(arr, 0, size - 1);
        print_vec(arr);
        //2.swap and sift
        swap(arr, 0, size-1);
        for (int sort_size = size - 1; sort_size > 0; sort_size--)
        {
            sift_down(arr, 0, sort_size - 1);
            swap(arr, 0, sort_size - 1);
        }
    }
}

int main(int argc, char * * argv, char * * env)
{
    vector<int> vec = { 8, 5, 7, 2, 9 , 1};
    print_vec(vec);
    heap_sort(vec);
    print_vec(vec);
    char ch;
    scanf_s("%c", &ch);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值