1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* www.troubleshootyourself.com C program to sort an Array in Descending order using Bubble Sort */ #include <stdio.h> #define MAX 100 int main() { int arr[MAX], maxLimit; int i,j,temp; printf("Enter total number of elements to sort: "); scanf("%d",&maxLimit); /* Read elements to sort */ printf("Enter array elements: \n"); for(i=; i<maxLimit; i++) { printf("Enter element %3d: ",i+1); scanf("%d",&arr[i]); } /* Bubble sort logic: Sort elements in Descending Order */ for(i=; i<(maxLimit-1); i++) { for(j=; j<(maxLimit-i-1); j++) { if(arr[j]<arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf("Array elements in Descending Order:\n"); for(i=; i<maxLimit; i++) { printf("%d ",arr[i]); } printf("\n"); return ; } |
Check and run the program here:
You can run the above program on codeboard editor and see the results. You are allowed to modify code and run. In the editor there is a menu at the top-right corner. Here you can see two buttons such as ‘Compile’ and ‘Run’.