You are on page 1of 15

TERM PAPER

CSE-321

Topic:-GUI Sorting Technique: Heap Sort

Submitted To:
Mr. Sandeep Singh Dept. Of computer science

Submitted By:
Randhir Kumar Singh Section:-KE007 Roll No.-RKE007A06 Reg. No.-10902551

Heapsort is a comparison-based sorting algorithm to create a sorted array (or list), and is part of the selection sort family. Although somewhat slower in practice on most machines than a wellimplemented quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime. Heapsort is an in-place algorithm, but is not a stable sort. Heapsort is a two step algorithm. The first step is to build a heap out of the data. The second step begins with removing the largest element from the heap. We insert the removed element into the sorted array. For the first element, this would be position 0 of the array. Next we reconstruct the heap and remove the next largest item, and insert it into the array. After we have removed all the objects from the heap, we have a sorted array. We can vary the direction of the sorted elements by choosing a min-heap or max-heap in step one. Heapsort can be performed in place. The array can be split into two parts, the sorted array and the heap. The storage of heaps as arrays is diagrammed at Binary heap#Heap implementation.The heap's invariant is preserved after each extraction, so the only cost is that of extraction.

Comparison with other sorts


Heapsort primarily competes with quicksort, another very efficient general purpose nearly-in-place comparison-based sort algorithm. Quicksort is typically somewhat faster due to better cache behavior and other factors, but the worst2 case running time for quicksort is O (n ), which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk. See quicksort for a detailed discussion of this problem and possible solutions. Thus, because of the O(n log n) upper bound on heapsort's running time and constant upper bound on its auxiliary storage, embedded systems with real-time constraints or systems concerned with security often use heapsort. Heapsort also competes with merge sort, which has the same time bounds. Merge sort requires (n) auxiliary space, but heapsort requires only a constant amount. Heapsort typically runs faster in practice on machines with small or slow data caches. On the other hand, merge sort has several advantages over heapsort: Like quicksort, merge sort on arrays has considerably better data cache performance, often outperforming heapsort on modern desktop computers because merge sort frequently accesses contiguous memory locations (good locality of reference); heapsort references are spread throughout the heap. Heapsort is not a stable sort; merge sort is stable. Merge sort parallelizes well and can achieve close to linear speedup with a trivial implementation; heapsort is not an obvious candidate for a parallel algorithm. Merge sort can be adapted to operate on linked lists with O(1) extra space. Heapsort can be adapted to operate on doubly linked lists with only O(1) extra space overhead. Merge sort is used in external sorting; heapsort is not. Locality of reference is the issue.

Introsort is an interesting alternative to heapsort that combines quicksort and heapsort to retain advantages of both: worst case speed of heapsort and average speed of quicksort.

