-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu-driven.cpp
More file actions
115 lines (114 loc) · 1.85 KB
/
Menu-driven.cpp
File metadata and controls
115 lines (114 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream.h>
#include<conio.h>
int binary(int B[],int size,int item);
void selection(int [],int);
void bubble(int [],int);
void insertion(int A[],int size);
void main()
{
cout<<"Enter the size of the array\n";
int size;
cin>>size;
int arr[80];
for(int i=0;i<size;i++)
{
cout<<"Enter the element at position "<<i+1<<" of the array\n";
cin>>arr[i];
}
cout<<"enter your choice,i.e,1 for selection sort and 2 for bubble sort and 3 for insertion sort\n";
int choice;
cin>>choice;
switch(choice)
{
case 1:selection(arr,size);
break;
case 2:bubble(arr,size);
break;
case 3:insertion(arr,size);
break;
}
cout<<"\nEnter the elements to be searched\n";
int item;
cin>>item;
int index=binary(arr,size,item);
if(index==-1)
cout<<"\nsorry!!given element could not be found.\n";
else
cout<<"\nElement found at index:"<<index<<",position:"<<index+1<<endl;
getch();
clrscr();
}
void selection(int arr1[],int size)
{
int small,pos,tmp;
for(int i=0;i<size-1;i++)
{
small=arr1[i];
pos=i;
for(int j=i+1;j<size;j++)
{
if(arr1[j]<small)
{
small=arr1[j];
pos=j;
}
}
tmp=arr1[i];
arr1[i]=arr1[pos];
arr1[pos]=tmp;
cout<<"\nArray after pass "<<i+1<<" is: ";
for(int m=0;m<size;m++)
cout<<arr1[m]<<" ";
}
}
void bubble(int arr1[],int size)
{
int tmp;
for(int i=0;i<size;i++)
{
for(int j=0;j<(size-1)-i;j++)
{
if(arr1[j]>arr1[j+1])
{
tmp=arr1[j];
arr1[j]=arr1[j+1];
arr1[j+1]=tmp;
}
}
cout<<"\nArray after pass "<<i+1<<" is: ";
for(int m=0;m<size;m++)
cout<<arr1[m]<<" ";
}
}
void insertion(int A[],int size)
{
int i,j,key;
for(i=1;i<size;i++)
{
key=A[i];
j=i-1;
while(j>=0 && A[j]>key)
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=key;
}
cout<<"\nArray after sorting is: ";
for(int m=0;m<size;m++)
cout<<A[m]<<" ";
}
int binary(int B[],int size,int item)
{
int beg,last,mid;
beg=0;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==B[mid])return mid;
else if(item>B[mid])beg=mid+1;
else
last=mid-1;
}
return-1;
}