Question: Program C++ Please shuffle counter in main. The count should display how many times the array was shuffled. #include #include #include #include #define SIZE 4
Program C++
Please shuffle counter in main. The count should display how many times the array was shuffled.
#include
// Partitioning the array on the basis of values at high as pivot value. int Partition(int a[], int low, int high) { int pivot, index, i; index = low; pivot = high; // Getting index of pivot. for(i=low; i < high; i++) { if(a[i] < a[pivot]) //ascending and desending { swap(&a[i], &a[index]); index++; } } // Swapping value at high and at the index obtained. swap(&a[pivot], &a[index]); return index; } // Random selection of pivot. int RandomPivotPartition(int a[], int low, int high) { int pvt, n, temp; n = rand(); // Randomizing the pivot value in the given subpart of array. pvt = low + n%(high-low+1); // Swapping pvt value from high, so pvt value will be taken as pivot while partitioning. swap(&a[high], &a[pvt]); return Partition(a, low, high); } // Implementing QuickSort algorithm. int QuickSort(int a[], int low, int high) { int pindex; if(low < high) { // Partitioning array using randomized pivot. pindex = RandomPivotPartition(a, low, high); // Recursively implementing QuickSort. QuickSort(a, low, pindex-1); QuickSort(a, pindex+1, high); } return 0; }
int main(int argc, char* argv[]) {
int countFunction (int shuffle); srand(time(NULL)); int arr[SIZE];
cout<<" Unsorted Data \t"; for (int i = 0; i < SIZE; ++i) { arr[i] = rand() % LIMIT +1; cout<< arr[i] << " "; }
ShuffleArray(arr, SIZE); cout << " Shuffled \t"; for (int i = 0; i < SIZE; i++){ cout<< arr[i] << " "; }
QuickSort(arr, 0, SIZE-1); // Printing the sorted data. cout<<" Sorted Array: "; for (int i = 0; i < SIZE; i++){ cout<< arr[i] << " "; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
