heapsort

Solutions on MaxInterview for heapsort by the best coders in the world

showing results for - "heapsort"
Mona
30 Nov 2016
1#include <iostream>
2 
3using namespace std;
4 
5
6void heapify(int arr[], int n, int i)
7{
8    int largest = i; 
9    int l = 2 * i + 1;
10    int r = 2 * i + 2;
11 
12    
13    if (l < n && arr[l] > arr[largest])
14        largest = l;
15 
16    
17    if (r < n && arr[r] > arr[largest])
18        largest = r;
19 
20    
21    if (largest != i) {
22        swap(arr[i], arr[largest]);
23 
24    
25        heapify(arr, n, largest);
26    }
27}
28 
29
30void heapSort(int arr[], int n)
31{
32
33    for (int i = n / 2 - 1; i >= 0; i--)
34        heapify(arr, n, i);
35 
36
37    for (int i = n - 1; i > 0; i--) {
38       
39        swap(arr[0], arr[i]);
40        heapify(arr, i, 0);
41    }
42}
43 
44
45void printArray(int arr[], int n)
46{
47    for (int i = 0; i < n; ++i)
48        cout << arr[i] << " ";
49    cout << "\n";
50}
51 
52
53int main()
54{
55    int arr[] = { 12, 11, 13, 5, 6, 7 };
56    int n = sizeof(arr) / sizeof(arr[0]);
57 
58    heapSort(arr, n);
59 
60    cout << "Sorted array is \n";
61    printArray(arr, n);
62}
Francesca
23 Aug 2019
1// Heap Sort in C
2  
3  #include <stdio.h>
4  
5  // Function to swap the the position of two elements
6  void swap(int *a, int *b) {
7    int temp = *a;
8    *a = *b;
9    *b = temp;
10  }
11  
12  void heapify(int arr[], int n, int i) {
13    // Find largest among root, left child and right child
14    int largest = i;
15    int left = 2 * i + 1;
16    int right = 2 * i + 2;
17  
18    if (left < n && arr[left] > arr[largest])
19      largest = left;
20  
21    if (right < n && arr[right] > arr[largest])
22      largest = right;
23  
24    // Swap and continue heapifying if root is not largest
25    if (largest != i) {
26      swap(&arr[i], &arr[largest]);
27      heapify(arr, n, largest);
28    }
29  }
30  
31  // Main function to do heap sort
32  void heapSort(int arr[], int n) {
33    // Build max heap
34    for (int i = n / 2 - 1; i >= 0; i--)
35      heapify(arr, n, i);
36  
37    // Heap sort
38    for (int i = n - 1; i >= 0; i--) {
39      swap(&arr[0], &arr[i]);
40  
41      // Heapify root element to get highest element at root again
42      heapify(arr, i, 0);
43    }
44  }
45  
46  // Print an array
47  void printArray(int arr[], int n) {
48    for (int i = 0; i < n; ++i)
49      printf("%d ", arr[i]);
50    printf("\n");
51  }
52  
53  // Driver code
54  int main() {
55    int arr[] = {1, 12, 9, 5, 6, 10};
56    int n = sizeof(arr) / sizeof(arr[0]);
57  
58    heapSort(arr, n);
59  
60    printf("Sorted array is \n");
61    printArray(arr, n);
62  }
Delilah
17 Jul 2019
1// @see https://www.youtube.com/watch?v=H5kAcmGOn4Q
2
3function heapify(list, size, index) {
4    let largest = index;
5    let left = index * 2 + 1;
6    let right = left + 1;
7    if (left < size && list[left] > list[largest]) {
8        largest = left;
9    }
10    if (right < size && list[right] > list[largest]) {
11        largest = right;
12    }
13    if (largest !== index) {
14        [list[index], list[largest]] = [list[largest], list[index]];
15        heapify(list, size, largest);
16    }
17    return list;
18}
19
20function heapsort(list) {
21    const size = list.length;
22    let index = ~~(size / 2 - 1);
23    let last = size - 1;
24    while (index >= 0) {
25        heapify(list, size, --index);
26    }
27    while (last >= 0) {
28        [list[0], list[last]] = [list[last], list[0]];
29        heapify(list, --last, 0);
30    }
31    return list;
32}
33
34heapsort([4, 7, 2, 6, 4, 1, 8, 3]);
Leandro
22 May 2018
1Implementation of heap sort in C++:
2
3#include <bits/stdc++.h>
4using namespace std;
5
6// To heapify a subtree rooted with node i which is
7// Heapify:- A process which helps regaining heap properties in tree after removal 
8void heapify(int A[], int n, int i)
9{
10   int largest = i; // Initialize largest as root
11   int left_child = 2 * i + 1; // left = 2*i + 1
12   int right_child = 2 * i + 2; // right = 2*i + 2
13
14   // If left child is larger than root
15   if (left_child < n && A[left_child] > A[largest])
16       largest = left_child;
17
18   // If right child is larger than largest so far
19   if (right_child < n && A[right_child] > A[largest])
20       largest = right_child;
21
22   // If largest is not root
23   if (largest != i) {
24       swap(A[i], A[largest]);
25
26       // Recursively heapify the affected sub-tree
27       heapify(A, n, largest);
28   }
29}
30
31// main function to do heap sort
32void heap_sort(int A[], int n)
33{
34   // Build heap (rearrange array)
35   for (int i = n / 2 - 1; i >= 0; i--)
36       heapify(A, n, i);
37
38   // One by one extract an element from heap
39   for (int i = n - 1; i >= 0; i--) {
40       // Move current root to end
41       swap(A[0], A[i]);
42
43       // call max heapify on the reduced heap
44       heapify(A, i, 0);
45   }
46}
47
48/* A  function to print sorted Array */
49void printArray(int A[], int n)
50{
51   for (int i = 0; i < n; ++i)
52       cout << A[i] << " ";
53   cout << "\n";
54}
55
56// Driver program
57int main()
58{
59   int A[] = { 22, 19, 3, 25, 26, 7 }; // array to be sorted
60   int n = sizeof(A) / sizeof(A[0]); // n is size of array
61
62   heap_sort(A, n);
63
64   cout << "Sorted array is \n";
65   printArray(A, n);
66}
Eleonora
24 Jan 2018
1void heapify(int arr[], int n, int i) {
2  // Find largest among root, left child and right child
3  int largest = i;
4  int left = 2 * i + 1;
5  int right = 2 * i + 2;
6
7  if (left < n && arr[left] > arr[largest])
8    largest = left;
9
10  if (right < n && arr[right] > arr[largest])
11    largest = right;
12
13    // Swap and continue heapifying if root is not largest
14    if (largest != i) {
15      swap(&arr[i], &arr[largest]);
16      heapify(arr, n, largest);
17  }
18}
Lenzo
24 Sep 2019
1#include <iostream>
2
3using namespace std;
4void swap(int*,int*);
5void heapify(int arr[],int n,int index)
6{
7    int left=2*index+1;
8    int right=left+1;
9    int max=index;
10    if(left<n&&arr[left]>arr[max])
11    {
12        max=left;
13    }
14    if(right<n&&arr[right]>arr[max])
15    {
16        max=right;
17    }
18    if(index!=max)
19    {
20        swap(&arr[index],&arr[max]);
21        heapify(arr,n,max);
22    }
23}
24void buildheap(int arr[],int n)
25{
26    for(int i=(n/2);i>=0;i--)
27    {
28        heapify(arr,n,i);
29    }
30}
31
32void heapsort(int arr[],int n)
33{
34    buildheap(arr,n);
35    int l=n-1;
36    while(l>0)
37    {
38        swap(&arr[0],&arr[l]);
39        l--;
40        n--;
41        heapify(arr,n,0);
42    }
43}
44void disp(int arr[],int n)
45{
46    for(int i=0;i<n;i++)
47    {
48        cout<<arr[i]<<" ";
49    }
50    cout<<endl;
51}
52
53int main()
54{
55    int n;
56    cout<<"enter the size of the array:"<<endl;
57    cin>>n;
58    int a[n];
59
60    cout<<"enter the elements of the array:"<<endl;
61    for(int i=0;i<n;i++)
62    {
63        cin>>a[i];
64    }
65    cout<<"array before sorting:"<<endl;
66    for(int i=0;i<n;i++)
67    {
68        cout<<a[i]<<" ";
69    }
70    cout<<endl;
71    //buildheap(a,n);
72    //disp(a,n);
73    cout<<"array after sorting:"<<endl;
74    heapsort(a,n);
75    disp(a,n);
76    return 0;
77}
78void swap(int*a,int*b)
79{
80    int temp=*a;
81    *a=*b;
82    *b=temp;
83}
84
queries leading to this page
wap to sort integers in an ascending order using heap sort using recursion in cheap sort and heap structureheap sort on an arrayheap data structure implementation in cppheapsort in cheapsort geekheap sorting implementation in cheap sort using arrayheapsort o notationheap sort used 3fheapsort jheap sort algotithmheapsort javaheapify in cheapwhy need of heap sortwhat is the datastructure for heap sortwhen is heap sort usedarray heap sorton which algorithm is heap sort based on 3fwhere do use heapsorthow much time heap sort will take to sort n 2f10 elements heap sorywhy is heap sort in place 3fin place heap sotyheap sort complexityheap sort and its analysisiheapsort algorithm exampleheapsort java solutionheap sort algorithmsort with heap sortheaps algorithmheap 27s algorithmsteps of heap sort methodheaps implementationheapsort analysisheap sort program in c with time complexityis heap sort stab 3behow to do heap sort heap sort using techniqueheap sort write a c 2b 2b programto find the heap sortheapsterselection sort for heapheap sort is implementation ofrecursive heapsortheapsort in library of c 2b 2bheapsort csorting a binary heapheap and heap sortgeeksforgeeks heapsorthow heap sort example heap sortingheap sort in data structurewho invented heapsortheap and heapsorthow does heap sort workheap sort 27heap sort code c 2b 2bwhen we use heap sortheap sorterheap e2 80 99s algorithmmax heap sort javabinary heap sortheap sort analysisheap sort based onheap queueimplementation of heap sortheap osrtheap array implementation c 2b 2bheap sort in cheap programminheap sort in depthheapsort c 2b 2b example2 constructing the heap for the list 11 2c 51 2c 42 2c 35 2c 23 2c 45 2c 32 a bottom up b top down c array representation and sort it 28write out the steps in both of the two stages 29min heapsorttime complexity of heap sortin place heap sortdefine heap sorthow to sort the heapheap sort flyod algorithmwhen was heap sort createdheapsort stlheap bottom upheap sort algorithmwhich heap does heap sort algorithm useafter performing heapsort will the array also be called as heapthe first step of heap sort is toheap methodsheap sort arrayheap sort examplesdefine 2c discuss 2c explain the heapsort algorithmheap gfgfmax heap c codec 2b 2b heap sortin place heap sort algorithm using an array based heap implement heap sortheap sortsort heap largest to smallestheap sort in stlthe heap sort method has a worst case complexity function that is insort the array in ascending order by using max heap sort techniqueheapsort stablealgorithm for heap sort exampleheap sort program in cheap soerheap sort factsheap sort programheap sort algorithm exampleexplain heap sort on the array heap overflow c 2b 2bwhat is heapsortcworst heapsortheap sort practice problems geeksforgeeksheap to implementwhen using heap sortheapsortsort codeheap sort itsheapify cprogramiz heap sortc 2b 2b program implement the heap sort algorithmheap sort uses stack memorymin heap orderheap sort in dsheapsort heap sortin heap sort algorithm 2c to remove the maximum element every time 2ccode heap sort c 2b 2bmax heapify algorithmheapsort algorithm max heapc 2b 2b heap corruptionheapsort aufwandmax heapify codeheap sort dry run examplewhat is heap sort 3fheapsort diagram c 2b 2bheap sort minmax heap and sortheapsort max heapis heap sort goodheap sort in javaheap sort in cppsort heap c 2b 2bheap sort propertiesbubble sort in c 2b 2bhow to order heapimplementation of heap sort heap sort complexicyheapsort algorithm in c 2b 2bheap sorting for array in cheap sort c 2b 2bare heaps sortedheap in c 2b 2bheap sorting for arrayheap memorycreate an array on the heap c 2b 2bheap tree programin cppheapsort ascending orderheapsort with all steps sort in javaheap sort sorted arrayis heap sort in place 3fheapsort max heap pytjonuse of sort heap function in c 2b 2bheapify function in c 2b 2bfor the following sequence 3c16 14 15 10 12 27 28 3e 2c apply the heapify 28max heap or min heap 29 find the total number of heapify procedure at the root write a program to heapsort algorithm to sort an integer array in ascendingheap sort theory and pseudocodewhat is heap in c 2b 2bwhy use heap sortheapsort implementa c3 a7 c3 a3owrite a c program to construct a min heap with n elements 2c then display the elements in the sorted order conitions for heapify in heapsortarray sort using max heapthe technique used in heap sort order of data items in a heap treeheap sort out place sorting algorithmheapsort c 2b 2bheap sort dryrunmax heapify algorithmheap sort using stackheap sprtsort the array 5b10 2c3 2c5 2c1 2c4 2c2 2c7 2c8 5d using heap sorting algorithm show all the steps in figures in design and analysis of algorithm24 if you were given the array below 2c and used the procedure we learned in class to build a heap from the array in place 28i e step 1 of heapsort 29 2c what would the resulting heapified array look like 3fheap and sortheap sort methodheapsort implementationsorting min heap complexitypython heapsortpython max heap sortdifferent methods to sort binary heapwhat is heapsorting element using selectin sort whatwill be the child nodes of a node when the max heap is constructedhow does heap sort work 3f implement heap sort for the given array heapsort heaphow to do a heap sortheap sotheap sort c 2b 2b codewrite an algorithm to explain the concept of heap sortheap sort inbuilt function in c 2b 2bheapsort algorithm python geeks for geeksheapifi codeheap sort complexityheap sort explainedheap sort graphheap sortingheap sorheapsort complexityheap implementationanalysis of heap sortwhy heap sortheap sort dasheapify codewhat does heap sort doheap sort program in c 2bheapsort is used to sort a list of 8 integers in an array and few heapify method has been used after that the array is now as follows 3a16 14 15 10 12 27 28heap sort implementation in c 2b 2b using arrayheap sort practiceheapify algorithm pythonheap sort space complexityheap sort c 2b 2b stringheapsort heapifysort using heapifycreate max heap for following sequence 3c12 2c 18 2c 15 2c 20 2c 7 2c 9 2c 2 2c 6 2c 5 2c 3 3e then perform heap sort for the following sequence show all the steps of insertion 2c deletion and sorting 2c and analyse the running time complexity for heap sort java heapsort left rightheap sort is implementation isheapsort arrayheap sort jsheap sort using max heapheap sort geeksforgeeksheap sort of arrayheapsort stable 3fheap sort ohepa sort pythonheap sort definitionheap sort 3fheapsort pythonhow to implement heap sortheapsort how it worksinbuit heap sort in c 2b 2bheap sort 28 29heap sort max heapifyheap sort algorithm in javaheap sort clrsc 2b 2b heapheap sort using max heap pythonheap sort stlheap sort theory and sudo codeheap sort 12 2c 34 2c 3 2c 4 2c 8 2c 1 2c 2 2c 9 2c 11 2c 20 2c 7create a max heap and then sort cthe first step of heap sort is to 3aarray heap or stackheapsort cppfor the implementation of heap sort discussed in class 2c the worst case complexity for sorting an array of size n isopci c3 b3n c3 banica java heapsortheapsort jspyramid sort javaheapify code robert sidhow heapsort worksheapsort algorithm to sort an integer array in ascendingcomplexity of an sorting an array using the heapinplace heap sortheap sort pythonheap sort algorithm explainedsort the array 5b10 2c3 2c5 2c1 2c4 2c2 2c7 2c8 5d using heap sorting algorithm show all the steps in figures heap structureheapsort codeheap sort algorithamhow to implement heap sort in c 2b 2bheapify javaheap sort codinghaeap sort using mean and max heaphow does heapsort workhow to heap sortsort the following array using heap sort technique 3a 7b5 2c13 2c2 2c25 2c7 2c17 2c20 2c8 2c4 7d discuss the time complexity of heap sort algorithm make heap cpp 29 heap sortheap sort stepsmax heap csort the keys 66 2c 99 2c 23 2c 12 2c 98 2c 09 2c 45 2c 29 2c 31 2c 20 in ascending order using the heap sort also write the algorithmheap sort given algorithm 27heap sort visulsort struct using heapsortclement heap sortheap algorithm cheapsort thtaheap sort c codeheapsort algorithm timehow to do heap sort in javac 2b 2b program to implement the functions of heapheap sort explanationheap sortthow to sort a heapsort heap c 2b 2b stl timeheap sort vs quick sortwhat is heap sortingwhat is heapify in heap sorthow heap sort workhow to find out last element used in max heap sorthow to illustrate the heap sort in c 2b 2bheap sort cppheap sort heapifywrite an algorithm for heapsortheap sort inplacesolve heap sort step by stepheapsort gheap sort in c 2b 2bheapsort traceheap in algorithmtrees short question bst heap sortexplain heap sort with its algorithmmax heap sort algoritm using treesortruntime heapsortderivation of running times for heapsorthead sortheapsort c 2b 2b stlheap dsheap sort algorithm tutorialheap reviewsjasva heap algorithmheapsort programizdoes heap sort sort least to greatestheap sort time complexityheapsort algorithmheap sort in c 2b 2b geekforgeekwhat is heap orderdoes heap sort work on non omcplete treeheap sort with binarywrite a program to sort an array using heap sort with build max heap 28 29 and heapify 28 29 as sub functions ques10heapq sort arrayheapify function gfgheap sort in place explainedbuild a heap sortwhat is the time complexity to compute a median of heapsortpyramid sortdefine 2c discuss 2c explain and implement the heapsort algorithmheap sort javaheap sort is based onheap sort in placeimplement heap sort algorithmheap sorting oheapsort algorithm on input array using max heapheap sort from do wayexplain heapsortheap sort using max heap in cheapsort explainedheapsort example using c 2b 2bheapsort in c 2b 2b referencemax heap sort algorithmheap sort max heaphapify heapsort javaheap soretheapsort techniquesort the array 5b10 2c3 2c5 2c1 2c4 2c2 2c7 2c8 5d using heap sorting algorithm show all the steps in figuresheap orderheapsort c 2b 2b implementationheap sort programizon which algorithm is heap sort based onsort the set of numbers 22 2c 15 2c 88 2c 46 2c 35 2c 44 in increasing order with heap sort using a different array to store the heap tree perform heap sort given data 12 2c 34 2c 3 2c 4 2c 8 2c 1 2c 2 2c 9 2c 11 2c 20 2c 7c 2b 2b heapsort explainedheap sort is based on which algorithmheap sort implementation using c 2b 2bhow to perform sorting using heap sortheap isort complexityheap sort example and analysisheap sort invariantheap sort pseudocodeheap sorting algorithmmax heao algorithmheapsort is an algorithm that takes what time 3fheap sort applicationheap sort examples to runheap sort in c 2b 2b 2bheap sort optimizationheapsort takssheap sort cheapify pheso codeheap sort pseheap sort runtimeheap sort programmizheapsort from array diagram c 2b 2bheap sotsheap sort program in c 2b 2bheapsort in c 2b 2bheap algorithmis heapifying an array a heapsortheap algorityhmjava heap sortheapsort programheap sort in place sorting algorithmsort using heap sortc 2b 2b heap sort insertingfirst step of heap sort is to remove the largest element from the heapheap sort algorhtimon which algorithm is heap sort based on 3fheap implementation in c 2b 2b using arrayheap sort is tecniqueheap sort in office line data structure minimum costheap sort with build heap c 2b 2bhow heap sort worksheap sort using heapifywhat is the time complexity of heap sort 3fheap sort treealgorithm of heap sortheap sort with exampleheapsort in placealgorithm for heapsorttypical running time of a heap sort algorithmsort heapmax heap sortwrite the heap sort algorithm how to heap sort an arrayheap sort tutorialheap sort in pythonheap sort heapify and max heapheapsort in data structureheap sort heapify meaningheap sort exampleheap sort code explainedheap sort with priority algorithm c 2b 2b implementationshort using heap sort heap sort java codeheap sort implementationwrite a program to c 2b 2b program to sort the data using heap sort heapsort definitionheap meaningheap sort tree foralgorithm for heap sortheapsort vs quicksort geeksforgeeksheapsort runtimehow to do heap sort algorithmheap sorrtheap sort theoryheap sort using binary treearrange following data in descending order using heap sort 3a 6 2c 8 2c 5 2c 6 2c 8 2c 7 2c 10 2c 12 2c 3 heap sort complexitywhat is heap sortheapsort demomin heap sortfar right node in min heapheap sort algorithm in data structureheap sort explanation in cheap sort c 2b 2b stldown heap algorithmheap sort codepython heap sortheapsort max arraywhen should you use heap sort javacpp algorithm heapsortalgorithm for heap sort in cheap sort with stepsheapify algorithmexplain how heap sort worksheap sort is an implementation ofcode for heapifyan array of elements can be sorted using the heap sort algorithm the first step of the heap sort algorithm is to convert the array into a maximum heap if the following array is to be sorted 3aheapsort vorsortiertheapsort