Program to sort array using Bubble Sort solution using c language--programming_info.problem2

Program to sort array using Bubble Sort.

This above example is just a reference picture. don't
think it as an input of an array.
you can give your own input.

#include<stdio.h>
#include<conio.h>

void main()
{
    int total, element[100], i, j, temp;
    clrscr();

    printf("Enter total number of elements :");
    scanf("%d", &total);

    for(i=0; i<total; i++)
    {
        printf("Enter element no%d :", i+1);
        scanf("%d", &element[i]);
    }
 
    for (i=0; i<(total - 1); i++)
    {
        for (j = 0; j < total  - 1; j++)
        {
            if (element[j] > element[j + 1])
            {
                temp = element[j];
                element[j] = element[j + 1];
                element[j + 1] = temp;
            }
        }
    }

    printf("\nSorted list in ascending order :");

    for (i = 0; i < total; i++)
    {
        printf("\n%d", element[i]);
    }
    getch();
}
//To sort in descending order, use < (less than) in if condition.
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support