Professional Documents
Culture Documents
Searching and Sorting
Searching and Sorting
.
.
.
.
.
int binarySearch(int *input, int n, int val)
{
//Write your code here
int start=0,end=n-1;
int mid;
while(end>=start)
{
mid = (start + end) / 2;
if (val == input[mid]) {
return mid;
}
else if(val>input[mid] ){
start=mid+1;
}
else if(val<input[mid])
{
end=mid-1;
} else {
return 0;
}
}
return -1;
}
SELECTION SORT
.
.
.
.
.
#include <iostream>
using namespace std;
void print(int k[] , int a)
{
for(int i=0;i<a;i++)
{
cout<<k[i]<<" ";
}
}
.
.
.
..
.
.
.
.
.
BUBBLE SORT
.
.
.
.
.
.
.
.
void bubbleSort(int *input, int size)
{
//Write your code here
for (int step = 0; step < (size - 1); ++step) {
swapped = 1;
}
}