Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

/* Name:-Ankit Ananda Gholase

Branch:-IT
Roll no:-SI16
Batch:-A

Assignment no:-1 */

#include <iostream>
#include<string.h>
using namespace std;
struct student
{
int Rollno;
char Name[10];
float SGPA;
};
void getdata(struct student S[10],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\nEnter Rollno,Name,SGPA of student:"<<i<<"\t";
cin>>S[i].Rollno>>S[i].Name>>S[i].SGPA;

}
}
void putdata(struct student S[10],int n)
{
cout<<"\n Rollno\tName\tSGPA";
for(int i=0;i<n;i++)
cout<<"\n"<<S[i].Rollno<<"\t"<<S[i].Name<<"\t"<<S[i].SGPA;

}
void bubble(struct student a[10],int n)
{
for(int i=0;i<n-1 ;i++)
{
for(int j=0;j<n-i-1;j++)

{ if(a[j].Rollno>a[j+1].Rollno)

{ struct student t=a[j];

a[j]=a[j+1];
a[j+1]=t;

}
}
}
}

void insertion(struct student a[10],int n)


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

{
struct student x;
int j;
x=a[i];
for( j=i-1;j>=0;j--)

{
if(strcmp(x.Name,a[j].Name)<0)

a[j+1]=a[j];
else break;
}
a[j+1]=x;
}

int partition(struct student s[20],int left,int right)


{
struct student pivot,temp;
pivot = s[left];
int Pos_pivot=left;
while (left < right)
{
while ((s[right].SGPA < pivot.SGPA) && (left <= right))
right--;
while ((s[left].SGPA >= pivot.SGPA) && (left <= right))
left++;
if(left<right)
{ temp=s[right];
s[right] = s[left];
s[left] =temp; }
}
if(Pos_pivot!=right)

{ s[Pos_pivot]=s[right]; s[right]=pivot; }

return right;

}
void quicksort(struct student s[20], int left, int right)
{
int pivot=partition(s,left,right);

if (left < pivot)


{
quicksort(s, left, pivot-1);
}
if (right > pivot)
{
quicksort(s, pivot+1, right);
}

}
int linearSearch(struct student values[20], int n,int Key)
{ int flag=0;
cout<<"\nRollno\tName\tSGPA";
for(int i = 0; i < n; i++)
{
if (values[i].SGPA == Key )
{
flag=1;

cout<<"\n"<<values[i]. Rollno<<"\t";
cout<<values[i].Name<<"\t"<<values[i].SGPA;
}
}
return flag;
}
int BinarySearchN(struct student S[20], int N,char value[20])
{

int Low = 0, High = N - 1,Mid,count=0;


cout<<"\nEnter the name of student to find it's Record: ";
cin>>value;
while (Low <= High)
{
Mid = (Low + High) / 2;
int diff=strcmp(S[Mid].Name,value);
if (diff >0)
High = Mid - 1;
else if (diff<0)
Low = Mid + 1 ;
else
return Mid;
}
return -1;
}
int main()
{ int n,ch;
char Name[20];
int i,j,k;
struct student S[20];
cout<<"Enter number of records ";
cin>>n;
getdata(S,n);
do
{cout<<"\n1:Bubble sort in ascending order of there Roll no.\n2:Insertion sort
alphabetically\n3:Quicksort according to first ten toppers from a class\
n4:Linearsearch SGPA of a student\n5:Binary search according student name\n6:Exit
";
cout<<"\n\nEnter the choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nRecord of students in ascending order of there Roll no.";
bubble(S,n);
putdata(S,n);
cout<<"\n\n";
break;

case 2:
cout<<"\n\nList of records alphabetically";
insertion(S,n);
putdata(S,n);
cout<<"\n\n";
break;
case 3:
cout<<"\nList of students according to first ten toppers from a class";
quicksort(S,0,n-1);
putdata(S,n);
cout<<"\n\n";
break;

case 4:
float find;
cout<<"\nEnter SGPA of a student to find record: ";
cin>>find;
int flag;
flag=linearSearch(S,n,find);
if(flag==0) cout<<"\n Record Not present";
cout<<"\n\n";
putdata(S,n);
break;

case 5:
char Name[20];
insertion(S,n);
cout<<"Enter the Name : ";
cin>>Name;
int pos;
pos=BinarySearchN(S,n,Name);

if (pos==-1)

cout<<"Record Not present";


else

cout<<"\n\nRollno\tName\tSGPA";
for(i = pos; i>=0; i--)
if(strcmp(S[i].Name,Name)!=0)
break;
for(j = pos; j<n; j++)
if(strcmp(S[j].Name,Name)!=0)
break;
for(k = i+1; k<j; k++)
cout<<"\n"<<S[k].Rollno<<"\t"<<S[k].Name<<"\t"<<S[k].SGPA<<"\
n";

break;

case 6:
return 1;

}
}while(ch<7);
}

/* Name - Ankit Ananda Gholase


Roll no - SI16
Batch- A
Enter number of records 5

Enter Rollno,Name,SGPA of student:0 1 Ankit 7

Enter Rollno,Name,SGPA of student:1 3 Kaushal 6

Enter Rollno,Name,SGPA of student:2 2 Bhagat 9

Enter Rollno,Name,SGPA of student:3 5 Rushi 8.6

Enter Rollno,Name,SGPA of student:4 4 Om 9.3

1:Bubble sort in ascending order of there Roll no.


2:Insertion sort alphabetically
3:Quicksort according to first ten toppers from a class
4:Linearsearch SGPA of a student
5:Binary search according student name
6:Exit

Enter the choice 1

Record of students in ascending order of there Roll no.


Rollno Name SGPA
1 Ankit 7
2 Bhagat 9
3 Kaushal 6
4 Om 9.3
5 Rushi 8.6

1:Bubble sort in ascending order of there Roll no.


2:Insertion sort alphabetically
3:Quicksort according to first ten toppers from a class
4:Linearsearch SGPA of a student
5:Binary search according student name
6:Exit

Enter the choice 2

List of records alphabetically


Rollno Name SGPA
1 Ankit 7
2 Bhagat 9
3 Kaushal 6
4 Om 9.3
5 Rushi 8.6

1:Bubble sort in ascending order of there Roll no.


2:Insertion sort alphabetically
3:Quicksort according to first ten toppers from a class
4:Linearsearch SGPA of a student
5:Binary search according student name
6:Exit

Enter the choice 3

List of students according to first ten toppers from a class


Rollno Name SGPA
4 Om 9.3
2 Bhagat 9
5 Rushi 8.6
1 Ankit 7
3 Kaushal 6

1:Bubble sort in ascending order of there Roll no.


2:Insertion sort alphabetically
3:Quicksort according to first ten toppers from a class
4:Linearsearch SGPA of a student
5:Binary search according student name
6:Exit

Enter the choice 4

Enter SGPA of a student to find record: 7

Rollno Name SGPA


1 Ankit 7

Rollno Name SGPA


4 Om 9.3
2 Bhagat 9
5 Rushi 8.6
1 Ankit 7
3 Kaushal 6
1:Bubble sort in ascending order of there Roll no.
2:Insertion sort alphabetically
3:Quicksort according to first ten toppers from a class
4:Linearsearch SGPA of a student
5:Binary search according student name
6:Exit

Enter the choice 5


Enter the Name : Om

Enter the name of student to find it's Record: Om

Rollno Name SGPA


4 Om 9.3

1:Bubble sort in ascending order of there Roll no.


2:Insertion sort alphabetically
3:Quicksort according to first ten toppers from a class
4:Linearsearch SGPA of a student
5:Binary search according student name
6:Exit

Enter the choice 6 */

You might also like