0% found this document useful (0 votes)
7 views15 pages

Rohit ADA

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)
7 views15 pages

Rohit ADA

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/ 15

CONTENT

S.NO Title the Experiment Page no. Sign

1. Write a program for Binary Search program. 3-4

2. Write a program for Quick Sort program. 5-6

3. Write a program for Heap Sort program. 7-8

4. Write a program for Insertion program. 9-10

5. Write a program for Bubble Sort Program. 11-12

6. Write a program for Linear Search Program. 13-14

7. write a program for prims algorithm 15-16

1 | Page
Que.1 Write a program for Binary Search program.

#include<iostream>
using namespace std;

int binarySearch(int arr[],int target,int left,int right){


while(left<=right){
int mid=left+(right-left)/2;

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

int partition(int arr[],int low,int high){


int pivot=arr[high];
int i=low-1;

for(int j=low; j<high; j++){


if(arr[j]<pivot){
i++;
swap(&arr[i],&arr[j]);
}
}

swap(&arr[i+1],&arr[high]);
return i+1;
}

void quickSort(int arr[], int low,int high){


if(low<high){
int pi=partition(arr,low,high);

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;

void heapify(int arr[], int n, int i){


int largest=i;
int left=2*i+1;
int right=2*i+2;

if(left<n && arr[left]>arr[largest]){


largest=left;
}

if(right<n && arr[right]>arr[largest]){


largest=right;
}

if(largest!=i){
swap(arr[i], arr[largest]);
heapify(arr,n,largest);
}
}

void heapSort(int arr[],int n){


for(int i=n/2-1; i>0; i--){
heapify(arr,n,i);

for(int i=n-1; i>0; i--){


swap(arr[0],arr[i]);
heapify(arr,i,0);
}
}

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;

void insertionSort(int arr[],int n){


int i, key,j;
for(i=1; i<n; i++){
key=arr[i];
j=i-1;
while(j>=0 && arr[i]>key){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}

void printArray(int arr[],int n){


for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl
}

int main(){
int arr[]={12,11,13,5,6};
int n=sizeof(arr)/sizeof(arr[0]);

cout<<"Orginal array: ";


printArray(arr,n);

insertionSort(arr,n);

cout<<"Sorted array: ";


printArray(arr,n);

return 0;
}

8 | Page
4 OUTPUT:-

9 | Page
Que.5 Write a program for Bubble Sort Program.

#include<iostream>
using namespace std;

void bubbleSort(int arr[],int n){


for(int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j]>arr[j+1]){
//swap the elements

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]);

cout<<"Array before sorting:";


for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}

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;

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


if(arr[i]==target){
foundIndex=i;
break;
}
}

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 {

public void Prim(int G[][], int V) {

int INF = 9999999;

int no_edge; // number of edge

boolean[] selected = new boolean[V];

Arrays.fill(selected, false);

no_edge = 0;

selected[0] = true;

System.out.println("Edge : Weight");

while (no_edge < V - 1) {


int min = INF;
int x = 0; // row number
int y = 0; // col number

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


if (selected[i] == true) {
for (int j = 0; j < V; j++) {
// not in selected and there is an edge
if (!selected[j] && G[i][j] != 0) {
if (min > G[i][j]) {
min = G[i][j];
x = i;
y = j;
}
}
}
}
}
System.out.println(x + " - " + y + " : " + G[x][y]);
selected[y] = true;
14 | P a g e
no_edge++;
}
}

public static void main(String[] args) {


PGraph g = new PGraph();

int V = 5;

int[][] G = { { 0, 9, 75, 0, 0 }, { 9, 0, 95, 19, 42 }, { 75, 95, 0, 51, 66 }, { 0, 19,


51, 0, 31 },
{ 0, 42, 66, 31, 0 } };

g.Prim(G, V);
}
}

7 Output

Output mst

15 | P a g e

You might also like