Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<iostream>
using namespace std;
int main()
{
int a[]={34,12,76,31,21,98};
cout<<"Array Before Sorting"<<endl;
for(int i=0;i<6;i++)
{
cout<<a[i]<<endl;
}
int temp,j,k;
for(k=1;k<=6;k++)
{
temp=a[k];
j=k-1;
while(j>=0&&temp<=a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"Array after Sorting"<<endl;
for(int i=0;i<6;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
58 changes: 58 additions & 0 deletions binarysearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<iostream>
using namespace std;
int binary_search(int arr[],int key,int n)
{
int start=0;
int end=n-1;
int mid=(start+end)/2;
while(start<=end)
{
if(arr[mid]==key)
{
return mid;
}
if(key>arr[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}
return -1;
}
int main()
{
int n,i,j,a[50],temp,key,index;
cout<<"Enter the Number of elements in Array : ";
cin>>n;
cout<<"Enter the value of each element in Array"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Sorted Array"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
cout<<"Enter the element to be searched : ";
cin>>key;
index=binary_search(a,key,n);
cout<<"Element "<<key<<" found at Index : "<<index;
return 0;
}