forked from ash638/code-for-hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction_binary_&_selection.c
More file actions
77 lines (72 loc) · 1.52 KB
/
Function_binary_&_selection.c
File metadata and controls
77 lines (72 loc) · 1.52 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
#include<stdio.h>
#include<conio.h>
void selection(int array[], int n)
{
int i, j, position, swap;
for(i = 0; i < (n - 1); i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(array[position] > array[j])
position=j;
}
if(position != i)
{
swap=array[i];
array[i]=array[position];
array[position]=swap;
}
}
printf("Sorted value: ");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
}
void binary(int array[], int n, int search)
{
int first, mid, last;
first = 0;
last = n-1;
while(first<=last)
{
mid=(first+last)/2;
if(array[mid]==search)
{
printf("We found %d in location %d\n", search, mid+1);
break;
}
else if(array[mid]<search)
{
first=mid+1;
}
else{
last = mid-1;
}
}
if(first>last)
printf("Not found!!! %d is not present.\n", search);
}
int main()
{
int array[10];
int i, n, search;
printf("Enter the value of number: ");
scanf("%d", &n);
printf("Enter value:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
//selection sorting begins
//Binary searching begins
selection(array, n);
printf("\nEnter the element to be searched: ");
scanf("%d", &search);
printf("\n searching....\n");
sleep(1);
binary(array,n,search);
getch();
return 0;
}