heap sort
C++
// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i)
{
swap(arr[i], arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
// main function to do heap sort
void heapSort(int arr[], int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i=n-1; i>0; i--)
{
// Move current root to end
swap(arr[0], arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
for (int i=0; i<n; ++i)
cout << arr[i] << " ";
cout << "\n";
}
// Driver program
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
heapSort(arr, n);
cout << "Sorted array is \n";
printArray(arr, n);
}
// @see https://www.youtube.com/watch?v=H5kAcmGOn4Q
function heapify(list, size, index) {
let largest = index;
let left = index * 2 + 1;
let right = left + 1;
if (left < size && list[left] > list[largest]) {
largest = left;
}
if (right < size && list[right] > list[largest]) {
largest = right;
}
if (largest !== index) {
[list[index], list[largest]] = [list[largest], list[index]];
heapify(list, size, largest);
}
return list;
}
function heapsort(list) {
const size = list.length;
let index = ~~(size / 2 - 1);
let last = size - 1;
while (index >= 0) {
heapify(list, size, --index);
}
while (last >= 0) {
[list[0], list[last]] = [list[last], list[0]];
heapify(list, --last, 0);
}
return list;
}
heapsort([4, 7, 2, 6, 4, 1, 8, 3]);// Heap Sort in C
#include <stdio.h>
// Function to swap the the position of two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(int arr[], int n, int i) {
// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
// Swap and continue heapifying if root is not largest
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
// Main function to do heap sort
void heapSort(int arr[], int n) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// Heapify root element to get highest element at root again
heapify(arr, i, 0);
}
}
// Print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main() {
int arr[] = {1, 12, 9, 5, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
}
Also in C++:
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- use of strstr in c++
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- c++ short if
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- variabvles in c++
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- c++ public inheritance not getting protected
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- pyqt connect
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- insert at position in vector c++
- Category
- C++
- Title
- body parser
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- getline not working c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- create copy constructor c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- how to iterate through a map in c++
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- runtime array size c++
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- pair c++
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- get index of value c++
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- lower bound c++ for array in decreasing order
- Category
- C++
- Title
- reference function in c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- length of 2d array c++
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- c++ map find
- Category
- C++