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* 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 < r) { 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; i++) cin>>arr[i]; quickSort(arr, 0, n-1); for (i=0; i < n; i++) cout<

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

#include <iostream>

#include <stdlib.h>

#include <time.h>  

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 < r)

{

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; i++)

cin>>arr[i];

quickSort(arr, 0, n-1);

for (i=0; i < n; i++)

cout<<arr[i]<<";";

return 0;

}

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 ;.
Transcribed Image 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 ;.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education