Professional Documents
Culture Documents
1. Aim/Overview of the practical: Design a quick sort with random pivoting using Lomuto
partition scheme.
2. Task to be done: To write a program in C++ to design a quick sort with random pivoting
using lomuto partition scheme.
.
#include <cstdlib>
#include <iostream>
using namespace std;
while (true) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i >= j)
return j;
swap(arr[i], arr[j]);
}
}
int partition_r(int arr[], int low, int high)
{
srand(time(NULL));
int random = low + rand() % (high - low);
swap(arr[random], arr[low]);
int main()
{
int arr[] = { 10, 7, 8, 9, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
7. Result/Output/Writing Summary:
Conclusion/ Remarks:
3. Target of partitions is, given an array and an element x of array as pivot, put x at its
correct position in sorted array and put all smaller elements (smaller than x) before x, and
put all greater elements (greater than x) after x. All this should be done in linear time.