0% found this document useful (0 votes)
5 views3 pages

Data Structure and Algorithm Lab Exercise

The document outlines various sorting and searching algorithms implemented in C++, including Linear Search, Binary Search, Insertion Sort, Selection Sort, and Bubble Sort. Each exercise contains code snippets demonstrating the algorithm's functionality, along with prompts for user input. The exercises are structured sequentially, with each focusing on a different algorithm and its implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Data Structure and Algorithm Lab Exercise

The document outlines various sorting and searching algorithms implemented in C++, including Linear Search, Binary Search, Insertion Sort, Selection Sort, and Bubble Sort. Each exercise contains code snippets demonstrating the algorithm's functionality, along with prompts for user input. The exercises are structured sequentially, with each focusing on a different algorithm and its implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Data structure and Algorithm Lab Exercise

Exercise 1: Linear Search else{


#include<iostream> if(key<list[mid])
using namespace std;; right=mid-1;
int Linear_Search(int list[], int key) else
{ left=mid+1;
int index=0; }
int found=0; }while(found==0&&left<=right);
do{ if(found==0)
if(key==list[index]) {
{ cout<<key<<" is not found in the list "<<endl;
found=1; index=-1;
cout<<key<<" found at index "<<index<<endl; }
} else
else {
index++; index=mid;
}while(found==0&&index<5); cout<<"the data element "<<key<<" found at index
if(found==0) "<<index<<endl;
index=-1; }
return index; return index;
} }
int main() int main()
{ {
int list[5]; int list[10];
int x=0; for(int i=0;i<10;i++)
for(x=0;x<5;x++) {
{ cout<<"Please Enter your data at "<<"list["<<i<<"]"<<endl;
cout<<"Enter The Elements of list at index: "<<x<<endl; cin>>list[i];// the data u enter should be sorted
cin>>list[x]; }
} int key;
int key; cout<<"Please enter the data you want to search "<<endl;
cout<<"the data element to be searched: "<<endl; cin>>key;
cin>>key; Binary_Search(list, key);
Linear_Search(list, key); }
} //End of exercise 2
//End of Exercise 1 Exercise 3: Insertion Sort
Exercise 2: Binary Search #include<iostream>
#include<iostream> #include<string>
using namespace std; using namespace std;
int Binary_Search(int list[],int key) void insertion(int list[]);
{ int main()
int left=0; {
int right=9; int num[]={20,12,67,18,50};
int found=0; insertion(num);
int index; }
int mid; void insertion(int list[])
do{ {
mid=(left+right)/2; int temp;
if(key==list[mid])
found=1;

1|Page
for(int i=1;i<5;i++) cout<<list[i]<<",";
{ }
temp=list[i]; cout<<endl;
for(int j=i; j>0 && temp<list[j-1];j--) }
{ // work backwards through the array finding where temp cout<<"the final sorted list is: \n";
should go for(int i=0; i<5;i++)
list[j]=list[j-1]; {
list[j-1]=temp; cout<<list[i]<<endl;
}//end of inner loop }}
cout<<"the list after "<<i<<" iteration is: "; // End of exercise 4
for(int i=0;i<5;i++) Exercise 5: Bubble Sort
{ #include<iostream>
cout<<list[i]<<","; #include<string>
} using namespace std;
cout<<endl; void bubble(int list[]);
} int main()
cout<<"the final sorted list is: \n"; {
for(int i=0; i<5;i++) int num[]={20,12,67,18,50};
{ bubble(num);
cout<<list[i]<<endl; }
}} void bubble(int list[])
//End of exercise 3 {
Exercise 4: Selection Sort int temp;
#include<iostream> for(int i=0;i<5;i++)
#include<string> {
using namespace std; for(int j=4;j>i;j--)
void selection (int list[]); {
int main() { if(list[j]<list[j-1])
int num[]={20,12,67,18,50}; {
selection(num); temp=list[j];
} list[j]=list[j-1];
void selection (int list[]) list[j-1]=temp;
{ }
int temp,smallest; }//end of inner loop
for(int i=0;i<5;i++) cout<<"the list after "<<i+1<<" iteration: ";
{ for(int i=0;i<5;i++)
smallest=i; {
for(int j=i+1; j<5;j++) cout<<list[i]<<",";
{ }
if(list[smallest]>list[j]) cout<<endl;
{ }
smallest=j; cout<<"the final sorted list is: \n";
} for(int i=0; i<5;i++)
}//end of inner loop {
temp=list[i]; cout<<list[i]<<endl;
list[i]=list[smallest]; }
list[smallest]=temp; }
cout<<"the list after "<<i+1<<" iteration: "; //End of exercise 5
for(int i=0;i<5;i++)
{

2|Page
3|Page

You might also like