0% found this document useful (0 votes)
3 views

ass1

The document contains a C++ program that manages student records using various sorting and searching algorithms. It includes bubble sort, insertion sort, and quick sort for sorting students by roll number, name, and SGPA, respectively, as well as linear and binary search methods for finding students based on SGPA and name. The program allows user input for student details and provides options to display sorted records or search for specific students.

Uploaded by

Anirudh
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 views

ass1

The document contains a C++ program that manages student records using various sorting and searching algorithms. It includes bubble sort, insertion sort, and quick sort for sorting students by roll number, name, and SGPA, respectively, as well as linear and binary search methods for finding students based on SGPA and name. The program allows user input for student details and provides options to display sorted records or search for specific students.

Uploaded by

Anirudh
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/ 8

#include <iostream>

#include <string>

using namespace std;

struct Student {

int roll_no;

string name;

float sgpa;

};

// Bubble sort using roll number

void bubbleSort(Student *s[], int n) {

for (int i = 0; i < n - 1; ++i) {

for (int j = 0; j < n - 1; ++j) {

if (s[j]->roll_no > s[j + 1]->roll_no) {

swap(s[j], s[j + 1]);

cout << "\nSorted by Bubble sort\n";

// Insertion sort using name

void insertionSort(Student *s[], int n) {

for (int i = 1; i < n; i++) {

int j = i;

while (j > 0 && s[j - 1]->name > s[j]->name) {

swap(s[j - 1], s[j]);

j--;
}

cout << "\nSorted by Insertion sort\n";

// Quick sort using SGPA

int partitionSGPA(Student *s[], int l, int h) {

float pivot = s[l]->sgpa;

int i = l;

int j = h;

while (i < j) {

while (s[i]->sgpa <= pivot && i <= h - 1) i++;

while (s[j]->sgpa > pivot && j >= l + 1) j--;

if (i < j) swap(s[i], s[j]);

swap(s[l], s[j]);

return j;

void quickSortSGPA(Student *arr[], int l, int h) {

if (l < h) {

int pi = partitionSGPA(arr, l, h);

quickSortSGPA(arr, l, pi - 1);

quickSortSGPA(arr, pi + 1, h);

}
// Linear search using SGPA

void linearSearchSGPA(Student *s[], int n, float SGPA) {

bool found = false;

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

if (s[i]->sgpa == SGPA) {

if (!found) {

cout << "Students with SGPA " << SGPA << ":\n";

found = true;

cout << "Roll no. " << s[i]->roll_no << ", Name: " << s[i]->name
<< ", SGPA: " << s[i]->sgpa << "\n";

if (!found) {

cout << "No Students with SGPA " << SGPA << "\n";

// Binary search using SGPA

int binarySearchSGPA(Student *s[], int n, float SGPA) {

int left = 0;

int right = n - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (s[mid]->sgpa == SGPA) return mid;

if (s[mid]->sgpa < SGPA) left = mid + 1;


else right = mid - 1;

return -1;

// Binary search using name

int binarySearchName(Student *s[], int n, string NAME) {

int left = 0;

int right = n - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (s[mid]->name == NAME) return mid;

if (s[mid]->name < NAME) left = mid + 1;

else right = mid - 1;

return -1;

// Display student details

void displayStudentDetails(Student *s[], int n) {

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

cout << "Student " << i + 1 << "\n";

cout << "Roll No: " << s[i]->roll_no << "\n";

cout << "Name: " << s[i]->name << "\n";

cout << "SGPA: " << s[i]->sgpa << "\n";

cout << "\n";

}
}

// Display the first 10 toppers in the class using quick sort

void displayToppers(Student *s[], int n) {

for (int i = n - 1; i >= max(n - 10, 0); i--) {

cout << "Student " << n - i << "\n";

cout << "Roll No: " << s[i]->roll_no << "\n";

cout << "Name: " << s[i]->name << "\n";

cout << "SGPA: " << s[i]->sgpa << "\n";

cout << "\n";

int main() {

int n;

cout << "Enter the number of students: ";

cin >> n;

Student* s[n];

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

s[i] = new Student;

cout << "Enter details for student " << i + 1 << "\n";

cout << "Roll No: "; cin >> s[i]->roll_no;

cin.ignore(); // to ignore the newline after roll number

cout << "Name: "; getline(cin, s[i]->name);

cout << "SGPA: "; cin >> s[i]->sgpa;

cout << "\n";

}
int c;

bool ans = true;

while (ans) {

cout << "For Bubble sort -> 1 \nFor Insertion sort -> 2 \nFor Quick
sort using SGPA -> 3 \nLinear Search -> 4 \nBinary Search using SGPA ->
5 \nBinary Search using name -> 6 \nExit -> 7 \n";

cin >> c;

switch (c) {

case 1:

bubbleSort(s, n);

displayStudentDetails(s, n);

break;

case 2:

insertionSort(s, n);

displayStudentDetails(s, n);

break;

case 3:

quickSortSGPA(s, 0, n - 1);

displayToppers(s, n);

break;

case 4: {

float SGPA;

cout << "Enter the SGPA to search: ";

cin >> SGPA;

linearSearchSGPA(s, n, SGPA);

break;

case 5: {

float SGPA;

cout << "Enter the SGPA to search: ";


cin >> SGPA;

int index = binarySearchSGPA(s, n, SGPA);

if (index != -1) {

cout << "Student found: Roll no. " << s[index]->roll_no << ",
Name: " << s[index]->name << ", SGPA: " << s[index]->sgpa <<endl;

} else {

cout << "Student not found"<<endl;

break;

case 6: {

string NAME;

cout << "Enter the NAME to search: ";

cin >> NAME;

int index = binarySearchName(s, n, NAME);

if (index != -1) {

cout << "Student found: Roll no. " << s[index]->roll_no << ",
Name: " << s[index]->name << ", SGPA: " << s[index]->sgpa <<endl;

} else {

cout << "Student not found."<<endl;;

break;

case 7:

cout << "Exited Successfully\n";

ans = false;

break;

default:

cout << "Enter a valid operation."<<endl;;

continue;
}

return 0;

You might also like