0% found this document useful (0 votes)
13 views

Linear and Binary Search - Bubble Sort - Programs

Linear search

Uploaded by

rinatambade860
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Linear and Binary Search - Bubble Sort - Programs

Linear search

Uploaded by

rinatambade860
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8

#include <stdio . h>


10
11 int main()
12- {
13 17 here key (search key)
14 int Array [100] , n,i,key;
15
16 printf("\nEnter the size of array:");
17 scanf("%d",&n);
18
19 printf("\nEnter the array Elements : ");
20 for (i=0 ,;i<n;i++)
21 {
22 printf("\nArray [%d]: ",i);
23 scanf("%d" ,&Array [i]) ;
24
25
26 1 here print array elements if you want
27 printf("\nElements are: "):
28 for (i=0;i<n;i++)
29
30 printf(" %d ",Array [i]) ;
31
32
33 printf("\nEnter the Number you want Search :");
34 scanf("%d", &key);
35
36 1/ Logic here
37 for (i=0,;i<n;i++)
38
39 if (Array [i]==key)
40
41 print f("Elemnet %d found in index %d",key, i);
42 return 0;
43
44
45
46 printf("Elemnet %d not found", key);
47
48 return 0;
49
50
51

Linear Search Code


9 #include <stdio . h>
10
11 int main()
12-{
13 int array[1 00] , n,i,j, temp, k;
14
15 printf("Enter The size of array :");
16 Scanf("%d", &n);
17
18 printf("\nEnter Array Elements :");
19 for (i=0;i<n; i++)
20
21 printi("array [%d] :",i);
22 Scanf("%d" ,&array [i] ) ;
23 }
24
25 1/ print array before Sorting. . .
26 printf("\nBefore Sorting : |");
27 for (i-0;i<n;it+)
28 {
29 printf("%d | ", array [i] );
30
31
32
33 /// main logic bubble sort pass is n-1
34
35 for (i-0;i<n-1;it+) // pass
36
37 for (j=0;j<n-i-1;j++) // sub pass
38 {
39 if(array [j] >array [j+1] )
40
41 temp = array[jl;
42 array [j] =array [j+1];
43 array [j +1]= temp;
44 }// if
45
46 } //subpass
47
48
49 } //pass forloop
50
51
52 // After Sort
53 printf("\nAfter Sorting : |");
54 for (i=0;i<n;i++)
55 {
56 printf("%d | " ,array[i]);
57
58
59
60
61
Bubble Sort Code
62 return 0;
63 }
64
2
3 Binary search program
4
5 ****** ******************** *************

6
7 #include <stdio . h>

int main()
10- {
11 int array[1 00],n,key, low, high, mid, i;
12
13 printf("\nEnter the size of array( Sorted):");
14 Scanf( "%d" , &n);
15
16 printf("\nEnter the Array Elements : ");
17 for (i=0;i<n;i++)
18 {
19 scanf("%d", &array [i] );
20 }
21
22 1/print array Elemen t
23 printf("\n The Array Elements are : ");
24 for (i=0;i<n;it+)
25 - {
26 printf(" %d " ,array[i]);
27
28
29 printf("\nEnter The Number you want search:");
30 Scanf(" %d",&key);
31
32 1/ main logic
33 low =0; //first index
34 high-n-1; // las t index
35
36 while(1ow<-high)
37 {
38 mid=(low+high) /2;
39
40 if (array [mid] ==key)
41 - {
42 printí("\nNumber %d found at location %d",key, mid);
43 return 0;
44
45
46 if(key>array [mid] )
47 - {
48 low = mid+1;
49
50 else{
51 high=mid-1;
52
53
54 } //while end
55
56
57 printf("Number %d Not Found", key);
58
59 return 0;
60 }
61 Binary Search Code
62

You might also like