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

Roots of Quadratic Equation: JavaScript

Quadratic Equations are of the form ax2 + bx + c = 0. To find roots(root1 and root2) of such an equation, we need to use the formula

quadratic-equation


Video Explaining The JavaScript Code To Find Roots of a Quadratic Equation:


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

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


To calculate the roots of a quadratic equation using a computer program, we need to break down the formula and calculate smaller parts of it and then combine to get the actual solution.

So lets calculate square root of b2 – 4 * a * c and store it in variable root_part. Also store 2 * a in variable denom. Now calculate ( – b + root_part ) / denom and store it in root1 and ( – b – root_part ) / denom in root2.
Output the values of root1 and root2 to the browser using document.write statement.

Javascript Coding Explained In Above Video:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <title>Quadratic Equation</title>
 
 
<script type="text/javascript">
<!--
var a = prompt("Enter value of a","1");
var b = prompt("Enter value of b","4");
var c = prompt("Enter value of c","4");
 
var root_part = Math.sqrt(b * b - 4 * a * c);
var denom = 2 * a;
 
var root1 = ( -b + root_part ) / denom;
var root2 = ( -b - root_part ) / denom;
 
document.write("1st root: "+root1+"<br />");
document.write("2nd root: "+root2+"<br />");
 
// -->
</script>

Usually people will make mistake in this line

var root_part = Math.sqrt(b * b - 4 * a * c);

Be sure that, you write capital letter M in Math.sqrt.

Work Space:
Quadratic Equation: ax2 + bx + c = 0
Let,
a = 1
b = 4
c = 4
i.e., 1x2 + 4x + 4 =0
=> 1x2 + 2x + 2x + 4 = 0
=> x ( x + 2 ) + 2 ( x + 2 ) = 0
=> ( x + 2 ) + ( x + 2 ) = 0
=> x + 2 = 0 AND x + 2 = 0
=> x = -2 AND x = -2