-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray in data structures.cpp
226 lines (223 loc) · 3.78 KB
/
array in data structures.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include<iostream>
#include<conio.h>
#include<time.h>
using namespace std;
class Array
{
int array[100000];
int size;
time_t start,end;
public:
void insert();
void display();
void bubble_sort();
void insertion_sort();
void Linear_search();
void Binary_search();
void insertion();
void deletion();
void choice();
};
void Array::Binary_search()
{
bubble_sort();
int lower=0, upper=size-1,midle,item;
cout<<"Enter the item which you want to search\n";
cin>>item;
while(lower<=upper)
{
midle=(lower+upper)/2;
if(item==array[midle])
{
cout<<"Element Found at : "<<midle;
getch();
return;
}
else if(item<array[midle])
{
upper=midle-1;
}
else
{
lower=midle+1;
}
}
cout<<"Item not found\n";
getch();
}
void Array::insertion_sort()
{
int i,j,temp;
for(i=1;i<size;i++)
{
temp=array[i];
for(j=(i-1);j>=0 && temp<array[j];j--)
{
array[j+1]=array[j];
}
array[j+1]=temp;
}
}
void Array::insert()
{
int i,temp;
cout<<"Enter number of elements of the array\n";
cin>>size;
temp=size;
for(i=0;i<size;i++)
{
array[i]=temp--;
}
}
void Array::deletion()
{
int delnum,i,j;
cout<<"Enter the number which you want to delete\n";
cin>>delnum;
for(i=0;i<size;i++)
{
if(array[i]==delnum)
{
for(j=i+1;j<size;j++)
{
array[j-1]=array[j];
}
break;
}
if(array[i]!=delnum && i==size-1)
{
cout<<"Element not found\n";
getch();
}
}
size=size-1;
}
void Array::display()
{
int i;
for(i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
}
void Array::bubble_sort()
{
int temp,i,j;
for(i=0;i<size;i++)
{
for(j=1;j<size-i;j++)
{
temp=array[j-1];
if(temp>array[j])
{
array[j-1]=array[j];
array[j]=temp;
}
}
}
}
void Array::insertion()
{
int data,index,i;
cout<<"Enter the element which you want to insert\n";
cin>>data;
cout<<"Enter the position where you want to enter\n";
cin>>index;
if(index<size)
{
for(i=size;i>index;i--)
{
array[i]=array[i-1];
}
array[index]=data;
size=size+1;
}
else
cout<<"Position is invalid\n";
}
void Array::Linear_search()
{
int number,i;
cout<<"Enter the element you want to search\n";
cin>>number;
for(i=0;i<size;i++)
{
if(array[i]==number)
{
cout<<"Element Found : "<<i;
break;
}
if(array[i]!=number && i==size-1)
{
cout<<"Element not found\n";
}
}
}
void Array::choice()
{
int k;
while(1)
{
system("cls");
cout<<"1.Insert Elements in the array\n2.Display the elements\n3.Bubble Sort\n4.Insertion Sort\n5.Linear Search\n6.Binary Search\n7.Insertion\n8.Delete the element from the array\n9.Exit\nEnter Your choice\n";
cin>>k;
switch(k)
{
Array A1;
case 1:
A1.insert();
break;
case 2:
start=time(NULL);
A1.display();
end=time(NULL);
cout<<"Execution time to display the element is=\n"<<(end-start);
getch();
break;
case 3:
start=time(NULL);
A1.bubble_sort();
end=time(NULL);
cout<<"Execution time in bubble sorting is=\n"<<(end-start);
getch();
break;
case 4:
start=time(NULL);
A1.insertion_sort();
end=time(NULL);
cout<<"Execution time in insertion sorting is=\n"<<(end-start);
getch();
break;
case 5:
start=time(NULL);
A1.Linear_search();
end=time(NULL);
cout<<"Execution time in Linear search is=\n"<<(end-start);
getch();
break;
case 6:
start=time(NULL);
A1.Binary_search();
end=time(NULL);
cout<<"Execution time in Binary search is=\n"<<(end-start);
getch();
break;
case 7:
A1.insertion();
break;
case 8:
A1.deletion();
break;
case 9:
exit(0);
default:
cout<<"\nWrong choice\n";
}
}
}
int main()
{
Array A1;
A1.choice();
return 0;
}