Skip to content

Instantly share code, notes, and snippets.

@rubaiyat6370
Last active August 29, 2015 14:20
Show Gist options
  • Save rubaiyat6370/8e32192e9e64dd67e01b to your computer and use it in GitHub Desktop.
Save rubaiyat6370/8e32192e9e64dd67e01b to your computer and use it in GitHub Desktop.
package HeapSort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class MainHeap {
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many input you want? \n:");
int number = input.nextInt();
int [] array = new int[number+1];
array[0]=0;
for(int j=1;j<=number;j++)
array[j]= input.nextInt();
//Heap p = new Heap();
//array = p.Heap(array);
Heap(array);
HeapSort hp = new HeapSort(array);
System.out.println("\n\n");
for(int j=1;j<=number;j++)
System.out.println(array[j]);
//HeapSort(array);
}
public static void Heap(int[] input)
{
for(int i =1;i<input.length;i++)
{
int f = i;
while(input[f]>input[f/2] && f/2!=0)
{
int temp = input[f/2];
input[f/2] = input[f];
input[f] = temp;
f=f/2;
}
}
// HeapSort(input);
}
public static void HeapSort(int[]input)
{
for(int i= input.length-1;i>0;i--)
{
int temp1 = input[i];
input[i] = input[1];
input[1] = temp1;
//Heap(input);
for(int j =1;j<input.length-1;j++)
{
int f = j;
while(input[f]>input[f/2] && f/2!=0)
{
int temp = input[f/2];
input[f/2] = input[f];
input[f] = temp;
f=f/2;
}
}
}
System.out.println("\n\n");
for(int j=1;j<input.length;j++)
System.out.println(input[j]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment