Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void printrandom(int*A[],int m, int n, int min, int max){
- int num;
- for(int j=0;j<m;j++){
- for(int i=0;i<n;i++){
- int num=(rand()%(max-min+1)) + min;
- A[j][i]=num;
- }
- }
- };
- //function to print array
- void printout_array (int* A[], int m, int n){
- for(int i=0;i<m;++i){
- for(int j=0;j<n;++j){
- cout<<A[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- //copying value array A into array B
- void duplicate_array(int*A[], int*B[], int m, int n){
- for(int i=0;i<m;++i){
- for(int j=0;j<n;++j){
- B[i][j]=A[i][j];
- //cout<<B[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- void min_heap(int *A[], int m, int n, int k){
- int j, t;
- t= A[m][k];
- j = 2 * k;
- while (j <= n) {
- if (j < n && A[m][j+1] < A[m][j])
- j = j + 1;
- if (t < A[m][j])
- break;
- else if (t >= A[m][j]) {
- A[m][j/2] = A[m][j];
- j = 2 * j;
- }
- }
- A[m][j/2] = t;
- return;
- }
- void build_minheap(int *A[], int m, int n) {
- int k;
- for(int i=0;i<m;i++){
- for(k = n/2; k >= 1; k--) {
- min_heap(A, m, n, k);
- }
- }
- return;
- };
- // Swap two elements - Utility function
- void swap(int* a, int* b)
- {
- int t = *a;
- *a = *b;
- *b = t;
- }
- // partition the array using last element as pivot
- int partition (int *A[], int m, int low, int high)
- {
- int pivot = A[m-1][high]; // pivot
- int i = (low - 1);
- for (int j = low; j <= high- 1; j++)
- {
- //if current element is smaller than pivot, increment the low element
- //swap elements at i and j
- if (A[m-1][j] <= pivot)
- {
- i++; // increment index of smaller element
- swap(&A[m-1][i], &A[m-1][j]);
- }
- }
- swap(&A[m-1][i + 1], &A[m-1][high]);
- return (i + 1);
- }
- void quickSort(int *A[], int m, int low, int high)
- {
- if (low < high)
- {
- //partition the array
- int pivot = partition(A, m, low, high);
- //sort the sub arrays independently
- quickSort(A, m, low, pivot - 1);
- quickSort(A, m, pivot + 1, high);
- }
- }
- int main (){
- int m, n, min, max;
- cout<<"please input m (m=1): ";
- cin>>m;
- int**A=new int*[m]; // Declare Array A
- for(int i=0;i<m;++i)
- A[i]=new int [n];
- cout<<"Please input [n, a_min, a_max]: ";
- cin>>n>>min>>max;
- srand(time(0));
- printrandom(A,m,n, min, max);
- cout<<"print function result: ";
- for(int j=0;j<m;j++){
- for(int i=0;i<n;i++){
- cout<<A[j][i]<<" ";
- }
- }
- cout<<endl<<endl;
- cout<<"print printout array: ";
- printout_array (A,m,n);
- int**B=new int*[m]; // Create B array
- for(int i=0;i<m;++i)
- B[i]=new int [n];
- for(int i=0;i<m;++i) // Clear B array
- for(int j=0;j<n;++j)
- B[i][j] = 0;
- duplicate_array (A,B,m,n); //duplicate array A into B
- cout<<"duplicate array A to B: ";
- for(int j=0;j<m;j++){
- for(int i=0;i<n;i++){
- cout<<B[j][i]<<" ";
- }
- }
- build_minheap(A,m,n);
- cout<<endl<<"using min heap: ";
- for(int j=0;j<m;j++){
- for(int i=0;i<n;i++){
- cout<<A[j][i]<<" ";
- }
- }
- cout<<endl<<"by heapsort : ";
- printout_array (B,m,n);
- quickSort(B, m, 0, n-1);
- cout<<"Array sorted with quick sort"<<endl;
- printout_array (B,m,n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement