Write a C program that implements the Bubble sort method
to sort a given list of names in ascending order.
Algorithm:
Step 1: Start Step 2: Read n, a[i] values as integers Step 3: for i: 1 to n do increment i by 1 begin for j: 0 to n - i - 1 increment j by 1 begin if(a[j] > a[j + 1]) begin t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; end end end Step 4: for i: 0 to n Print a[i] Step 5: Stop
Flowchart:
Program:
#include<stdio.h> #include<conio.h> void main() { int n, a[20], temp, i, j; clrscr(); printf("Enter the size of the array\n"); scanf("%d", &n); printf("Enter the array elements\n"); for(i = 0; i < n; i++) { scanf("%d", &a[i]); } for(i = 0; i < n - 1; i++) { for(j = 0; j < n - 1; j++) { if(a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } printf("The sorted array is\n"); for(i = 0; i < n; i++) printf("%d\n", a[i]); getch(); }
Input & Output:
Enter the size of the array: 5
Enter the array elements: 50 40 30 20 10
The sorted array is: 10 20 30 40 50
-
UpdatedOct 21, 2014
-
Views20,909
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.