—----------------- College operation —------------------
#include <iostream>
using namespace std;
class student
{
public:
int rollno;
string name;
double sgpa;
void add()
{
cout << "Enter roll no: ";
cin >> rollno;
cout << "Enter name: ";
cin >> name;
cout << "Enter SGPA: ";
cin >> sgpa;
}
void show()
{
cout << "Roll No: " << rollno << endl;
cout << "Name : " << name << endl;
cout << "SGPA : " << sgpa << endl;
cout << "------------------" << endl;
}
};
/* --------- GLOBAL SWAP ---------- */
void swap(student &a, student &b)
{
student temp = a;
a = b;
b = temp;
}
/* --------- BUBBLE SORT (ROLL NO) ---------- */
void bubble(student s[], int n)
{
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - i - 1; j++)
{
if(s[j].rollno > s[j + 1].rollno)
swap(s[j], s[j + 1]);
}
}
}
/* --------- INSERTION SORT (NAME) ---------- */
void insertion_sort(student s[], int n)
{
for(int i = 1; i < n; i++)
{
student key = s[i];
int j = i - 1;
while(j >= 0 && s[j].name > [Link])
{
s[j + 1] = s[j];
j--;
}
s[j + 1] = key;
}
}
/* --------- QUICK SORT (SGPA DESCENDING) ---------- */
int partition(student arr[], int low, int high)
{
double pivot = arr[high].sgpa;
int i = low - 1;
for(int j = low; j < high; j++)
{
if(arr[j].sgpa > pivot)
{
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void quicksort(student arr[], int low, int high)
{
if(low < high)
{
int p = partition(arr, low, high);
quicksort(arr, low, p - 1);
quicksort(arr, p + 1, high);
}
}
void linearsearch(student arr[],int n,double key)
{
for(int i=0;i<n;i++)
{
if(arr[i].sgpa== key)
{
cout<<"student search successfully "<<endl;
cout<<arr[i].rollno<<" ";
cout<<arr[i].name<<" ";
cout<<arr[i].sgpa<<" ";
}
}
}
void binarysearch(student arr[], int n, int key)
{
int low = 0, high = n - 1;
bool found = false;
while(low <= high)
{
int mid = (low + high) / 2;
if(arr[mid].rollno == key)
{
cout << "Student found successfully\n";
cout << "Roll No: " << arr[mid].rollno << endl;
cout << "Name : " << arr[mid].name << endl;
cout << "SGPA : " << arr[mid].sgpa << endl;
found = true;
break;
}
else if(arr[mid].rollno < key)
low = mid + 1;
else
high = mid - 1;
}
if(!found)
cout << "Student not found\n";
}
int main()
{
int n;
cout << "How many students: ";
cin >> n;
student c[n];
for(int i = 0; i < n; i++)
c[i].add();
int ch;
bool flag = true;
while(flag)
{
cout << "\n1. Sort by Roll No\n2. Sort by Name\n3. Sort by SGPA\[Link] by using
sgpa\[Link] search by using roll no ";
cout << "Enter choice: ";
cin >> ch;
switch(ch)
{
case 1:
bubble(c, n);
break;
case 2:
insertion_sort(c, n);
break;
case 3:
quicksort(c, 0, n - 1);
break;
case 4:
double m;
cout<<"enter search student sgpa";
cin>>m;
linearsearch(c,n,m);
break;
case 5:
int r;
cout << "Enter roll number to search: ";
cin >> r;
bubble(c, n);
binarysearch(c, n, r);
break;
default:
flag= false;
break;
}
}
return 0;
}