Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
25. Write a program to sort the elements of the array in ascending order using the Bubble Sort technique.
/*
File: Prgrm25.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to sort the elements of the array using Bubble Sort.
*/
int main (void)
{
int array [10];
int i, j, temp, swap;
//Input elements of array
for (i = 0; i < 10; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &array [i]);
}
//Sorting the elements
for (i = 0; i < 9; i++)
{
swap = 0;
for (j = 0; j < 9 - i; j++)
{
if (array [j] > array [j + 1])
{
temp = array [j];
array [j] = array [j + 1];
array [j + 1] = temp;
swap = 1;
}
}
if (swap == 0)
break;
}
//Output the result
printf ("\nSorted Array: ");
for (i = 0; i < 10; i++)
{
printf ("%d ", array [i]);
}
return 0;
for (i = 0; i < 9; i++)
{
swap = 0;
for (j = 0; j < 9 - i; j++)
{
if (array [j] > array [j + 1])
{
temp = array [j];
array [j] = array [j + 1];
array [j + 1] = temp;
swap = 1;
}
}
if (swap == 0)
break;
}
//Output the result
printf ("\nSorted Array: ");
for (i = 0; i < 10; i++)
{
printf ("%d ", array [i]);
}
return 0;
};
Output
Input 1 element: 27
Input 2 element: 46
Input 3 element: 92
Input 4 element: 42
Input 5 element: 80
Input 6 element: 58
Input 7 element: 13
Input 8 element: 22
Input 9 element: 73
Input 10 element: 38
Input 2 element: 46
Input 3 element: 92
Input 4 element: 42
Input 5 element: 80
Input 6 element: 58
Input 7 element: 13
Input 8 element: 22
Input 9 element: 73
Input 10 element: 38
Sorted Array: 13 22 27 38 42 46 58 73 80 92
No comments:
Post a Comment
Please do not post spam links.