2003-11-23 11:11 PM
2011-05-17 01:16 AM
I'm at the beginning with st7 programmation and i'm looking for a program that implemented the bubblesort algorithm.
Thank you that help me2011-05-17 01:16 AM
Here is the C-code
void swapElements(int a[], int maxPos, int last); void bubbleSortPhase(int a[], int last) { // Precondition: a is an array indexed from a[0] to a[last] // Move the largest element between a[0] and a[last] into a[last], // by swapping out of order pairs int pos; for (pos = 0; pos < last - 1; pos++) if (a[pos] > a[pos+1]) { swapElements(a, pos, pos+1); } // Postconditions: a[0] ... a[last] contain the same elements, // possibly reordered; a[last] >= a[0] ... a[last-1] } void bubbleSort(int a[], int n) { // Precondition: a is an array indexed from a[0] to a[n-1] int i; for (i = n - 1; i > 0; i--) bubbleSortPhase(a, i); // Postcondition: a is sorted } Reference: www.amberman.org/evolve/Transparencies/ch5.ppt Good luck