Skip to content
This repository was archived by the owner on Dec 19, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Java/heapsort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.*;
class Heap_Sort
{
void printArray(int arr[],int n)
{
//int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Heap_Sort hs = new Heap_Sort();
int arr[] = new int[1000000];
int T = sc.nextInt();
while(T>0)
{
int n = sc.nextInt();
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();

Solution ob=new Solution();
ob.heapSort(arr,n);
hs.printArray(arr,n);
T--;
}
}

}


// } Driver Code Ends



class Solution
{
//Function to build a Heap from array.
void buildHeap(int arr[], int n)
{
// Your code here
for(int i=(n-2)/2;i>=0;i--)
heapify(arr,n,i);
}

//Heapify function to maintain heap property.
void heapify(int arr[], int n, int i)
{
// Your code here
int l=2*i+1;
int r=2*i+2;
int largest=i;

if(l<n && arr[l]>arr[largest]) largest=l;
if(r<n && arr[r]>arr[largest]) largest=r;

if(i!=largest){
int temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
heapify(arr,n,largest);
}
}

//Function to sort an array using Heap Sort.
public void heapSort(int arr[], int n)
{
//code here
buildHeap(arr,n);
for(int i=n-1;i>=0;i--){
//swap first last
int temp=arr[i];
arr[i]=arr[0];
arr[0]=temp;
heapify(arr,i,0);
}
}
}