Professional Documents
Culture Documents
CSE-321
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.
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
Heap
sorted array
details
8, 6, 7, 4, 5, 3, 8, 1 2, 1
1, 6, 7, 4, 5, 3, 2, 8
1, 6, 7, 4, 5, 3, 1, 7 2
7, 6, 1, 4, 5, 3, 1, 3 2
7, 6, 3, 4, 5, 1, 2
7, 2
2, 6, 3, 4, 5, 1, 7
2, 6, 3, 4, 5, 1
2, 6
7, 8
6, 2, 3, 4, 5, 1
2, 5
7, 8
6, 5, 3, 4, 2, 1
6, 1
7, 8
heap
1, 5, 3, 4, 2, 6
7, 8
1, 5, 3, 4, 2
1, 5
6, 7, 8
5, 1, 3, 4, 2
1, 4
6, 7, 8
5, 4, 3, 1, 2
5, 2
6, 7, 8
2, 4, 3, 1, 5
6, 7, 8
2, 4, 3, 1
2, 4
5, 6, 7, 8
4, 2, 3, 1
4, 1
5, 6, 7, 8
1, 2, 3, 4
5, 6, 7, 8
1, 2, 3
1, 3
4, 5, 6, 7, 8
3, 2, 1
3, 1
4, 5, 6, 7, 8
1, 2, 3
4, 5, 6, 7, 8
1, 2
1, 2
3, 4, 5, 6, 7, 8
2, 1
2, 1
3, 4, 5, 6, 7, 8
1, 2
3, 4, 5, 6, 7, 8
2, 3, 4, 5, 6, 7, 8
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;
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+;
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+);
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.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+;
System.out.println("Your mean-heap array element after deletion : "); for(i=1;i<=n1;i++) System.out.print(" "+tree1*i+);