Linear Search: C

In this video tutorial we shall see how we can search for a number in an array in linear fashion.

First ask the user to enter the number to be searched. Store it inside a variable called key.
Now start comparing key value with a[i] using a for loop. If the key is found in the array, then assign 1 to flag orelse flag will be 0.

Depending upon the value of flag, print the message.

If flag is 1, search is successful.
If flag is 0, search failed. in the sense, the number is not present in the given array.

Video Tutorial: Linear Search: C


[youtube https://www.youtube.com/watch?v=AqjVd6FVFbE]

YouTube Link: https://www.youtube.com/watch?v=AqjVd6FVFbE [Watch the Video In Full Screen.]



Full Source Code

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
#include < stdio.h >
#include < conio.h >
 
void main()
{
int a[20], key, N, flag = 0, i;
clrscr();
 
printf("Enter array limit\n");
scanf("%d", &N);
 
printf("Enter %d elements\n", N);
for(i=0; i < N; i++)
 scanf("%d",&a[i]);
 
printf("Enter the number to be searched\n");
scanf("%d", &key);
 
for( i=0; i < N; i++)
 if( a[i] == key )
 {
flag = 1;
break;
 }
 
if(flag)
printf("\nSearch Successful\n");
else
printf("\nSearch Failed\n");
getch();
}

Output
Enter array limit
5
Enter 5 elements
77
0
13
9
55
Enter the number to be searched
9
Search Successful

Find First and Second Biggest In An Array, Without Sorting It: C

We assume that a[0] has first biggest and a[1] has the second biggest value.

Now check if this assumption is correct, if not, swap the values.

 
 if( sbig > fbig )
 {
    temp = sbig;
    sbig = fbig;
    fbig = temp;
 }

Now since we have already checked with a[0] and a[1], we start the comparison from a[2], till N.

for(i=2; i < n ; i++)
 if(a[i] > fbig)
 {
sbig = fbig;
fbig = a[i];
 }
 else if(a[i] > sbig)
sbig = a[i];

Now, if a[i] contains a number which is bigger than fbig, we transfer the value of a[i] to fbig and the value present in fbig to sbig;
If the value of a[i] is not bigger than fbig, then we check it with sbig. If a[i] is bigger than sbig, then we assign the value of a[i] to sbig.

This way, at the end of the loop fbig will have the first biggest and sbig will have the second biggest element/number in the array.

Video Tutorial: Find First and Second Biggest In An Array, Without Sorting It: C


[youtube https://www.youtube.com/watch?v=DzZvT87WuGQ]

YouTube Link: https://www.youtube.com/watch?v=DzZvT87WuGQ [Watch the Video In Full Screen.]



Full source code

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
#include < stdio.h >
#include < conio.h >
 
void main()
{
int a[20], N, i, fbig, sbig, temp;
clrscr();
 
printf("Enter array limit\n");
scanf("%d", &N);
 
printf("Enter %d array elements\n", N);
for(i=0; i < n ; i++)
 scanf("%d", &a[i]);
 
fbig = a[0];
sbig = a[1];
 
if( sbig > fbig )
{
   temp = sbig;
   sbig = fbig;
   fbig = temp;
}
 
for(i=2; i < n ; i++)
 if(a[i] > fbig)
 {
sbig = fbig;
fbig = a[i];
 }
 else if(a[i] > sbig)
sbig = a[i];
 
 printf("First Big is %d and Second big is %d", fbig, sbig);
 
 getch();
 
}

Output:
Enter array limit
6
Enter 6 array elements
99
108
777
723
786
999
First Big is 999 and Second big is 786

Find Biggest In An Array, Without Sorting It: C

First we have to assume that a[0] is biggest. Now store the value of a[0] inside a variable called big. Now compare value of big with other values in the array. If big is less than any other value, then store the bigger value inside variable big.

Video Tutorial: Biggest In An Array, Without Sorting It: C


[youtube https://www.youtube.com/watch?v=tKaImGrxgKo]

YouTube Link: https://www.youtube.com/watch?v=tKaImGrxgKo [Watch the Video In Full Screen.]



Note that, the previous value inside the variable big is discarded.

Full source code

#include < stdio.h >
#include < conio.h >

void main()
{
int big, a[20], N, pos, i;
clrscr();

printf("Enter the array size\n");
scanf("%d", &N);

printf("Enter %d elements\n", N);
for(i = 0; i < N; i++) 
           scanf("%d", &a[i]);

         big = a[0];
         pos = 0;

         for(i = 1;  i < N; i++)
          if(a[i] > big)
  {
    big = a[i];
    pos = i+1;
    }

 printf("Big is %d and its position is %d", big, pos);

 getch();
}

Output
Enter the array size
5
Enter 5 elements
2
0
100
108
55
Big is 108 and its position is 4

Bubble Sort: C

In this video tutorial we illustrate the bubble sort algorithm and also see how to write it in C programming, for 5 elements as well as to N elements.

In bubble sort, a[0] is compared with a[1]. a[1] with a[2], a[2] with a[3] .. so on
if a[0] is greater than a[1], the values are swapped. This logic continues till the end of the array.

At the end of each iteration, 1 element gets sorted.

If the array size is 5, then we will have 4 iterations. i.e., if size is N, then we will have N-1 iterations.



Bubble Sort: Full source code for 5 elements

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
#include < stdio.h >
#include < conio.h >
 
void main()
{
int a[5] = { 15, 0, -2, 8, 3 };
int i, j, temp;
clrscr();
 
printf("Array elements are..\n");
for( i=0; i&lt;5; i++)
 printf("\n%d", a[i]);
 
for(j=3; j>=0; j--)
 for(i=0; i< =j; i++)
  if( a[i] > a[i+1] )
  {
    temp = a[i];
    a[i] = a[i+1];
    a[i+1] = temp;
  }
 
printf("\nafter sorting..\n");
for(i=0; i&lt;5; i++)
 printf("\n%d", a[i]);
 
getch();
}

Output:
Array elements are..
15
0
-2
8
3

after sorting..
-2
0
3
8
15

Video Tutorial: Bubble Sort: C


[youtube https://www.youtube.com/watch?v=Ne9QNWuGJvw]

YouTube Link: https://www.youtube.com/watch?v=Ne9QNWuGJvw [Watch the Video In Full Screen.]



Bubble Sort: Full source code for N elements

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
#include < stdio.h >
#include < conio.h >
 
void main()
{
int a[20];
int i, j, temp, N;
clrscr();
 
printf("\nEnter array size\n");
scanf("%d", &N);
 
printf("Enter %d elements\n", N);
for(i=0; i<n ; i++)
 scanf("\n%d", &a[i]);
 
printf("Array elements are..\n");
for( i=0; i<N; i++)
 printf("\n%d", a[i]);
 
for(j=N-2; j>=0; j--)
 for(i=0; i< =j; i++)
  if( a[i] > a[i+1] )
  {
    temp = a[i];
    a[i] = a[i+1];
    a[i+1] = temp;
  }
 
printf("\nafter sorting..\n");
for(i=0; i</n><n ; i++)
 printf("\n%d", a[i]);
 
getch();
}

Output:
Enter array size
10
Enter 10 elements
20
19
18
17
16
15
14
13
12
11

Array elements are..
20
19
18
17
16
15
14
13
12
11

after sorting..
11
12
13
14
15
16
17
18
19
20

Selection Sort of N numbers: C

Selection sort is a sorting technique which is inefficient on large list of elements. Its simple and easy to understand.

In selection sort, first we select one element and compare it with the rest of the elements. If the compared element is smaller than the selected element, then those numbers are sorted. This approach is continued till the last element in the array index.

The number of iterations is 1 less than the array size.
i.e., if the user enters array size as 5, then there will be 4 iterations.

Below is an example:

Lets assume, user has entered the array size as 5.
and entered 13, 0, -2, 8, 3 as array elements.


First Iteration:

selection-sort-first-iteration



In first iteration, a[0] is selected. and is compared with a[1], a[2], a[3] and a[4]. and is swapped with the least value in the array.


Second Iteration:

selection-sort-second-iteration



In the second iteration, we leave off a[0] and select a[1] and compare it with the rest of the elements in the array i.e., a[2], a[3] and a[4]. and swap the least value with a[1]

Third Iteration:

selection-sort-third-iteration



In third iteration, we select a[2] and compare it with a[3], a[4] and swap the least value to a[2]


Forth Iteration:

selection-sort-forth-iteration



In forth iteration, we select a[3] and compare it with a[4] and swap the least value.

At the end of 4 iterations all array elements are sorted.

Video Tutorial: Selection Sort of N numbers: C


[youtube https://www.youtube.com/watch?v=NWLPmRdIPBA]

YouTube Link: https://www.youtube.com/watch?v=NWLPmRdIPBA [Watch the Video In Full Screen.]



Since the array index starts from 0, and we need not compare the last element in the array with any other elements, we iterate the outer loop from 0 to N-2.
We iterate the inner loop from i+1 till N-1.

Source Code: Selection Sort of N numbers: C

#include<stdio.h>
#include<conio.h>
 
void main()
{
int a[20], i, j, N, temp;
clrscr();
 
printf("Enter the size of the array\n");
scanf("%d", &N);
 
printf("Enter %d elements\n", N);
for(i=0; i<n ; i++)
 scanf("%d", &a[i]);
 
for( i=0; i<=N-2; i++ )
 for( j=i+1; j<=N-1; j++ )
  if(a[i] > a[j])
  {
     temp = a[i];
     a[i] = a[j];
     a[j] = temp;
  }
 
         printf("\nSorted Array:\n");
  for(i=0; i < n ; i++)
   printf("\n%d", a[i]);
 
   getch();
 
}

Output:
Enter the size of the array
5
Enter 5 elements
13
0
-2
8
3
Sorted Array:
-2
0
3
8
13