Last active
December 17, 2015 01:28
-
-
Save supermomonga/5528110 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.io.*; | |
class Heap{ | |
private ArrayList<Integer> nodes; | |
private int position; | |
Heap(){ | |
this.nodes = new ArrayList<Integer>(); | |
this.position = 0; | |
} | |
private void replace(int k1, int k2){ | |
Integer tempValue = this.nodes.get(k1); | |
this.nodes.set(k1, this.nodes.get(k2)); | |
this.nodes.set(k2, tempValue); | |
} | |
public void insert(int value){ | |
this.nodes.add(value); | |
this.position++; | |
int k1,k2; | |
if(this.position != 1){ | |
k1 = this.position; | |
k2 = this.position / 2; | |
while(k1 > 1 && this.nodes.get(k1 - 1) < this.nodes.get(k2 - 1)){ | |
this.replace(k1 - 1, k2 - 1); | |
k1 = k2; | |
k2 = k1 / 2; | |
} | |
} | |
} | |
public void insertArray(int[] values){ | |
for(int value : values){ | |
this.insert(value); | |
} | |
} | |
public int take(){ | |
int value = this.nodes.get(0); | |
this.nodes.set(0, this.nodes.get(--this.position)); | |
int k1 = 1, k2 = 2; | |
while( k2 <= this.position){ | |
if( k2 < this.position && this.nodes.get( k2 - 1 ) > this.nodes.get(k2) ){ | |
k2++; | |
} | |
if( this.nodes.get( k1 - 1 ) > this.nodes.get( k2 - 1 ) ){ | |
this.replace(k1 - 1, k2 - 1); | |
} | |
k1 = k2; | |
k2 = k1 * 2; | |
} | |
return value; | |
} | |
public ArrayList<Integer> takeAll(){ | |
ArrayList<Integer> sortedNodes = new ArrayList<Integer>(); | |
for(int i = 0; i < this.nodes.size(); i++){ | |
sortedNodes.add(this.take()); | |
} | |
return sortedNodes; | |
} | |
public String toString(){ | |
return this.nodes.toString(); | |
} | |
} | |
class Ex4{ | |
public static void main(String[] args) throws IOException{ | |
new Ex4().run(); | |
} | |
public void run() throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
ArrayList<Integer> values; | |
while(true){ | |
values = new ArrayList<Integer>(); | |
System.out.println("\n\nPlease input int values sepaleted by comma without any spaces:"); | |
System.out.print(" > "); | |
try{ | |
for(String value : reader.readLine().split(",")){ | |
values.add(Integer.parseInt(value)); | |
} | |
// Convert to int[] for satisfy the specifications...... | |
int[] intvalues = new int[values.size()]; | |
for(int i = 0; i < values.size(); i++){ | |
intvalues[i] = values.get(i); | |
} | |
System.out.println("inputed\t: " + values.toString()); | |
System.out.println("sorted\t: " + this.Heapsort(intvalues).toString()); | |
}catch(Exception e){ | |
System.out.println("\n[ERROR] Invalid input."); | |
System.out.println("Please read the message carefully and see the following example."); | |
System.out.println("EX) > 1,3,5,7,9,2,4,6,8"); | |
} | |
} | |
} | |
public ArrayList<Integer> Heapsort(int[] a){ | |
Heap heap = new Heap(); | |
heap.insertArray(a); | |
return heap.takeAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment