0% found this document useful (0 votes)
19 views2 pages

Quicksort

Uploaded by

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

Quicksort

Uploaded by

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

File: quicksort.

cpp Page 1 of 2

#include <iostream>
#include <string>
using namespace std;

const int arraySize = 15; // Increased array size to 15

struct student {
int roll_no;
string name;
float SGPA;
};

// ACCEPT FUNCTION
void accept(student list[arraySize]) {
for (int i = 0; i < arraySize; i++) {
cout << "\nEnter Roll-Number, Name, SGPA: ";
cin >> list[i].roll_no >> list[i].name >> list[i].SGPA;
}
}

// DISPLAY FUNCTION
void display(const student list[], int size) {
cout << "\nRoll-Number \t Name \t SGPA\n";
for (int i = 0; i < size; i++) {
cout << list[i].roll_no << " \t " << list[i].name << "\t " << list[i].SGPA << endl;
}
}

// QUICK SORT FUNCTION


void quick_sort(student list[], int first, int last) {
int pivot, i, j;
if (first < last) {
pivot = first;
i = first;
j = last;
while (i < j) {
while (list[i].SGPA >= list[pivot].SGPA && i < last) {
i++;
}
while (list[j].SGPA < list[pivot].SGPA) {
j--;
}
if (i < j) {
swap(list[i], list[j]);
}
}
swap(list[pivot], list[j]);
quick_sort(list, first, j - 1);
quick_sort(list, j + 1, last);
}
}

int main() {
int ch;
student data[arraySize];
accept(data);

do {
cout << "\n1) Quick Sort";
cout << "\n2) Exit";
File: quicksort.cpp Page 2 of 2

cout << "\nSelect Your Choice: ";


cin >> ch;

switch (ch) {
case 1:
quick_sort(data, 0, arraySize - 1);
// Display top 10 or less if there are fewer than 10 students
cout << "\nTop Students:\n";
display(data, min(10, arraySize));
break;

case 2:
cout << "\nYou Have Successfully Exited!!!.";
break;

default:
cout << "\nPlease Enter Valid Choice.\n";
}
} while (ch != 2);

return 0;
}

You might also like