0% found this document useful (0 votes)
6 views11 pages

Dsa 1

Uploaded by

prasadborse256
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)
6 views11 pages

Dsa 1

Uploaded by

prasadborse256
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/ 11

DSA PRACTICAL

NAME: karan
agrawal ROLLNO:
01 CLASS: SE IT

PROGRAM:

#include<iostrea
m>
#include<string.h
> using
namespace std;

struct student
{
int rno;
char
name[20];
float spga;
}list;

void accept(struct student list[15], int n);

void display(struct student list[15],


int n); void bubblesort(struct student
list[15], int n);
void insertionsort(struct student list[15],
int n); void quicksort(struct student
list[15], int l,int u); int partition(struct
student list[15], int l,int u);;
void search(struct student list[15], int n, float key);
int binarysearch( struct student list[15], char x[20], int low, int high);

int main()
{
student list[15];
int choice, n, key,
result; char x[20];
do
{
cout<<"\n 1) Create Student
Database "; cout<<"\n 2) Display
Student Records ";
cout<<"\n 3) Arrange list of students according to roll numbers in ascending order
(Bubble Sort) ";
cout<<"\n 4) Arrange list of students alphabetically
(Insertion Sort) "; cout<<"\n 5) Find out first ten toppers
from a class (Quick Sort) ";
cout<<"\n 6) Search students according to SGPA(Linear search) ";
cout<<"\n 7) Search a particular student according to name (Binary
search) "; cout<<"\n 8) Exit ";
cout<<"\n Enetr Your
Choice:="; cin>>choice;

switch (choice)
{
case 1:
cout << "\n Enter no. of Student details to be added (max 15) : ";

cin >> n;
accept(list,
n);

break;

case 2:
display(list, n);
break;

case 3:
bubblesort(list,
n); display(list,
n); break;

case 4:
insertionsort(list,
n); display(list,
n); break;

case 5:
quicksort(list, 0, n-1);
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\
t"<< "Marks"; for(int i=n-1; i>=0; i--)
{
cout<<"\n";
cout<<"\t "<< list[i].rno<<"\t "<<list[i].name<<"\t "<<list[i].spga;
}
break;

case 6:
cout<<"\n Enter the marks which u want to

search:="; cin>>key;
search(list, n,
key); break;

case 7:
cout<<"\n Enter the name of student which u want to
search:="; cin>>x;
insertionsort(list,n);
result=binarysearch(list,x,0,
(n-1)); if(result==-1)
{
cout<<" \n Student name you want to search for is not present ! \n";
}
else
{
cout<<" \n The student is present :\t" << list[result].name;
}
break;

case 8:
default:cout<<"\n Invalid choice !! Please enter your choice
again."<<endl; break;
}

}while(choice!
=8); return 0;
}

void accept(student list[15], int n)


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

{
cout << "\n# Details of Student no. " << i +
1 << " - "; cout<<"\n Enter the Roll
number:=";
cin>>list[i].rno;
cout<<"\n Enter the
Name:=";
cin>>list[i].name;
cout<<"\n Enter the
SPGA:=";
cin>>list[i].spga;
}
}

void display(student list[15], int n)


{
int i;
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\
t"<< "SGPA"; for(i=0;i<n;i++)
{
cout<<"\n";
cout<<"\t "<< list[i].rno<<"\t "<<list[i].name<<"\t "<<list[i].spga;
}

//bubble sort to sort in ascending order on roll


number void bubblesort(student list[15], int n)
{
int i,j;
student temp;
for(i=0;i<n-
1;i++)
{
for(j=0;j<(n-1-i);j++)

{
if(list[j].rno>list[j+1].rno)
{
temp=list[j]
;
list[j]=list[j
+1];
list[j+1]=te
mp;
}
}
}

// insertion sort to sort on names in


ascending order void insertionsort(student
list[15], int n)
{
int j;
student
key;
for (int i = 1; i < n; i++)
{
key= list[i];
j = i - 1; /* Move elements of arr[0..i-1],
that are greater than key, to one
position ahead
of their current position */

while (j >= 0 && strcmp(list[j].name, key.name) >0)


{
list[j + 1]=
list[j]; j = j -
1;
}

list[j + 1]= key;

void quicksort(student list[15], int l, int u)


{
int j;
if(l<
u)
{
j=partition(list,l,
u);
quicksort(list,l,j-
1);
quicksort(list,j+1
,u);
}
}

int partition(student list[15], int l, int u)


{
int i,j;
student temp,
v; v=list[l];
i=l;
j=u+
1;

do
{
do
i+
+;
while(list[i].spga<v.spga &&
i<=u); do
j--;

while(v.spga<list[j].spg
a); if(i<j)
{
temp=list
[i];
list[i]=list
[j];
list[j]=te
mp;
}
}while(i<j);
list[l]=list[j]
; list[j]=v;
return j;
}

// linear search for marks if more than one student having same marks print
all of them void search(student list[15], int n, float key)
{
int t=0;
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\
t"<< "SPGA"; for(int i=0; i<n; i++)
{
if(key==list[i].spga)
{
cout<<"\n\t "<< list[i].rno<<"\t "<<list[i].name<<"\t
"<<list[i].spga; t++;
continue;
}
}
if (t == 0)
{
cout<<" \n Student SGPA you want to search for is not present ! \n";

}
}

int binarysearch(student list[15], char x[20], int low, int high)


{
int mid;
while(low<=hi
gh)
{
mid=(low+high)/2;
if(strcmp(x,list[mid].name)==0)
{
return mid;
}
else if(strcmp(x,list[mid].name)<0)
{
high=mid-1;
}
else
{
low=mid+1;
}
}

return -1;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy