Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void printrandom(int*A[],int m, int n, int min, int max){
  6. int num;
  7. for(int j=0;j<m;j++){
  8. for(int i=0;i<n;i++){
  9. int num=(rand()%(max-min+1)) + min;
  10. A[j][i]=num;
  11. }
  12. }
  13. };
  14. //function to print array
  15. void printout_array (int* A[], int m, int n){
  16. for(int i=0;i<m;++i){
  17. for(int j=0;j<n;++j){
  18. cout<<A[i][j]<<" ";
  19. }
  20. cout<<endl;
  21. }
  22. }
  23. //copying value array A into array B
  24. void duplicate_array(int*A[], int*B[], int m, int n){
  25. for(int i=0;i<m;++i){
  26. for(int j=0;j<n;++j){
  27. B[i][j]=A[i][j];
  28. //cout<<B[i][j]<<" ";
  29. }
  30. cout<<endl;
  31. }
  32. }
  33.  
  34. void min_heap(int *A[], int m, int n, int k){
  35. int j, t;
  36. t= A[m][k];
  37. j = 2 * k;
  38. while (j <= n) {
  39. if (j < n && A[m][j+1] < A[m][j])
  40. j = j + 1;
  41. if (t < A[m][j])
  42. break;
  43. else if (t >= A[m][j]) {
  44. A[m][j/2] = A[m][j];
  45. j = 2 * j;
  46. }
  47. }
  48. A[m][j/2] = t;
  49. return;
  50. }
  51.  
  52. void build_minheap(int *A[], int m, int n) {
  53. int k;
  54. for(int i=0;i<m;i++){
  55. for(k = n/2; k >= 1; k--) {
  56. min_heap(A, m, n, k);
  57. }
  58. }
  59. return;
  60. };
  61. // Swap two elements - Utility function
  62. void swap(int* a, int* b)
  63. {
  64. int t = *a;
  65. *a = *b;
  66. *b = t;
  67. }
  68. // partition the array using last element as pivot
  69. int partition (int *A[], int m, int low, int high)
  70. {
  71. int pivot = A[m-1][high]; // pivot
  72. int i = (low - 1);
  73.  
  74. for (int j = low; j <= high- 1; j++)
  75. {
  76. //if current element is smaller than pivot, increment the low element
  77. //swap elements at i and j
  78. if (A[m-1][j] <= pivot)
  79. {
  80. i++; // increment index of smaller element
  81. swap(&A[m-1][i], &A[m-1][j]);
  82. }
  83. }
  84. swap(&A[m-1][i + 1], &A[m-1][high]);
  85. return (i + 1);
  86. }
  87.  
  88. void quickSort(int *A[], int m, int low, int high)
  89. {
  90. if (low < high)
  91. {
  92. //partition the array
  93. int pivot = partition(A, m, low, high);
  94.  
  95. //sort the sub arrays independently
  96. quickSort(A, m, low, pivot - 1);
  97. quickSort(A, m, pivot + 1, high);
  98. }
  99. }
  100.  
  101. int main (){
  102. int m, n, min, max;
  103. cout<<"please input m (m=1): ";
  104. cin>>m;
  105.  
  106. int**A=new int*[m]; // Declare Array A
  107. for(int i=0;i<m;++i)
  108. A[i]=new int [n];
  109.  
  110. cout<<"Please input [n, a_min, a_max]: ";
  111. cin>>n>>min>>max;
  112. srand(time(0));
  113.  
  114. printrandom(A,m,n, min, max);
  115. cout<<"print function result: ";
  116. for(int j=0;j<m;j++){
  117. for(int i=0;i<n;i++){
  118. cout<<A[j][i]<<" ";
  119. }
  120. }
  121.  
  122. cout<<endl<<endl;
  123. cout<<"print printout array: ";
  124. printout_array (A,m,n);
  125.  
  126. int**B=new int*[m]; // Create B array
  127. for(int i=0;i<m;++i)
  128. B[i]=new int [n];
  129.  
  130. for(int i=0;i<m;++i) // Clear B array
  131. for(int j=0;j<n;++j)
  132. B[i][j] = 0;
  133.  
  134. duplicate_array (A,B,m,n); //duplicate array A into B
  135. cout<<"duplicate array A to B: ";
  136. for(int j=0;j<m;j++){
  137. for(int i=0;i<n;i++){
  138. cout<<B[j][i]<<" ";
  139. }
  140. }
  141.  
  142. build_minheap(A,m,n);
  143. cout<<endl<<"using min heap: ";
  144. for(int j=0;j<m;j++){
  145. for(int i=0;i<n;i++){
  146. cout<<A[j][i]<<" ";
  147. }
  148. }
  149. cout<<endl<<"by heapsort : ";
  150. printout_array (B,m,n);
  151.  
  152. quickSort(B, m, 0, n-1);
  153. cout<<"Array sorted with quick sort"<<endl;
  154.  
  155. printout_array (B,m,n);
  156.  
  157.  
  158. return 0;
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement