Remove unused parameter in heap sort example

This commit is contained in:
Shaun Reed 2021-05-26 09:52:26 -04:00
parent ef5f952519
commit c53277b97c
3 changed files with 6 additions and 6 deletions

View File

@ -18,7 +18,6 @@ int main(const int argc, const char * argv[])
{ {
srand(time(nullptr)); srand(time(nullptr));
std::vector<int> array(ARRAY_LENGTH); std::vector<int> array(ARRAY_LENGTH);
// array[0] = 0; // Don't use the 0 index
std::generate(array.begin(), array.end(), [](){ return rand() % 1000;}); std::generate(array.begin(), array.end(), [](){ return rand() % 1000;});
auto print = [](std::vector<int> array) { auto print = [](std::vector<int> array) {

View File

@ -17,7 +17,7 @@ size_t Parent(const size_t &index) { return index / 2;}
size_t Left(const size_t &index) { return 2 * index + 1;} size_t Left(const size_t &index) { return 2 * index + 1;}
size_t Right(const size_t &index) { return (2 * index) + 2;} size_t Right(const size_t &index) { return (2 * index) + 2;}
void MaxHeapify(std::vector<int> &array, size_t thisIndex, int &heapSize) void MaxHeapify(std::vector<int> &array, size_t thisIndex, const int &heapSize)
{ {
// Get an index for the left and right nodes attached to thisIndex // Get an index for the left and right nodes attached to thisIndex
size_t l = Left(thisIndex); size_t l = Left(thisIndex);
@ -42,8 +42,9 @@ void MaxHeapify(std::vector<int> &array, size_t thisIndex, int &heapSize)
} }
} }
void BuildMaxHeap(std::vector<int> &array, int &heapSize) void BuildMaxHeap(std::vector<int> &array)
{ {
int heapSize = array.size();
// For each value within the heap, starting at last index moving to the first // For each value within the heap, starting at last index moving to the first
for (int i = (array.size() / 2); i >= 0; i--) { for (int i = (array.size() / 2); i >= 0; i--) {
// Call MaxHeapify, sorting the value held at index i // Call MaxHeapify, sorting the value held at index i
@ -58,7 +59,7 @@ void HeapSort(std::vector<int> &array)
// + Pass this value by reference to track size of heap as modifiable value // + Pass this value by reference to track size of heap as modifiable value
int heapSize = array.size(); int heapSize = array.size();
// Create a maximum heap from the array using its size // Create a maximum heap from the array using its size
BuildMaxHeap(array, heapSize); BuildMaxHeap(array);
// For each value within the max heap, starting from its final value // For each value within the max heap, starting from its final value
for (int i = array.size() - 1; i > 0; i--) { for (int i = array.size() - 1; i > 0; i--) {
// Swap the top value within the heap with the value at i // Swap the top value within the heap with the value at i

View File

@ -18,9 +18,9 @@ size_t Parent(const size_t &index);
size_t Left(const size_t &index); size_t Left(const size_t &index);
size_t Right(const size_t &index); size_t Right(const size_t &index);
void MaxHeapify(std::vector<int> &array, size_t thisIndex, int &heapSize); void MaxHeapify(std::vector<int> &array, size_t thisIndex, const int &heapSize);
void BuildMaxHeap(std::vector<int> &array, int &heapSize); void BuildMaxHeap(std::vector<int> &array);
void HeapSort(std::vector<int> &array); void HeapSort(std::vector<int> &array);