-
Notifications
You must be signed in to change notification settings - Fork 3
/
LAB1.cpp
157 lines (144 loc) · 3.34 KB
/
LAB1.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<conio.h>
#include<stdio.h>
#include<cstdlib>
#include<iostream>
using namespace std;
int aray[10]={1,2,3,4,5,6,7,8,9,10};
void linear_search(int a);
void bienary_search(int b);
int main(){
int n;
int w;
char x;
cout<<"Search or Sort?(S/T)\n";
cin>>x;
if(x=='S'){
cout<<"Which No. is to be searched?\n"<<endl;
cin>>n;
cout<<"Which Way?\n1. Linear search\n2. Binary Search\n"<<endl;
cin>>w;
switch(w){
case 1:
linear_search(n);
system("pause");
break;
case 2:
bienary_search(n);
system("pause");
break;
}
}else{
if(x=="T"){
cout<<"Which Way?\n1. Bubble sort\n2. Insertion Sort\n3. Selection Sort\n"<<endl;
cin>>w;
switch(w){
case 1:
bubble_sort(n);
system("pause");
break;
case 2:
insertion_sort(n);
system("pause");
break;
case 3:
selection_sort(n);
system("pause");
break;
}
}else{
cout<<"Wrong param check for S/T..\n";
main();
}
}
}
void linear_search(int b)
{
bool flag=false;
int i=0;
while(aray[i]!='\0'){
if(aray[i]==b){
cout<<b<<"is found at "<<(i+1)<<"Position"<<endl;
flag = true;
}
i++;
}
if(flag==false){
char r;
cout<<"No such No. can be seen in the array!!\n"<<endl;
cout<<"Retry? (y/n)\n"<<endl;
cin>>r;
if(r=='y'){
main();
}else{
exit(0);
}
}
}
void bienary_search(int b)
{
int r;
int initial = 0;
int final = sizeof(aray)/sizeof(aray[0]);
int mid;
int location = -(final+1);
while(initial<=final){
mid=(initial+final)/2;
if(array[mid]==b){
location=mid;
break;
}
if(r<array[mid])
final=mid-1;
if(r>array[mid])
final=mid+1;
}
if(location==-11)
cout<<"Required number not found"<<endl;
else
cout<<"Required number is found at Index"<<location<<endl;
getch();
}
void bubble_sort(int b)
{
int n,i,j,temp;
for(i=1;i<n;i++){
for(j=0;j<(n-1);++j){
if(aray[j]>aray[j+1]){
temp=aray[j];
aray[j]=aray[j+1];
aray[j+1]=temp;
}
}
}
cout<<"Array after Bubble Sort.."<<endl;
for(i=0;i<n;i++){
cout<<" "<a[i];
}
}
void selection_sort(int size)
{
int min_ele_loc;
}
void insertion_sort(int b)
{
bool flag=false;
int i=0;
while(aray[i]!='\0'){
if(aray[i]==b){
cout<<b<<"is found at "<<(i+1)<<"Position"<<endl;
flag = true;
}
i++;
}
if(flag==false){
char r;
cout<<"No such No. can be seen in the array!!\n"<<endl;
cout<<"Retry? (y/n)\n"<<endl;
cin>>r;
if(r=='y'){
main();
}else{
exit(0);
}
}
}