Rohit ADA
Rohit ADA
1 | Page
Que.1 Write a program for Binary Search program.
#include<iostream>
using namespace std;
if(arr[mid]==target){
return mid;
}
else if(arr[mid]<target){
left=mid+1;
}
else{
right=mid-1;
}
}
return-1;
}
int main(){
int arr[]={2,5,8,12,16,23,38,56,72,91};
int target=23;
int n=sizeof(arr)/sizeof(arr[0]);
int foundIndex=binarySearch(arr,target,0,n-1);
if(foundIndex!=-1){
cout<<"element found at index:"<<foundIndex<<endl;
}
else{
cout<<"element not found in the array."<<endl;
}
return 0;
}
2 | Page
1 OUTPUT:-
3 | Page
Que.2 Write a program for Quick Sort program.
#include<iostream>
using namespace std;
void swap(int*a,int*b){
int t=*a;
*a=*b;
*b=t;
}
swap(&arr[i+1],&arr[high]);
return i+1;
}
quickSort(arr,low,pi-1);
quickSort(arr,pi+1,high);
}
}
int main(){
int arr[]={38,27,43,3,9,82,10};
int n=sizeof(arr)/sizeof(arr[0]);
quickSort(arr,0,n-1);
cout<<"Sorted array:";
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
4 | Page
2 OUTPUT:-
5 | Page
Que.3 Write a program for Heap Sort program .
#include<iostream>
using namespace std;
if(largest!=i){
swap(arr[i], arr[largest]);
heapify(arr,n,largest);
}
}
int main(){
int arr[]={12,11,13,5,6,7};
int n=sizeof(arr)/sizeof(arr[0]);
heapSort(arr,n);
cout<<"Sorted array:";
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
6 | Page
3 OUTPUT:-
7 | Page
Que.4 Write a program for Insertion program.
#include<iostream>
using namespace std;
int main(){
int arr[]={12,11,13,5,6};
int n=sizeof(arr)/sizeof(arr[0]);
insertionSort(arr,n);
return 0;
}
8 | Page
4 OUTPUT:-
9 | Page
Que.5 Write a program for Bubble Sort Program.
#include<iostream>
using namespace std;
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main(){
int arr[]={64,34,25,12,22,11,90};
int n=sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr,n);
cout<<"\nArray after sorting:";
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
return 0;
}
10 | P a g e
5 OUTPUT:-
11 | P a g e
Que.6 Write a program for Linear Search.
#include<iostream>
using namespace std;
int main(){
int arr[]={2,5,8,12,16,23,38,56,72,91};
int target=23;
int n=sizeof(arr)/sizeof(arr[0]);
int foundIndex= -1;
if(foundIndex!=-1){
cout<<"Element found at index:"<<foundIndex<<endl;
}
else
{
cout<<"Element not found in the array."<<endl;
}
return 0;
}
12 | P a g e
6 OUTPUT:-
13 | P a g e
Q.7 write a program for prims algorithm
import java.util.Arrays;
class PGraph {
Arrays.fill(selected, false);
no_edge = 0;
selected[0] = true;
System.out.println("Edge : Weight");
int V = 5;
g.Prim(G, V);
}
}
7 Output
Output mst
15 | P a g e