Posted By

DrPepper on 09/19/11


Tagged


Versions (?)

[CISP 430] Heap Sort


 / Published in: C++
 

URL: http://en.wikipedia.org/wiki/Heapsort

  1. int save,i,j,v;
  2.  
  3. for(i=n-1;i>=0;i--){
  4. save=i;
  5. v=data[i];
  6. for(;;){
  7. j=save+save+1;
  8. if(j>n-1){ break; }
  9. if( (j!=n-1)&&(data[j+1]>data[j]) ){
  10. j++;
  11. }
  12. if(v>=data[j]){ break; }
  13. data[save]=data[j];
  14. save=j;
  15. }
  16. data[save]=v;
  17. }
  18.  
  19. for(i=n-1;i>0;i--){
  20. swap(data[0],data[i]);
  21. save=0;
  22. v=data[0];
  23. for(;;){
  24. j=save+save+1;
  25. if(j>i-1){ break; }
  26. if( (j!=i-1)&&(data[j+1]>data[j]) ){
  27. j++;
  28. }
  29. if(v>=data[j]){ break; }
  30. data[save]=data[j];
  31. save=j;
  32. }
  33. data[save]=v;
  34. }

Report this snippet  

You need to login to post a comment.