0% found this document useful (0 votes)
15 views

Lab# 07 Insertion Sorting

The document contains code for three sorting algorithms: insertion sorting, linear search sorting, and binary search sorting. It includes code snippets to input arrays, sort/search the arrays, and output the results. For insertion sorting, it uses a for loop to iterate through the array and swap elements if out of order. For linear search, it uses a for loop to iterate through the array and check if the search key matches each element. For binary search, it uses a while loop to repeatedly divide the search range in half and check if the key is greater than or less than the middle element to determine the next search range.

Uploaded by

Muhammad Bilal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab# 07 Insertion Sorting

The document contains code for three sorting algorithms: insertion sorting, linear search sorting, and binary search sorting. It includes code snippets to input arrays, sort/search the arrays, and output the results. For insertion sorting, it uses a for loop to iterate through the array and swap elements if out of order. For linear search, it uses a for loop to iterate through the array and check if the search key matches each element. For binary search, it uses a while loop to repeatedly divide the search range in half and check if the key is greater than or less than the middle element to determine the next search range.

Uploaded by

Muhammad Bilal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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