- /**
- * @author Kong Yin Lam - NMC Year2 Student - 17/11/2015
- */
- import java.util.*;
- public class HeapSortWithLinkedList {
- public static void main(String[] args) {
- int totalOfelement = 10; // assume the total of elements
- /* --------------------------------------------------------------------*/
- LinkedList list = new LinkedList(); // creat a LinkedList object
- Random rand = new java.util.Random();
- for(int i=0; i<totalOfelement; i++){
- list.addItem(rand.nextInt(100)); // add number into LinkedList
- }
- /* --------------------------------------------------------------------*/
- System.out.print("unsorted :\t");
- list.printAll(); // print a unsorted set of numbers
- /* --------------------------------------------------------------------*/
- System.out.print("\nsorted\t :\t");
- HeapSort hs = new HeapSort(); // creat a HeapSort object
- hs.init(list); // pass linked list for initialization
- hs.startSort(); // start heap sorting
- list.printAll(); // print a sorted set of numbers
- }
- }
- class HeapSort{
- private LinkedList list;
- private int length;
- public HeapSort(){}
- public void init(LinkedList list){
- this.list = list;
- this.length = list.returnLength();
- }
- public void startSort(){
- for(int i=length/2-1; i>=0; i--){
- heapify(i, length);
- }
- for(int i=length-1; i>0 ; i--){
- swap(0, i); // swap the first element(biggest) to the sorted group in each time
- heapify(0, i); // check maxHeap's properties with unsorted group
- }
- }
- public void heapify(int root, int length){
- int left = root*2+1; // left Child -> n*2+1
- int right = root*2+2; // Right Child -> n*2+2
- int maxHeap = root; // assume root is largest
- /* check whether it fulfills the maxHeap's properties */
- maxHeap = left<length && list.returnNodeByIndex(left).returnValue()>list.returnNodeByIndex(maxHeap).returnValue() ? left : maxHeap;
- maxHeap = right<length &&list.returnNodeByIndex(right).returnValue()>list.returnNodeByIndex(maxHeap).returnValue() ? right : maxHeap;
- if(maxHeap!=root){ // It means not fulfill
- swap(root, maxHeap); // swap
- heapify(maxHeap, length); // check again
- }
- }
- public void swap(int i, int j){
- int tmp = list.returnNodeByIndex(i).returnValue();
- list.returnNodeByIndex(i).setValue(list.returnNodeByIndex(j).returnValue());
- list.returnNodeByIndex(j).setValue(tmp);
- }
- }
- class LinkedList{
- private Node head;
- private int length;
- public LinkedList(){}
- public void addItem(int value){
- if(length==0) // It means no element in the list
- head = new Node(value);
- else
- this.returnLastNode().appendNode(new Node(value));
- length++;
- }
- public Node returnLastNode(){
- Node tmp = head;
- while(tmp.returnNext()!=null){
- tmp = tmp.returnNext();
- }
- return tmp;
- }
- public void printAll(){
- Node tmp = head;
- for(int i=0; i<length; i++){
- System.out.print(tmp.returnValue()+"\t");
- tmp=tmp.returnNext();
- }
- }
- public Node returnNodeByIndex(int index){
- Node tmp = head;
- for(int i=0; i<index; i++){
- tmp = tmp.returnNext();
- }
- return tmp;
- }
- public int returnLength(){
- return this.length;
- }
- }
- class Node{
- private Node next;
- private int value;
- public Node(int value){
- next=null;
- this.value = value;
- }
- public void appendNode(Node node){this.next = node;}
- public void setValue(int value){this.value = value;}
- public Node returnNext(){return this.next;}
- public int returnValue(){return this.value;}
- }
2015年11月17日 星期二
[Java] HeapSort with Linked List
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言