Write a C program for Bubble Sort Algorithm for the descending order in
CodeBlocks using a user defined function.
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int A[], int n) {
int temp,i,k;
for(k = 0; k< n-1; k++) {
for(i = 0; i < n-k-1; i++) {
if(A[ i ] < A[ i+1] ) {
temp = A[ i ];
A[ i ] = A[ i+1 ];
A[ i + 1] = temp;
}
}
}
}
int main(){
int arrayNumbers[]={5,4,78,6,1,2,8,6,44,56};
int i;
int n=10;
printf("Current numbers:\n");
for(i = 0; i < n; i++) {
printf("%d ",arrayNumbers[i]);
}
printf("\n\nSorted numbers:\n");
bubble_sort(arrayNumbers,n);
for(i = 0; i < n; i++) {
printf("%d ",arrayNumbers[i]);
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment