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

Find the Factorial of a Number: C

This video tutorial illustrates finding factorial of a given number using C programming.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Ex:
if user enters 5, then the factorial is 1 * 2 * 3 * 4 * 5 i.e., factorial = 120
This logic must be handled with a c program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include < stdio.h >
#include < conio.h >
 
void main()
{
int num, i, fact = 1;
clrscr();
 
printf("Enter a number\n");
scanf("%d", &num);
 
for( i = 1; i < = num; i++ )
 fact = fact * i;                // fact *= i;
 
printf("\nFactorial of %d is %d", num, fact);
 
getch();
 
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

Note: Factorial of 0 is 1. i.e., 0! = 1

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in c


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

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



Output:
Enter a number
5
Factorial of 5 is 120

Also Watch/Read: Find the Factorial of a Number: C++

Find Sum of Digits In A Given Number: C

Video Tutorial to illustrate, C Program to find sum of digits in the given/user entered integer number.

In this program we assign variable sum = 0 to avoid garbage values in sum before the calculation, which would result in wrong output.
We store the user entered value in num.

1
2
3
4
5
6
 while( num )
  {
    rem = num % 10;
    sum = sum + rem;
    num = num / 10;
  }

Here the loop executes until the value of num is zero.

If user enters 123, we apply the modulus to get the individual values.
Ex:
123 % 10 = 3
12 % 10 = 2
1 % 10 = 1

We get 123, 12 and 1 by dividing the original value by 10.
Ex:
123 user entered value.
123 / 10 = 12
12 / 10 = 1

Now the sum.

sum = sum + rem;

3 = 0 + 3
5 = 3 + 2
6 = 5 + 1

So the sum of digits in the given integer number is 6.

Video Tutorial: Find Sum of Digits In A Given Number: C


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

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



Full Free Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include < stdio.h >
#include < conio.h >
 
void main()
{
int num, rem, sum = 0;
clrscr();
 
printf("Enter an integer number\n");
scanf("%d", &num);
 
while(num)
{
  rem = num % 10;
  sum = sum + rem;
  num = num / 10;
}
 
printf("\nThe sum of digits is %d", sum);
getch();
}

Output:
Enter an integer number
123
The sum of digits is 6

Find Given Number Is Prime or Not: C

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

A natural number greater than 1 that is not a prime number is called a composite number. For example, 7 is prime, as only 1 and 7 divide it, whereas 10 is composite, since it has the divisors 2 and 5 in addition to 1 and 10.

In this video tutorial we show you 3 methods of finding whether the user entered number is a prime number or not.

Since every number is divisible by 1, we start the division(in for loop) from 2.

Note: A number can not be divided(to get a whole number) by another number which is greater than itself.

First Method:
Since every number is divisible by 1, we start the division(in for loop) from 2 till one number less than the user entered value.
Example: If user entered 10. For loop starts from 2 to 9.
for(i=2; i<10; i++) or for(i=2; i< =9; i++)

But the draw back: If user enters 500, the loop must get executed from 2 till 499. i.e., 497 times.

Second Method:
Here we reduce the number of iterations in for loop.
i.e., we divide the user entered value by 2 and divide the user entered value from 2 till num / 2;
Example: If user entered 500, we start the division from 2 till 500/2 i.e., till 250 times.

Third Method:
Here we still reduce the number of iterations in the for loop.
i.e., we take the square root of the user entered number and divide the user entered number from 2 till square root of number.
Example: If user entered 500, we start the division from 2 till sqrt(500) i.e., till 22 times.

So this third method is most optimum, as it involves least number of looping.

Video Tutorial: Find Given Number Is Prime or Not: C


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

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


Full Free Source Code:

#include < stdio .h >
#include < conio .h >
#include < math .h >
 
void main()
{
int num, i, fact, inum;
clrscr();
 
printf("Enter a number\n");
scanf("%d", &num);
 
inum = sqrt(num);
 
for(i=2; i <= inum ; i++)
 if( num % i == 0 )
 {
   fact = 0;
   break;
 }
 else
   fact = 1;
 
if(fact)
 printf("%d is Prime\n", num);
else
 printf("%d is NOT prime\n", num);
 
getch();
}

Table of all prime numbers up to 1,000: