0% found this document useful (0 votes)
9 views9 pages

Dsa 1

The document describes functions to manage student records including getting data, putting data, sorting records by roll number, name, and SGPA. It also includes functions for linear and binary search of records.

Uploaded by

Isha Velankar
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)
9 views9 pages

Dsa 1

The document describes functions to manage student records including getting data, putting data, sorting records by roll number, name, and SGPA. It also includes functions for linear and binary search of records.

Uploaded by

Isha Velankar
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/ 9

#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);
}

/*********** OUTPUT ************/


/*
Enter number of records 15

Enter Rollno,Name,SGPA of student:0 2


sanket
9

Enter Rollno,Name,SGPA of student:1 3


shridhar
8.8

Enter Rollno,Name,SGPA of student:2 1


omkar
9.3

Enter Rollno,Name,SGPA of student:3 6


vikrant
8.6

Enter Rollno,Name,SGPA of student:4 5


Hemant
8

Enter Rollno,Name,SGPA of student:5 8


Arun
8

Enter Rollno,Name,SGPA of student:6 9


Aditya
8

Enter Rollno,Name,SGPA of student:7 12


Yogesh
7

Enter Rollno,Name,SGPA of student:8 13


Shri
8

Enter Rollno,Name,SGPA of student:9 11


Sam
8

Enter Rollno,Name,SGPA of student:10 14


Prasad
9

Enter Rollno,Name,SGPA of student:11 7


Viky
8.5

Enter Rollno,Name,SGPA of student:12 15


Omraj
8

Enter Rollno,Name,SGPA of student:13 4


Varun
8.3

Enter Rollno,Name,SGPA of student:14 10


Adi
8.5

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 omkar 9.3
2 sanket 9
3 shridhar 8.8
4 Varun 8.3
5 Hemant 8
6 vikrant 8.6
7 Viky 8.5
8 Arun 8
9 Aditya 8
10 Adi 8.5
11 Sam 8
12 Yogesh 7
13 Shri 8
14 Prasad 9
15 Omraj 8

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
10 Adi 8.5
9 Aditya 8
8 Arun 8
5 Hemant 8
15 Omraj 8
14 Prasad 9
11 Sam 8
13 Shri 8
4 Varun 8.3
7 Viky 8.5
12 Yogesh 7
1 omkar 9.3
2 sanket 9
3 shridhar 8.8
6 vikrant 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
1 omkar 9.3
2 sanket 9
14 Prasad 9
3 shridhar 8.8
6 vikrant 8.6
7 Viky 8.5
10 Adi 8.5
4 Varun 8.3
11 Sam 8
9 Aditya 8
15 Omraj 8
5 Hemant 8
8 Arun 8
13 Shri 8
12 Yogesh 7

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: 8

Roll Name SGPA


11 Sam 8
9 Aditya 8
15 Omraj 8
5 Hemant 8
8 Arun 8
13 Shri 8

Rollno Name SGPA


1 omkar 9.3
2 sanket 9
14 Prasad 9
3 shridhar 8.8
6 vikrant 8.6
7 Viky 8.5
10 Adi 8.5
4 Varun 8.3
11 Sam 8
9 Aditya 8
15 Omraj 8
5 Hemant 8
8 Arun 8
13 Shri 8
12 Yogesh 7
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 : Omraj

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

Roll No Name SGPA


15 Omraj 8

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