Example
Let { 6, 5, 3, 1, 8, 7, 2, 4 } be the list that we want to sort from the smallest to the largest. (NOTE, for 'Building the Heap' step: Larger nodes don't stay below smaller node parents. They are swapped with parents, and then recursively checked if another swap is needed, to keep larger numbers above smaller numbers on the heap binary tree.) 1. Build the heap Heap newly added element swap elements

Null

6, 5

6, 5, 3

6, 5, 3,1

6, 5, 3, 1, 8

5, 8

6, 8, 3, 1, 5

6, 8

8, 6, 3, 1, 5

8, 6, 3, 1, 5, 7

3, 7

8, 6, 7, 1, 5, 3

8, 6, 7, 1, 5, 3, 2

8, 6, 7, 1, 5, 3, 2, 4

1, 4

8, 6, 7, 4, 5, 3, 2, 1

2. Sorting. swap elements delete element

Heap

sorted array

details

8, 6, 7, 4, 5, 3, 8, 1 2, 1

swap 8 and 1 in order to delete 8 from heap

1, 6, 7, 4, 5, 3, 2, 8

delete 8 from heap and add to sorted array

1, 6, 7, 4, 5, 3, 1, 7 2

swap 1 and 7 as they are not in order in the heap

7, 6, 1, 4, 5, 3, 1, 3 2

swap 1 and 3 as they are not in order in the heap

7, 6, 3, 4, 5, 1, 2

7, 2

swap 7 and 2 in order to delete 7 from heap

2, 6, 3, 4, 5, 1, 7

delete 7 from heap and add to sorted array

2, 6, 3, 4, 5, 1

2, 6

7, 8

swap 2 and 6 as they are not in order in the heap

6, 2, 3, 4, 5, 1

2, 5

7, 8

swap 2 and 5 as they are not in order in the heap

6, 5, 3, 4, 2, 1

6, 1

7, 8

swap 6 and 1 in order to delete 6 from

heap

1, 5, 3, 4, 2, 6

7, 8

delete 6 from heap and add to sorted array

1, 5, 3, 4, 2

1, 5

6, 7, 8

swap 1 and 5 as they are not in order in the heap

5, 1, 3, 4, 2

1, 4

6, 7, 8

swap 1 and 4 as they are not in order in the heap

5, 4, 3, 1, 2

5, 2

6, 7, 8

swap 5 and 2 in order to delete 5 from heap

2, 4, 3, 1, 5

6, 7, 8

delete 5 from heap and add to sorted array

2, 4, 3, 1

2, 4

5, 6, 7, 8

swap 2 and 4 as they are not in order in the heap

4, 2, 3, 1

4, 1

5, 6, 7, 8

swap 4 and 1 in order to delete 4 from heap

1, 2, 3, 4

5, 6, 7, 8

delete 4 from heap and add to sorted array

1, 2, 3

1, 3

4, 5, 6, 7, 8

swap 1 and 3 as they are not in order in the heap

3, 2, 1

3, 1

4, 5, 6, 7, 8

swap 3 and 1 in order to delete 3 from heap

1, 2, 3

4, 5, 6, 7, 8

delete 3 from heap and add to sorted array

1, 2

1, 2

3, 4, 5, 6, 7, 8

swap 1 and 2 as they are not in order in the heap

2, 1

2, 1

3, 4, 5, 6, 7, 8

swap 2 and 1 in order to delete 2 from heap

1, 2

3, 4, 5, 6, 7, 8

delete 2 from heap and add to sorted array

2, 3, 4, 5, 6, 7, 8

delete 1 from heap and add to sorted array

1, 2, 3, 4, 5, 6, 7, 8

completed

CODE:

import java.awt.*; import java.applet.*; import java.io.*; import java.util.Scanner; class heapsort , public static void main(String args*+)throws IOException , Scanner s=new Scanner(System.in); int ch; int tree*+=new int*50+; int tree1*+=new int*50+; int n=0,ptr,par,item,n1=0; while(true)

, System.out.println("\n\n1. Insert into heap"); System.out.println("2. print heap element into descending order"); System.out.println("3. print heap element into ascending order"); System.out.println("4. Delete element from heap"); System.out.println("5. Exit"); System.out.print("Enter your choice : "); ch=s.nextInt(); switch(ch) , case 1: , System.out.print("Enter a no. = "); item=s.nextInt();

n=n+1; ptr=n; while(ptr>1) , par=ptr/2; if(item<=tree*par+) , tree*ptr+=item; break; tree*ptr+=tree*par+; ptr=par; tree*ptr+=item;

n1=n1+1;

ptr=n1; while(ptr>1) , par=ptr/2; if(item>=tree1*par+) , tree1*ptr+=item; break; tree1*ptr+=tree1*par+; ptr=par; tree1*ptr+=item;

break; case 2: , if(n==0) , System.out.println("There is no element into array"); break; int i,j; int arr*+=new int*50+; j=n; System.out.println("Your max-heap elements are = "); for(i=1;i<=j;i++) , arr*i+=tree*i+;

System.out.print(" "+arr*i+); System.out.println("\n");

int delete*+=new int*50+; for(i=1;i<=n;i++) , item=arr*1+; delete*i+=item; int last=arr*j+; j=j-1; ptr=1; int left=2,right=3; while(right<=j) , if(last>=arr*left+&&last>=arr*right+) , arr*ptr+=last; break; if(arr*right+<=arr*left+) , arr*ptr+=arr*left+; ptr=left; else , arr*ptr+=arr*right+; ptr=right; left=2*ptr;

right=left+1; if(left==j && last<arr*left+) , arr*ptr+=arr*left+; ptr=left; arr*ptr+=last; System.out.println("Your sorted array : "); for(i=1;i<=n;i++) System.out.print(" "+delete*i+); break; case 3: , if(n1==0) , System.out.println("There is no element into array"); break; -

int i,j; int arr*+=new int*50+; j=n1; System.out.println("Your min-heap elements are = "); for(i=1;i<=j;i++) , arr*i+=tree1*i+; System.out.print(" "+arr*i+); -

System.out.println("\n");

int delete*+=new int*50+; for(i=1;i<=n1;i++) , item=arr*1+; delete*i+=item; int last=arr*j+; j=j-1; ptr=1; int left=2,right=3; while(right<=j) , if(last<=arr*left+&&last<=arr*right+) , arr*ptr+=last; break; if(arr*right+>=arr*left+) , arr*ptr+=arr*left+; ptr=left; else , arr*ptr+=arr*right+; ptr=right; left=2*ptr; right=left+1; -

if(left==j && last>arr*left+) , arr*ptr+=arr*left+; ptr=left; arr*ptr+=last; System.out.println("Your sorted array : "); for(i=1;i<=n1;i++) System.out.print(" "+delete*i+);

break; case 4: , if(n==0 && n1==0) , System.out.println("There is no element into array for deletion"); break; -

int i; System.out.println("\nYour max-heap array before deletion : "); for(i=1;i<=n;i++) System.out.print(" "+tree*i+);

item=tree*1+; int last=tree*n+; n=n-1;

ptr=1; int left=2,right=3; while(right<=n) , if(last>=tree*left+&&last>=tree*right+) , tree*ptr+=last; break; if(tree*right+<=tree*left+) , tree*ptr+=tree*left+; ptr=left; else , tree*ptr+=tree*right+; ptr=right; left=2*ptr; right=left+1; if(left==n && last<tree*left+) , tree*ptr+=tree*left+; ptr=left; tree*ptr+=last;

System.out.println("\n\nYour max-heap array after deletion : "); for(i=1;i<=n;i++)

System.out.print(" "+tree*i+);

System.out.println("\n\nYour min-heap array elements before deletion : "); for(i=1;i<=n1;i++) , System.out.print(" "+tree1*i+); System.out.println("\n");

item=tree1*1+; last=tree1*n1+; n1=n1-1; ptr=1; left=2; right=3; while(right<=n1) , if(last<=tree1*left+&&last<=tree1*right+) , tree1*ptr+=last; break; if(tree1*right+>=tree1*left+) , tree1*ptr+=tree1*left+; ptr=left; else , tree1*ptr+=tree1*right+;

ptr=right; left=2*ptr; right=left+1; if(left==n1 && last>tree1*left+) , tree1*ptr+=tree1*left+; ptr=left; tree1*ptr+=last;

System.out.println("Your mean-heap array element after deletion : "); for(i=1;i<=n1;i++) System.out.print(" "+tree1*i+);

break; case 5: , System.exit(0); break; default: System.out.println("Invalid entry try again"); -