0% found this document useful (0 votes)
42 views6 pages

Aass Lab

This document contains source code for 5 different algorithms: 1. Insertion sort 2. Linear search 3. Selection sort 4. Binary search 5. Bubble sort The source code provides implementations of each algorithm to sort or search arrays of integers. Main functions test each algorithm on sample input arrays and output the results.

Uploaded by

Magarsa Bedasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views6 pages

Aass Lab

This document contains source code for 5 different algorithms: 1. Insertion sort 2. Linear search 3. Selection sort 4. Binary search 5. Bubble sort The source code provides implementations of each algorithm to sort or search arrays of integers. Main functions test each algorithm on sample input arrays and output the results.

Uploaded by

Magarsa Bedasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

WOLKITE UNIVERSITY

COLLEGE OF COMPUTING AND INFORMATICS


DEPARTMENT OF INFORMATION SYSTEM

COURSE TITTLE: DATA STRUCTURE AND ALGORITHM


COURSE CODE: InSy2044

INDIVIDUAL ASSIGNMENT

NAME: AYANTU TILAHUN


ID NO: NSR/0311/13

INSTRUCTOR NAME: HAMDU K. SUBMITTED DATE : 7 /10/2014


//1) insertion sorting
#include<cstdlib>
#include<iostream>
#include<conio.h>
using namespace std;
void insertsort(int dataset[],int n){
int i,j;
for(i=1;i<n;i++){
int pick_item=dataset[i];
bool inserted=false;
for(j=i-1;j>=0 && inserted!=true;){
if(pick_item<dataset[j])
{
dataset[j+1]=dataset[j];
j--;
dataset[j+1]=pick_item;
}
else inserted=true;
}
}
}
int main(){
int arr[5]={23,2,3,34,6};
insertsort (arr,5);
for(int i=0;i<5;i++)
cout<<arr[i]<<endl;
getch();
return 0;
}

//2) linear searching


#include <iostream>
using namespace std;
int main(){
cout<<"enter the size of the array"<<endl;
int size;cin>>size;
int array[size],key,i;
cout<<"enter all elements of the array";
for(int j=0;j<size;j++){
cin>>array[j];
}
for(int a=0; a<size;a++){
cout<<"array["<<a<<"]= ";
cout<<array[a]<<endl;
}
cout<<"enter key to search";
cin>>key;
for(i=0;i<size;i++){
if(key==array[i]){
cout<<"key found at index :"<<i<<endl;
break;
}
}
if(i!=size){
cout<<"the key found at index :"<<i;
}
else{
cout<<"the key is not found in array ";
}
return 0;
}

//3) selection sorting


#include<iostream>
#include <cstdlib>
using namespace std;
void selection_sort(int A[],int n){
int i,j,min;
for(i=0;i<n;i++){
min=i;
for(j=i+1;j<n;j++)
if(A[j]<A[min])min=j;
int temp=A[i];
A[i]=A[min];
A[min]=temp;
}
}
int main(){
int A[]={23,4,45,23,12,2};
selection_sort(A,6);
for(int i=0;i<6;i++)
cout<<A[i]<<endl;
system("pause");

return EXIT_SUCCESS;
}
//4) binary searching
#include <iostream>
using namespace std;
void bubblesort(int array[],int size);
bool binarysearch(int array[],int size,int key);
int main()
{
cout<<"enter 5 numbers randomly :"<<endl;
int array[5];
for(int i=0;i<5;i++)
{
cout<<"/t";
cin>>array[i];
}
bubblesort(array,5);
cout<<"/n/t/t/tenter key to search:";
int key;
cin>>key;
int result=binarysearch(array,5,key);
if(result==1)
cout<<"/n/t/t/tkey found in array"<<endl;
else
cout<<"/n/t/t/tkey not found in array"<<endl;
return 0;
}
void bubblesort(int array[],int size)
{
cout<<"input array is:"<<endl;
for(int j=0;j<size;j++)
{
cout<<"/t/t/tvalue at"<<j<<"index:"<<array[j]<<endl;
}
cout<<endl;
int temp;
for(int i2=0;i2<size;i2++)
{
for(int j=0;j<size-1;j++){

if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<"sorted array is:"<<endl;
for(int i3=0;i3<size;i3++)
{
cout<<"/t/t/tvalue at"<<i3<<"index:"<<array[i3]<<endl;
}

}
bool binarysearch(int array[],int size,int key)
{
int start=1,end=size;
int mid=(start+end)/2;
while(start<=end&&array[mid]!=key)
{
if(array[mid]<key)
{
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;
}
if(array[mid]==key)
return true;
else
return false;
cout<<"/n/n/n";
}

//5)bubble sorting
#include<iostream>
using namespace std;
int main(){
int array[5];
cout<<"enter five number randomly"<<endl;
for(int i=0; i<5; i++){
cin>>array[i];
}
cout<<endl;
cout<<"input array is : "<<endl;
for(int j=0;j<5;j++){
cout<<"\t\t\tValue at "<<j<<" index :"<<array[j]<<endl;
}
cout<<endl;
int temp;
for(int i2=0;i2<=4;i2++){
for(int j=0;j<4;j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<" Sorted array is "<<endl;
for(int i3=0;i3<5;i3++){
cout<<"\t\t\tValue at "<<i3<<"index:"<<array[i3]<<endl;
}
return 0;
}

You might also like