Algorithm

With Bubblesort an array can be sorted.

Input: Array a

Output: sorted Array

Variables: n: size of array, i: current position, h

 

Bubblesort(a)

  1. for(n=a.size; n>1; n=n-1)
  2. for(i=0;i<n-1;i=i+1)
  3. If(a[i] > a[i+1])
  4. h=a[i+1]
  5. a[i+1] = a[i]
  6. a[i]=h

 

With Bubblesort an array a can be sorted. It compares two elements e with each other. If they need to be switched this is done. With this algorithm one element is compared with each other element until the comparison is wrong. This is done for all elements.

 

  1. Initialize n with the size of the array and I with 0
  2. for each position n in a and position i in n-1:
  3. Compare value of a[i] with value of a[i+a]
  4. Switch the two elements
    1. Set variable h to value of a[i+1]
    2. Set value of a[i+a] to value of a[i]
    3. Set value of a[i] to h

Leave a Reply

Your email address will not be published. Required fields are marked *