You are on page 1of 4

Lab# 07 insertion sorting

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a[16], i, j, k, temp;
cout<<"enter the elements\n";
for (i = 0; i < 16; i++)
{
cin>>a[i];
}
for (i = 1; i < 16; i++)
{
for (j = i; j >= 1; j--)
{
if (a[j] < a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
else
break;
}
}
cout<<"sorted array\n"<<endl;
for (k = 0; k < 16; k++)
{
cout<<a[k]<<endl;
}
getch();
}
Lab# 07 liner surch sorting

#include<iostream>
using namespace std;

int main()
{
cout<<"Enter The Size Of Array: ";
int size;
cin>>size;

int array[size], key,i;

// Taking Input In Array


for(int j=0;j<size;j++){
cout<<"Enter "<<j<<" Element: ";
cin>>array[j];
}

//Your Entered Array Is


for(int a=0;a<size;a++){
cout<<"array[ "<<a<<" ] = ";
cout<<array[a]<<endl;
}

cout<<"Enter Key To Search in Array";


cin>>key;

for(i=0;i<size;i++){
if(key==array[i]){
cout<<"Key Found At Index Number : "<<i<<endl;
break;
}
}
if(i != size){
cout<<"KEY FOUND at index : "<<i;
}
else{
cout<<"KEY NOT FOUND in Array ";
}
return 0;
}

Lab# 07 Binary surch

#include<conio.h>
#include<iostream>
using namespace std;

int main()
{
//declaring variables and array
int max = 10;
int i,j,s,m,location,k;
int a[max];
//Taking input(Numbers) from user.
for(i=0;i<max;i++)
{
cout<<"Enter "<<(i+1)<<"th Number: ";
cin>>a[i];
}
//Asking user to enter the number he/she wants to search
cout<<"\nEnter number to search: ";
cin>>s;
//Searching the number
i = 0;
j = max;
while (i < j)
{
m = ((i+j)/2);
if (s > a[m])
i = m+1;
else
j = m;
}
if (s == a[i])
{ //if searched number is equal to a[i]
cout<<"\n\nEntered number ("<<s<<") is at "<<(i+1)<<"th position.";
}
else
{ //if the searched number is not in the list
cout<<"\n\nEntered number is not in the list.";
}

cout<<"\n\nPress any key to exit.";


getch();
return 0;
}

You might also like