Sorting/Heap Sort
comparisonin-placeunstable
Press play to start
1void heapify(int* A, int n, int i) {
2 int maior=i, esq=2*i+1, dir=2*i+2, tmp;
3 if(esq<n && A[esq]>A[maior]) maior=esq;
4 if(dir<n && A[dir]>A[maior]) maior=dir;
5 if(maior!=i){
6 tmp=A[i]; A[i]=A[maior]; A[maior]=tmp;
7 heapify(A,n,maior);
8 }
9}
10void heapSort(int* A, int n) {
11 for(int i=n/2-1;i>=0;i--) heapify(A,n,i);
12 for(int i=n-1;i>0;i--){
13 int tmp=A[0]; A[0]=A[i]; A[i]=tmp;
14 heapify(A,i,0);
15 }
16}
Step 1/0

Practice

LeetCode·#215 Kth Largest ElementMediumHackerRank·Find the Running MedianHard
BestO(n log n)
AverageO(n log n)
WorstO(n log n)
SpaceO(1)