Please explain the code below line by line in detail . The code is finished, I just need the explanation ​​​​​​.Code in C++: #include #include #include using namespace std; void swap(int* a, int*...


Please explain the code belowline by line in detail. The code is finished, I just need the explanation ​​​​​​.Code in C++:


#include


#include


#include


using namespace std;


void swap(int* a, int* b)


{


int t = *a;


*a = *b;


*b = t;


}


int partition(int arr[],int p,int r)


{


   srand(time(0));


   int pivot = rand()%(r-p);


   pivot += p;


   swap(arr[r],arr[pivot]);


   pivot = r;


   int temp = arr[r];


   int i = p-1;


   for (int j = p; j < r="" ;j++)="">


    if ( arr[j] < temp="" )="">


    i++;


    swap (arr[i],arr[j]);


      }


   }


   i++;


   swap (arr[r],arr[i]);


   return (i);


}


void quickSort(int arr[], int p, int r)


{


if (p <>


{


int pivot = partition(arr, p, r);


quickSort(arr, p, pivot - 1);


quickSort(arr, pivot + 1, r);


}


}


int main()


{


int arr[1000000];


int n;


cin>>n;


int i;


for (i=0; i < n;="">


cin>>arr[i];


quickSort(arr, 0, n-1);


for (i=0; i < n;="">


cout<><>


return 0;


}


Quick-Sort<br>Description<br>(Lab04-2), your job is to implement the randomized version of Quick-sort. That is, you must<br>choose a random pivot from the elements in A[p...r] when partitioning the subarray. For more<br>details, see page 179 of the textbook. The following webpage describes a simple way to obtain a<br>random integer: http://www.cplusplus.com/reference/cstdlib/rand/<br>This is the second half of Lab04 and is worth 50 points. In this lab assignment<br>Input structure<br>elements (integers) to be sorted, n. Then, the elements follow, one per line.<br>The input starts with an integer number which indicates the number of<br>Output structure Output the elements in non-decreasing order. Each element must be fol-<br>lowed by ;.<br>

Extracted text: Quick-Sort Description (Lab04-2), your job is to implement the randomized version of Quick-sort. That is, you must choose a random pivot from the elements in A[p...r] when partitioning the subarray. For more details, see page 179 of the textbook. The following webpage describes a simple way to obtain a random integer: http://www.cplusplus.com/reference/cstdlib/rand/ This is the second half of Lab04 and is worth 50 points. In this lab assignment Input structure elements (integers) to be sorted, n. Then, the elements follow, one per line. The input starts with an integer number which indicates the number of Output structure Output the elements in non-decreasing order. Each element must be fol- lowed by ;.
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here