0% found this document useful (0 votes)
5 views6 pages

Worksheet 2 1

Uploaded by

vortexstock121
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)
5 views6 pages

Worksheet 2 1

Uploaded by

vortexstock121
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/ 6

Worksheet 2.

1
Student Name: Vishal Singh UID: 21BCA1253
Branch: BCA Section/Group: 21BCA3-A
Semester: 3rd Date of Performance: 08.11.2022
Subject Name: Data Structures Lab Subject Code: 21CAP-214

1. Aim/Overview of the practical:


 To perform quick sort using last element as pivot.

2. Task to be done:
 Write a program to perform quick sort using last element as pivot.

3. Program:

#include <iostream>
using namespace std;
int pvt(int arr[], int l, int r) {
int j, temp, i = l;

for (j = l; j < r - 1; j++) {


if (arr[j] <= arr[r - 1]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
}
}
temp = arr[i];
arr[i] = arr[r - 1];
arr[r - 1] = temp;
return i;
}

void qSort(int arr[], int l, int r) {


if (l < r) {
int q = pvt(arr, l, r);
qSort(arr, l, q - 1);
qSort(arr, q + 1, r);
}
}

int main() {
int size, i, max=0;

cout << endl;


cout << "Enter array size: ";
cin >> size;

int arr[size];

cout << "Enter array elements: ";


for (i = 0; i < size; i++)
cin >> arr[i];

for (int i = 0; i < size; i++) {


if(arr[i] > max)
max = arr[i];
}

qSort(arr, 0, size);

cout << "\nAfter quick sort: ";


for (i = 0; i < size; i++)
cout << arr[i] << " ";

cout << endl << endl;

return 0;
}

4. Observations:
 Quick sort has a space complexity of O(log n), making it an excellent choice for
situations when space is limited.
 When the pivot element is the largest or smallest, or when all of the components have the
same size. The performance of the quicksort is significantly impacted by these worst-case
scenarios.

5. Output:
6. Learning outcomes (What I have learnt):
1. Learned to implement quick sort using last element as pivot.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1. Worksheet 20
2. Quiz 10
3. Class Performance 10

You might also like