Friday, February 8, 2019

Bubble Sort using "C-Language" in Ubuntu

Sorting : Arranging the elements(numbers) either in ascending or descending order .
Bubble Sort:  is a simple sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

File Name : bobsrt.c
//12.Write a C program to sort a given list of integers in ascending order(Bubble Sort)
#include<stdio.h>
void main()
{
 int n,c,d,swap,a[10];
 printf("Input number of integers to sort:\n");
 scanf("%d",&n);
 printf("Enter %d integers:\n",n);
 for(c=0;c<n;c++)
    scanf("%d",&a[c]);
 for(c=0;c<(n-1);c++)
 {
  for(d=0;d<n-c-1;d++)
     {
      if(a[d]>a[d+1]) /* For descending order use < */
        {
          swap = a[d];
          a[d] = a[d+1];
          a[d+1] = swap;
        }
      }
 }
    printf("Sorted list of numbers:\n");
    for(c=0;c<n;c++)
        printf("%d\n",a[c]);
}


Output:
Input number of integers to sort:
4
Enter 4 integers:
23
5
225
1
Sorted list of numbers:
1
5
23
225

No comments:

Post a Comment

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog