Q:

bubble sort Ascending And Descending Order using c programming

belongs to collection: Functions in C Programs

0

In general terms, Ascending means smallest to largest, 0 to 9, and/or A to Z and Descending means largest to smallest, 9 to 0, and/or Z to A. Ascending order means the smallest or first or earliest in the order will appear at the top of the list: For numbers or amounts, the sort is smallest to largest

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include

void bubble_sort(long [], long);

int main()
{
  long array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%ld", &n);

  printf("Enter %ld integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%ld", &array[c]);

  bubble_sort(array, n);

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%ld\n", array[c]);
   
      printf("\nSorted list in descending order:\n");

  for ( c = n-1 ; c >= 0; c-- )
     printf("%d\n", array[c]);

  return 0;
}

void bubble_sort(long list[], long n)
{
  long c, d, t;

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (list[d] > list[d+1])
      {
        t         = list[d];
        list[d]   = list[d+1];
        list[d+1] = t;
      }
    }
  }
}

 

Output:

Enter Number of Elements

6

Enter 6 Integers 

12

32

65

45

98

78

Sorted list in  ascending order:

12

32

45

65

78

98

Sorted list in descending oreder:

98

78

65

45

32

12

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program For Convert Binary Number to Decimal or ... >>