cancel
Showing results for 
Search instead for 
Did you mean: 

ST7 bubblesort

baggio10
Associate II
Posted on November 24, 2003 at 08:11

ST7 bubblesort

2 REPLIES 2
baggio10
Associate II
Posted on May 17, 2011 at 10:16

I'm at the beginning with st7 programmation and i'm looking for a program that implemented the bubblesort algorithm.

Thank you that help me

alain239955
Associate II
Posted on May 17, 2011 at 10:16

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