Skip to content

Commit 631fb5d

Browse files
Merge pull request #10 from Muskanbansal18/master
Added a program
2 parents fcf713a + c923b64 commit 631fb5d

File tree

1 file changed

+277
-0
lines changed

1 file changed

+277
-0
lines changed

searchandsort.cpp

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
#include<iostream>
2+
#include<stdlib.h>
3+
using namespace std;
4+
5+
int AB[50];
6+
7+
void input(int a)
8+
{
9+
for(int i=0;i<a;i++)
10+
cin>>AB[i];
11+
cout<<"entered elements are ::";
12+
for(int i=0;i<a;i++)
13+
cout<<AB[i]<<"\t";
14+
cout<<"\n";
15+
}
16+
void printArray( int s)
17+
{
18+
int i;
19+
for (i = 0; i < s; i++)
20+
cout << AB[i] << " ";
21+
cout << endl;
22+
}
23+
void linearSearch(int a)
24+
{
25+
int i;
26+
int x;
27+
cout<<"enter the element to be searched ";
28+
cin>>x;
29+
cout<<endl;
30+
int found=0;
31+
for (i = 0; i < a; i++)
32+
{
33+
if (AB[i] == x)
34+
{
35+
found=1;
36+
break;
37+
}
38+
}
39+
if(found==1)
40+
cout<<"Elemnet found at "<<i+1<<" position"<<endl;
41+
else
42+
cout<<"Not found" ;
43+
}
44+
int binarySearch( int l, int r,int y) // only position of 1st and last element is passed as argument as the array is global
45+
{
46+
47+
if (r >= l) {
48+
int mid = (l + r )/ 2;
49+
50+
// If the element is present at the middle
51+
if (AB[mid] ==y)
52+
return mid;
53+
54+
// If element is smaller than mid, then it can only be present in left
55+
if (AB[mid] > y)
56+
return binarySearch( l, mid - 1,y);
57+
58+
// Else the element can only be present in right s
59+
return binarySearch( mid + 1, r,y);
60+
61+
}
62+
else
63+
return -1;
64+
// We reach here when element is not
65+
// present in array
66+
return -1;
67+
}
68+
void swapped(int *xp, int *yp)
69+
{
70+
int temp = *xp;
71+
*xp = *yp;
72+
*yp = temp;
73+
}
74+
75+
// A function to implement bubble sort
76+
void bubbleSort( int n)
77+
{
78+
int i, j;
79+
for (i = 0; i < n-1; i++)
80+
81+
// Last i elements are already in place
82+
for (j = 0; j < n-i-1; j++)
83+
if (AB[j] > AB[j+1])
84+
swapped(&AB[j], &AB[j+1]);
85+
printArray(n);
86+
}
87+
88+
void selectionSort( int n)
89+
{
90+
int i, j, min_idx;
91+
92+
// One by one move boundary of unsorted subarray
93+
for (i = 0; i < n-1; i++)
94+
{
95+
// Find the minimum element in unsorted array
96+
min_idx = i;
97+
for (j = i+1; j < n; j++)
98+
if (AB[j] < AB[min_idx])
99+
min_idx = j;
100+
101+
// Swap the found minimum element with the first element
102+
swapped(&AB[min_idx], &AB[i]);
103+
}
104+
printArray(n);
105+
}
106+
void insertionSort( int n)
107+
{
108+
int i, key, j;
109+
for (i = 1; i < n; i++)
110+
{
111+
key = AB[i];
112+
j = i - 1;
113+
114+
/* Move elements of AB[0..i-1], that are
115+
greater than key, to one position ahead
116+
of their current position */
117+
while (j >= 0 && AB[j] > key)
118+
{
119+
AB[j + 1] = AB[j];
120+
j = j - 1;
121+
}
122+
AB[j + 1] = key;
123+
}
124+
125+
}
126+
int part ( int low, int high)
127+
{
128+
int pivot = AB[high]; // pivot
129+
int i = (low - 1); // Index of smaller element
130+
131+
for (int j = low; j <= high - 1; j++)
132+
{
133+
// If current element is smaller than the pivot
134+
if (AB[j] < pivot)
135+
{
136+
i++; // increment index of smaller element
137+
swapped(&AB[i], &AB[j]);
138+
}
139+
}
140+
swapped(&AB[i + 1], &AB[high]);
141+
return (i + 1);
142+
}
143+
void quickSort( int low, int high)
144+
{
145+
if (low < high)
146+
{
147+
// pi is partitioning index, AB[p] is nownat right place
148+
int pi = part( low, high);
149+
150+
// Separately sort elements before
151+
// partition and after partition
152+
quickSort( low, pi - 1);
153+
quickSort( pi + 1, high);
154+
}
155+
156+
}
157+
void merger(int l, int m, int r)
158+
{
159+
int i, j, k;
160+
int n1 = m - l + 1;
161+
int n2 = r - m;
162+
163+
/* create temp arrays */
164+
int L[n1], R[n2];
165+
166+
/* Copy data to temp arrays L[] and R[] */
167+
for (i = 0; i < n1; i++)
168+
L[i] = AB[l + i];
169+
for (j = 0; j < n2; j++)
170+
R[j] = AB[m + 1 + j];
171+
172+
/* Merge the temp arrays back into arr[l..r]*/
173+
i = 0; // Initial index of first subarray
174+
j = 0; // Initial index of second subarray
175+
k = l; // Initial index of merged subarray
176+
while (i < n1 && j < n2) {
177+
if (L[i] <= R[j]) {
178+
AB[k] = L[i];
179+
i++;
180+
}
181+
else {
182+
AB[k] = R[j];
183+
j++;
184+
}
185+
k++;
186+
}
187+
188+
/* Copy the remaining elements of L[], if there
189+
are any */
190+
while (i < n1) {
191+
AB[k] = L[i];
192+
i++;
193+
k++;
194+
}
195+
196+
/* Copy the remaining elements of R[], if there
197+
are any */
198+
while (j < n2) {
199+
AB[k] = R[j];
200+
j++;
201+
k++;
202+
}
203+
}
204+
205+
/* l is for left index and r is right index of the
206+
sub-array of arr to be sorted */
207+
void mergeSort( int l, int r)
208+
{
209+
if (l < r) {
210+
// Same as (l+r)/2, but avoids overflow for
211+
// large l and h
212+
int m = l + (r - l) / 2;
213+
214+
// Sort first and second halves
215+
mergeSort( l, m);
216+
mergeSort( m + 1, r);
217+
218+
merger( l, m, r);
219+
}
220+
}
221+
int main()
222+
{ int a;
223+
cout<<"enter the number of elements you want to enter ";
224+
cin>>a;
225+
cout<<"\n enter array";
226+
input(a);
227+
char y='y';
228+
int x;
229+
int f,e;
230+
231+
do
232+
{ //system("cls");
233+
cout<<"enter choice:: ";
234+
235+
cout<<"\n 1 for linear search ";
236+
cout<<"\n 2 for binary search ";
237+
cout<<"\n 3 for Bubble sort ";
238+
cout<<"\n 4 for Selection sort ";
239+
cout<<"\n 5 for insertion sort";
240+
cout<<"\n 6 for quick sort ";
241+
cout<<"\n 7 for merge sort"<<endl;
242+
cin>>x;
243+
cout<<endl;
244+
switch(x)
245+
{
246+
case 1: linearSearch(a);
247+
break;
248+
case 2: cout<<"Enter the number to be searched ";
249+
cin>>e;
250+
f=binarySearch(0,a-1,e);
251+
if(f!=-1)
252+
cout<<"Element found at "<<f+1<<endl;
253+
else
254+
cout<<"Elemnet not found"<<endl;
255+
break;
256+
case 3: bubbleSort(a);
257+
break;
258+
case 4: selectionSort(a);
259+
break;
260+
case 5: insertionSort(a);
261+
printArray(a);
262+
break;
263+
case 6: quickSort(0,a-1);
264+
printArray(a);
265+
break;
266+
case 7: mergeSort(0,a-1);
267+
printArray(a);
268+
break;
269+
default:
270+
cout<<"enter valid choice";
271+
break;
272+
}
273+
cout<<"enter y to do continue ";
274+
cin>>y;
275+
}while(y=='y');
276+
return 0;
277+
}

0 commit comments

Comments
 (0)