Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- #include<stdlib.h>
- /*This code is written by NF between any time of 120711 and 210711;modified at 231211*/
- int main()
- {
- int length=0,min_heapified=0,start_length=0,index=0;
- cout<<"Enter Array Length:";
- cin>>length;
- int array[length];
- for(int i=0;i<length;i++)
- {
- array[i]=rand()%100;
- }
- cout<<"Array is filled now with random elements";
- cout<<"nSorted Array:"<<endl;
- do
- {
- do
- {
- min_heapified=0;
- for(int root=0;root<length/2;root++)/*As the leaf nodes have no child they should not be in a consideration while swapping so looping upto length/2 is enough*/
- {
- int left=((2*root)+1);
- int right=((2*root)+2);
- if(left<length)
- {
- if(array[left]<array[root])
- {
- int swap=array[left];
- array[left]=array[root];
- array[root]=swap;
- min_heapified=1;
- }
- }
- if(right<length)
- {
- if(array[right]<array[root])
- {
- int swap=array[right];
- array[right]=array[root];
- array[root]=swap;
- min_heapified=1;
- }
- }
- }
- }while(min_heapified>0);
- int swap=array[0];
- array[0]=array[--length];/*modification done at this point to avoid keeping the sorted elements into another array;and swapping done between the 0th and last element*/
- array[length]=swap;
- cout<<array[length]<<" ";
- }while(length>0);
- return 0;
- }
- void heapsort(int *begin, int *end)
- {
- // assume end is one pass the last element. eg. arry + length;
- heapify(begin, end);
- while(begin != --end)
- {
- swap(*begin, *end); // invariant: the next item to order will be at the top
- // after the swap, the next biggest/smallest item may not be
- // at the top. use push_down to fix this and preserve the heap property.
- push_down(begin, end);
- }
- }
- void push_down(int *begin, int *end, size_t defender = 0)
- {
- // at the very bottom?
- if(defender >= std::distance(begin, end) / 2) return;
- size_t challenger = defender * 2 + 1;
- // is there a right-child?
- // Note that in even# arrays, last parent doesn't have a right child
- if(begin + challenger + 1 != end)
- if(begin[challenger + 1] < begin[challenger])
- ++challenger;
- // defended successfully?
- if( !(begin[challenger] < begin[defender]) ) return;
- // challenger wins
- std::swap(begin[challenger], begin[defender]);
- push_down(begin, end, challenger);
- }
- int length=0,min_heapified=0,start_length=0,index=0;
- do {
- int min_heapified = 0;
- do {
- ...
- if(x<=(length-1))
- if (x < length)
- int swap=array[left];
- array[left]=array[root];
- array[root]=swap;
- int swap(array, index, rootIndex) {
- int swap = array[index];
- array[index] = array[rootIndex];
- array[root] = swap;
- }
- if(array[left]<array[root])
- {
- int swap=array[left];
- array[left]=array[root];
- array[root]=swap;
- min_heapified=1;
- }
- int swapIfLess(array, index, rootIndex) {
- if (array[index] < array[rootIndex]) {
- swap(array, index, rootIndex)
- return 1;
- }
- return 0;
- }
- if (left < length) {
- if (swapIfLess(array, left, root)) {
- min_heapified=1;
- }
- }
- if (right < length) {
- if (swapIfLess(array, right, root)) {
- min_heapified=1;
- }
- }
- int root=i;
- for (int root = 0; root < length; root++)
- int length=0,min_heapified=0,start_length=0,index=0;
- int array[length],sorted_array[length];
- if(root <= (length-1))
- // Prefer the following
- if(root < length)
- if(root < length)
- for(int root=0;root<length;root++)
- // prefer
- for(int root=0;root<length;++root)
- min_heapified=0;
- bool min_heapified = false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement