Created
February 4, 2016 09:04
-
-
Save ayushgp/78cea9e3cbd6671e9f99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Roll No: 101403051 | |
Name: Ayush Gupta | |
Group: COE3 | |
Question: 1 | |
*/ | |
#include<bits/stdc++.h> | |
using namespace std; | |
class Student{ | |
private: | |
long regNo; | |
string name,branch; | |
float cgpa; | |
public: | |
//Constructors: Default and parameterized | |
Student() {} | |
Student(long,const string,const string,float); | |
//getter function for help in sorting | |
long int getRegNo(); | |
//Pretty print student details | |
void printStudentDetails(); | |
}; | |
Student::Student(long regNo,const string name,const string branch,float cgpa){ | |
this->regNo = regNo; | |
this->name = name; | |
this->branch = branch; | |
this->cgpa = cgpa; | |
} | |
long int Student::getRegNo(){ | |
return regNo; | |
} | |
void Student::printStudentDetails(){ | |
cout<<"\n\nName: "<<name<<"\nRegNo: "<<regNo<<"\nBranch: "<<branch<<"\nCGPA: "<<cgpa<<"\n\n"; | |
} | |
//Non class functions for sorting and searching | |
void swap(Student& a,Student& b){ | |
Student temp = a; | |
a=b; | |
b=temp; | |
} | |
Student linearSearch(long regNo,vector<Student>& students){ | |
for(int i=0;i<students.size();i++){ | |
if(regNo == students[i].getRegNo()){ | |
return students[i]; | |
} | |
} | |
throw "Student not found!"; | |
} | |
//Ascending sort using bubble sort | |
void bubbleSort(vector<Student>& students){ | |
for(int i = students.size()-1;i>0;i--){ | |
bool flag = true; | |
for(int j=i;j>0;j--){ | |
if(students[j].getRegNo()<students[j-1].getRegNo()){ | |
swap(students[j],students[j-1]); | |
flag = false; //One swap detected, array may not be sorted. | |
} | |
} | |
if(flag) return; //No swaps detected, array is sorted | |
} | |
} | |
//Binary search on ascending sorted array | |
Student binarySearchRecursive(vector<Student>& students,long regNo,int leftIndex,int rightIndex){ | |
if(rightIndex<leftIndex) throw "Student not found!"; //Element not found | |
int midIndex = (leftIndex+rightIndex)/2; | |
if(students[midIndex].getRegNo()<regNo) return binarySearchRecursive(students,regNo,leftIndex,midIndex-1); | |
else if(students[midIndex].getRegNo()>regNo) return binarySearchRecursive(students,regNo,midIndex+1,rightIndex); | |
else return students[midIndex]; | |
} | |
//Descending sort using insertion sort | |
void insertionSort(vector<Student>& students){ | |
for(int i=1;i<students.size();i++) | |
for(int j=i;j>0 && students[j].getRegNo()>students[j-1].getRegNo() ;j--) | |
swap(students[j],students[j-1]); | |
} | |
//Print student vector | |
void printAllStudents(vector<Student>& students){ | |
for(int i=0;i<students.size();i++){ | |
students[i].printStudentDetails(); | |
} | |
} | |
int main(){ | |
int n,x; | |
cout<<"Enter no of students: "; | |
cin>>n; | |
vector<Student> s(n); | |
cout<<"Enter details of students in format: \n Regno Firstname lastname branch cgpa\n"; | |
for(int i=0;i<n;i++){ | |
long regno; | |
string fname,lname,branch; | |
float cgpa; | |
cin>>regno>>fname>>lname>>branch>>cgpa; | |
s[i] = Student(regno,fname+" "+lname,branch,cgpa); | |
} | |
bool sorted = false; //Keeps track whether array is sorted in right order for binary search | |
do{ | |
long regNo; | |
cout<<"\n\nSelect one of the following\n"; | |
cout<<"1. Search linearly for regno. \n"; | |
cout<<"2. Bubble Sort by Reg nos.(ascending order)\n"; | |
cout<<"3. Binary search for Reg no. \n"; | |
cout<<"4. Insertion sort by reg nos. (descending order)\n"; | |
cout<<"5. Print the students list\n"; | |
cout<<"Press any other button to exit\n\n"; | |
cout<<"Enter your choice: "; | |
cin>>x; | |
switch(x){ | |
case 0: break; | |
case 1: | |
cout<<"\nEnter the reg no. to be searched: "; | |
cin>>regNo; | |
try{ | |
Student student = linearSearch(regNo,s); | |
student.printStudentDetails(); | |
} catch(const char* err){ | |
cout<<err<<endl; | |
} | |
break; | |
case 2: | |
bubbleSort(s); | |
sorted = true; | |
cout<<"\nStudents list is sorted in ascending order!\n"; | |
break; | |
case 3: | |
if(!sorted) | |
bubbleSort(s); | |
cout<<"Enter regNo to be searched: "; | |
cin>>regNo; | |
try{ | |
Student student = binarySearchRecursive(s,regNo,0,s.size()); | |
student.printStudentDetails(); | |
} catch(const char* err){ | |
cout<<err<<endl; | |
} | |
break; | |
case 4: | |
insertionSort(s); | |
sorted = false; | |
break; | |
case 5: | |
printAllStudents(s); | |
break; | |
default: | |
return 0; | |
} | |
}while(x!=0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment