|
|
Sorting an array in ascending order - exchange sort algorithm. |
|
#include <stdio.h> void bubble_sort(int *, const int); int main(void) { int arr[] = { 55, 23, 15, 67, 15 }, i; printf( "\nOriginal array : " ); for( i = 0; i < 5; i++ ) printf( "\n%d", arr[i] ); bubble_sort(arr, 5); printf( "\nSorted array : " ); for( i = 0; i < 5; i++ ) printf( "\n%d", arr[i] ); return 0; } void bubble_sort(int *p, const int sz) { int i, j, temp; /* Sorting the array */ for( i = 1; i < sz; i++ ) /* (n - 1) iterations */ { for( j = 0; j < (sz - 1); j++ ) { if( p[j] > p[j + 1] ) { /* array elements are out of order hence swap them */ temp = p[j]; p[j] = p[j + 1]; p[j + 1] = temp; } } } }
|
|
|
|
|
|
|
|