I'm new to c++ I have a problem with my code I want to sort an array of string. with selection sort, it works but with heap sort, there is a problem that nothing gets printed out (i also have this problem with merge sort) I think the problem is strcmp(), but IDK how to fix it
#include<iostream>
#include<cstring>
#define MAX_LEN 100
void heapify(char arr[][MAX_LEN], int size, int i);
void heapSort(char arr[][MAX_LEN], int size);
// MAIN
int main (){
char arr[][MAX_LEN] = {"V", "Gorz", "Arta", "BM", "Monster"};
int size = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, size);
// printing array
for(int i = 0; i < 0; i++){
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
//==============================================================
// heapify function: check left and right children and also the parent
// and heapify it
void heapify(char arr[][MAX_LEN], int size, int i){
int largest, l, r;
largest = i;
l = 2 * i;
r = 2 * i + 1;
//left child
if(l < size){
if(std::strcmp(arr[l], arr[largest]) > 0)
largest = l;
//right child
}
if(r < size){
if(std::strcmp(arr[r], arr[largest]) > 0)
largest = r;
//if largest != i
}
if(largest != i){
std::strcpy(arr[largest], arr[i]);
heapify(arr, size, largest);
}
}
//==============================================================
// main heap sort function uses heapify function and then remove element
// one by one and re arrages it and heapify again
void heapSort(char arr[][MAX_LEN], int size){
for(int i = size / 2 - 1; i >= 0; i--){
heapify(arr, size, i);
}
for(int i = size - 1; i > 0; i--){
std::strcpy(arr[0], arr[i]);
heapify(arr, i, 0);
}
}