0% found this document useful (0 votes)
3 views4 pages

Design & Analysis of Algorithms: Assignment # 03

assgiment 4

Uploaded by

Waheed Sultan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Design & Analysis of Algorithms: Assignment # 03

assgiment 4

Uploaded by

Waheed Sultan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 04

Obtained
Marks:

Design & Analysis


of Algorithms
Assignment # 03

Last date of Submission: 2 DEC 2024

Submitted To: Hasnain Tahir

Student Name: Waheed sultan

Reg Number: 2212465

DAA BS(CS)-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.

Question
Write a C/C++ program that implements the Quicksort algorithm using the Lomuto
partition scheme to sort an array of integers in descending order.

Your program should perform the following steps:


1. Input an integer value 'n' representing the number of elements in the array.
2. Input 'n' integer values to populate the array.
3. Implement the Quicksort algorithm using the Lomuto partition scheme to sort the
array in Ascending order.
4. Print the sorted array.

Your implementation should meet the following requirements.


 Use the Lomuto partition scheme for the Quicksort algorithm.
 Handle cases where the array may contain duplicate values.
 Implement the Quicksort algorithm using recursion.
 Do not use any built-in sorting functions or libraries.
 The program should display the partition, highlighting the pivot, whenever it splits it.

Your program should be well-commented and organized, with a clear implementation of


the
Quicksort algorithm using the Lomuto partition scheme. It should also include
appropriate error Handling for input validation.

CODE:
#include <iostream>
#include <limits>

using namespace std;

// Function to perform the Lomuto partition


int lomutoPartition(int arr[], int low, int high) {
int pivot = arr[high]; // Pivot is the last element
int i = low - 1; // Index for the smaller element

// Partition process
for (int j = low; j < high; j++) {
if (arr[j] < pivot) { // For ascending order
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];

DAA BS(CS)-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

arr[j] = temp;
}
}

// Place the pivot in the correct position


int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;

// Print the array showing the pivot position


cout << "Partition around pivot " << pivot << ": ";
for (int k = low; k <= high; k++) {
if (k == i + 1)
cout << "[" << arr[k] << "] ";
else
cout << arr[k] << " ";
}
cout << endl;

return i + 1; // Return pivot index


}

// Recursive function to implement Quicksort


void quicksort(int arr[], int low, int high) {
if (low < high) {
// Partitioning index
int pivotIndex = lomutoPartition(arr, low, high);

// Recursively sort the subarrays


quicksort(arr, low, pivotIndex - 1);
quicksort(arr, pivotIndex + 1, high);
}
}

int main() {
int n;

// Input validation for array size


cout << "Enter the number of elements in the array: ";
while (!(cin >> n) || n <= 0) {
cout << "Invalid input. Please enter a positive integer: ";
cin.clear(); // Clear error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore invalid input
}

int *arr = new int[n]; // Dynamic array allocation for C++98 compatibility

DAA BS(CS)-4 SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

// Input array elements


cout << "Enter " << n << " integer values:" << endl;
for (int i = 0; i < n; i++) {
while (!(cin >> arr[i])) {
cout << "Invalid input. Please enter an integer: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}

cout << "\nSorting process:" << endl;

// Apply Quicksort
quicksort(arr, 0, n - 1);

// Print the sorted array


cout << "\nSorted array in ascending order: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

delete[] arr; // Free the allocated memory

return 0;
}

Output:

DAA BS(CS)-4 SZABIST-ISB

You might also like