Write a Program to arrange array elements in Ascending/descending order using bubble sort method.
#include<stdio.h> #include<limits.h> void Print_Array_Elements(int*,int); void Sort_Ascending(int*,int); void Sort_Descending(int*,int); int main() { int IntArray[10],TotalArrayElements; int Index; TotalArrayElements = sizeof(IntArray)/sizeof(IntArray[0]); printf("Enter %d Elements\n",TotalArrayElements); for(Index = 0;Index < TotalArrayElements;Index++) { scanf("%d",&IntArray[Index]); } printf("\nValues are\n"); Print_Array_Elements(IntArray,TotalArrayElements); //Sort_Ascending(IntArray,TotalArrayElements); Sort_Descending(IntArray,TotalArrayElements); printf("\nSorted Values are\n"); Print_Array_Elements(IntArray,TotalArrayElements); return 0; } void Sort_Descending(int*p,int n) { int i,j,k; for(i = 0;i < n - 1;i++) { for(j = 0;j < n - i -1;j++) { if(p[j] < p[j+1]) { k = p[j]; p[j] = p[j+1]; p[j+1] = k; } } } } void Sort_Ascending(int*p,int n) { int i,j,k; for(i = 0;i < n - 1;i++) { for(j = 0;j < n - i - 1;j++) { if(p[j] > p[j+1]) { k = p[j]; p[j] = p[j+1]; p[j+1] = k; } } } } void Print_Array_Elements(int*p,int n) { int i; for(i = 0;i < n;i++) printf("%d ",p[i]); printf("\n"); }
Output
Enter 10 Elements 100 80 60 40 20 10 30 50 70 90 Values are 100 80 60 40 20 10 30 50 70 90 Sorted Values are 100 90 80 70 60 50 40 30 20 10