Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here's a C + + program that implements Quick Sort using the Lomuto Partition scheme according to the provided pseudocode: ` ` ` cpp #include

Here's a C++ program that implements Quick Sort using the Lomuto Partition scheme according to the provided pseudocode:
```cpp
#include
typedef int Data;
// Function prototypes
void QuickSort(Data *A, int p, int r);
int LomutoPartition(Data *arr, int lo, int hi);
// QuickSort function
void QuickSort(Data *A, int p, int r){
if (p < r){
int q = LomutoPartition(A, p, r);
QuickSort(A, p, q -1);
QuickSort(A, q +1, r);
}
}
// Lomuto Partition function
int LomutoPartition(Data *arr, int lo, int hi){
Data pivot = arr[hi];
int i = lo -1; // place for swapping
for (int j = lo; j <= hi -1; j++){
if (arr[j]<= pivot){
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i +1], arr[hi]);
return i +1;
}
int main(){
Data *A;
int N; // N is number of data
// Get value for N
std::cout << "Enter the number of elements: ";
std::cin >> N;
A = new Data[N];
// Get all N values of A
std::cout << "Enter "<< N <<" elements: ";
for (int i =0; i < N; i++){
std::cin >> A[i];
}
// Call Quicksort
QuickSort(A,0, N -1);
// Display sorted array
std::cout << "Sorted array: ";
for (int i =0; i < N; i++){
std::cout << A[i]<<"";
}
std::cout << std::endl;
delete[] A; // Free the dynamically allocated memory
return 0;
}
```
This program takes the number of elements as input, reads the elements into an array, calls the QuickSort function to sort the array, and finally displays the sorted array.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago