fork download
  1. #include<iostream>
  2. #include<time.h>
  3. using namespace std;
  4.  
  5. void sift(int [],int,int);
  6. void BuildMaxHeap(int [],int);
  7. void Heap_sort(int [],int);
  8. void Heap_sort_old(int [],int);
  9.  
  10. int main(void){
  11.  
  12. srand(time(NULL));
  13.  
  14.  
  15. int heap[13];
  16.  
  17. for(int i=0;i<13;i++)
  18. heap[i]=rand()%100;
  19.  
  20. cout<<"Heap sort前:";
  21. for(int i=0;i<13;i++)
  22. cout<<heap[i]<<" ";
  23. cout<<endl;
  24.  
  25. Heap_sort(heap,13);
  26.  
  27. cout<<"Heap sort後:";
  28. for(int i=0;i<13;i++)
  29. cout<<heap[i]<<" ";
  30. cout<<endl;
  31.  
  32. system("pause");
  33. return 0;
  34. }
  35.  
  36.  
  37. void BuildMaxHeap(int heap[],int n){
  38. int i;
  39. for(i=n/2-1;i>=0;i--)
  40. sift(heap,i,n);
  41.  
  42. }
  43.  
  44.  
  45. void sift(int a[],int head,int n){
  46.  
  47.  
  48. int temp,j;
  49.  
  50.  
  51.  
  52.  
  53. while(head*2+1<n){
  54.  
  55. j=2*head+1;
  56.  
  57. if(head*2+1==n-1){
  58.  
  59.  
  60. if(a[head]>a[j])
  61. break;
  62.  
  63.  
  64. }
  65.  
  66. else{
  67. if(a[j+1]>a[j])
  68. j++;
  69.  
  70. if(a[head]>a[j])
  71. break;
  72.  
  73.  
  74. }
  75.  
  76. temp=a[head];
  77. a[head]=a[j];
  78. a[j]=temp;
  79. head=j;
  80.  
  81.  
  82. }
  83.  
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. void Heap_sort(int heap[],int n){
  93. int temp;
  94.  
  95. for(int i=(n-2)/2;i>=0;i--)
  96. sift(heap,i,n);
  97.  
  98.  
  99. for(int i=n-1;i>=1;i--){
  100.  
  101. temp=heap[i];
  102. heap[i]=heap[0];
  103. heap[0]=temp;
  104.  
  105. sift(heap,0,i);
  106.  
  107. }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void Heap_sort_old(int heap[],int n){
  114. int temp;
  115.  
  116. for(int i=(n-2)/2;i>=0;i--)
  117. sift(heap,i,n);
  118.  
  119.  
  120. while(n>1){
  121. temp=heap[n-1];
  122. heap[n-1]=heap[0];
  123. heap[0]=temp;
  124. n--;
  125.  
  126.  
  127. for(int i=0;i<=n-1;i++)
  128. sift(heap,0,n);
  129.  
  130.  
  131. }
  132.  
  133.  
  134. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:12: error: ‘srand’ was not declared in this scope
prog.cpp:18: error: ‘rand’ was not declared in this scope
prog.cpp:32: error: ‘system’ was not declared in this scope
stdout
Standard output is empty