# C program to Sort Array Elements in Ascending or Descending Order-Bubble Sort

An array sometimes need to be sorted in our preferred manner so as to get get better accessible data or just for convenience. Here you will learn how to sort an array in two ways — Ascending Order and Descending Order.

Recommended Read : C program to perform Quick Sort Algorithm

Let’s first C the code for sorting the array element in ascending or descending order

``/* Ascending Order */ #include<stdio.h> int main() { int a, n, i, j, temp;  printf("Enter how many numbers to be sorted"); scanf("\%d", &n);  printf("Enter Numbers to be sorted in ascending order"); for(i=0; i<n; i++) //input loop { scanf("\%d", &a[i]); }  for(i=0; i<n; i++) { for(j=0; j<n-i-1; j++) { if(a[j] > a[j+1]) { temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } }  printf("Sorted Numbers are:"); for( i=0; i<n; i++ ) //printing sorted numbers { printf("\%d", a[i]); } return 0; }``

Output Variables:( are in single quotes ‘ ’ )

‘n’ stores how many numbers need to be sorted.

‘i’, ‘j’ are used as counter variable here, to keep track of how many elements we have sorted

‘temp’ is used for swapping the numbers in the list, as a temporary storage.

‘a’ an array of size 100 is declared to hold the numbers inserted by the user which is then sorted afterwards.

``for(i=0; i<n; i++) //input loop { scanf("\%d", &a[i]); } ``

The above loop takes the numbers to be sorted from the user and stores it in the array ‘a[i]’ at index ‘i’ which keeps on incrementing after insertion of each number until i becomes smaller than ‘n’.

``for(i=0; i<n; i++) { for(j=0; j<n-i-1; j++) { if(a[j] > a[j+1])//checking if the list is sorted or not { temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } ``

This above loop is known as a Bubble Sort Algorithm.

Here swapping is done after checking whether the first element is greater than second element, if yes then swap them because we need the list to be sorted in ascending order, which is smallest number first.

Bubble sort takes Ο(n2) time so we’re keeping it short and precise.

Now, lets see how to sort numbers in Descending Order

There are just two modifications in the above program to sort the list in descending order. Try doing it yourself, if not then look ahead.

Hint:

Line 9 in printf();

Line 19 in if() statement

``/*Program for sorting numbers in Descending Order*/ #include<stdio.h> int main() { int a, n, i, j, temp;  printf("Enter how many numbers to be sorted"); scanf("\%d", &n);  printf("Enter Numbers to be sorted in descending order"); for(i=0; i<n; i++) //input loop { scanf("\%d", &a[i]); }  for(i=0; i<n; i++) { for(j=0; j<n-i-1; j++) { if(a[j] < a[j+1]) { temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } }  printf("Sorted Numbers are:"); for( i=0; i<n; i++ ) //printing sorted numbers { printf("\%d", a[i]); } return 0; } ``

Output: Modified Lines of code:

``printf("Enter Numbers to be sorted in descending order"); if(a[j] < a[j+1])``

Everything else remains the same!

Also, don’t forget to subscribe to our Newsletter.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Explaining Register variables in C with examples
17 August 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями: