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

Binary Search and Sorting Algorithms

The document contains implementations of various algorithms in C++, including iterative and recursive binary search, bubble sort, selection sort, and insertion sort. Each algorithm is accompanied by a main function that allows user input for sorting or searching elements in an array. The document demonstrates fundamental sorting and searching techniques commonly used in programming.

Uploaded by

themomgift
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)
7 views10 pages

Binary Search and Sorting Algorithms

The document contains implementations of various algorithms in C++, including iterative and recursive binary search, bubble sort, selection sort, and insertion sort. Each algorithm is accompanied by a main function that allows user input for sorting or searching elements in an array. The document demonstrates fundamental sorting and searching techniques commonly used in programming.

Uploaded by

themomgift
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

Binary search Iterative-

#include <iostream>
using namespace std;

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid; // Element found
else if (arr[mid] < key)
low = mid + 1; // Search right half
else
high = mid - 1; // Search left half
}
return -1; // Element not found
}

int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 10;

int result = binarySearch(arr, n, key);


if (result != -1)
cout << "Element found at index: " << result;
else
cout << "Element not found.";
return 0;
}
Recursive
#include <iostream>
using namespace std;

int binarySearch(int arr[], int low, int high, int key) {


if (low > high)
return -1; // Base case: key not found

int mid = (low + high) / 2;

if (arr[mid] == key)
return mid; // Element found
else if (arr[mid] > key)
return binarySearch(arr, low, mid - 1, key); // Search left
else
return binarySearch(arr, mid + 1, high, key); // Search right
}

int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 10;

int result = binarySearch(arr, 0, n - 1, key);


if (result != -1)
cout << "Element found at index: " << result;
else
cout << "Element not found.";
return 0;
}
Bubble sort
#include<iostream>
using namespace std;
void bubble_sort(int list[30],int n);
int main()
{
int n,i;
int list[30];
cout<<"enter no of elements\n";
cin>>n;
cout<<"enter "<<n<<" numbers ";
for(i=0;i<n;i++)
cin>>list[i];
bubble_sort (list,n);
cout<<" after sorting\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
return 0;
}
void bubble_sort (int list[30],int n)
{
int temp ;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
Selection sort
#include<iostream>
using namespace std;
void selection_sort (int list[],int n);
int main()
{
int n,i;
int list[30];
cout<<"enter no of elements\n";
cin>>n;
cout<<"enter "<<n<<" numbers ";
for(i=0;i<n;i++)
cin>>list[i];
selection_sort (list,n);
cout<<" after sorting\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
return 0;
}
void selection_sort (int list[],int n)
{
int min,temp,i,j;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(list[j]<list[min])
min=j;
}
temp=list[i];
list[i]=list[min];
list[min]=temp;
}
}
Insertion sort
#include<iostream>
using namespace std;
void insertion_sort(int a[],int n)
{
int i,t,pos;
for(i=0;i<n;i++)
{
t=a[i];
pos=i;
while(pos>0&&a[pos-1]>t)
{
a[pos]=a[pos-1];
pos--;
}
a[pos]=t;
}
}
int main()
{
int n,i;
int list[30];
cout<<"enter no of elements\n";
cin>>n;
cout<<"enter "<<n<<" numbers ";
for(i=0;i<n;i++)
cin>>list[i];
insertion_sort(list,n);
cout<<" after sorting\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
return 0;
}

You might also like