-
Notifications
You must be signed in to change notification settings - Fork 40
/
ABUBSORT.CPP
77 lines (77 loc) · 1.38 KB
/
ABUBSORT.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
//WRITE A PROGRAM IN C++ TO SORT AN ARRAY USING BUBBLE SORT TECHNIQUE.
# include <iostream.h>
# include <conio.h>
void bubsort(int [],int);
int bsearch(int arr1[],int x, int value)
{
int i, beg, end, mid;
beg = 0;
end = x - 1;
mid = 0;
while(beg <= end)
{
mid = (beg + end) / 2;
if(value == arr1[mid])
{
return mid +1;
}
else if(value > arr1[mid])
{
beg = mid +1;
}
else
{
end = mid -1;
}
}
return -1;
}
void main()
{
clrscr();
int arr[30], i , s ,x;
cout<<"\t\t SORTING AN ARRAY USING BUBBLE SORT TECHNIQUE "<<endl<<endl;
cout<< "Enter the size of the array required ";
cin >> s;
cout<<endl;
for(i=0; i < s; i++)
{
cout<< "Enter the "<<i+1 <<" element :";
cin >> arr[i];
}
bubsort(arr, s );
cout << "\n\n The sorted list is as follows -";
for(i=0; i < s; i++)
{
cout<< "\n "<<i +1 <<" element :" << arr[i];
}
cout<<"\n Enter the value to be searched ";
cin>>x;
int loc = 0;
loc = bsearch(arr, s , x);
if(loc == -1)
{
cout << "\n The element is not found";
}
else
{
cout<<"\n The element is found at "<< loc << " location";
}
getch();
}
void bubsort(int arr1[],int x)
{
int i, j , temp;
for(i = 0; i < x; i++,x--)
{
for(j = 0; j < x-1 ; j++)
{
if(arr1[j] > arr1[j+1])
{
temp = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = temp;
}
}
}
}