#Sorting#Computer-Science#CS251 Bubbles the largest values to the right (top) of the array

Pseudocode

BubbleSort(A:array of size n)
	repeat <- true
	while (repeat)
		repeat <- false
		for (i = 0, i < n-1, 1)
			if (A[i] > A[i+1])
				swap(A, i, i+1)
				repeat <- true
			endif
		endfor
	endwhile
	return A
end BubbleSort

Details

  • Is stable
  • Is in place, has a space complexity
  • Time Complexity
    • If the array is sorted
      • # Compares =
      • # Swaps =
    • If the array is sorted in opposite order?
      • # Compares =
      • # Swaps =

Best Case

Worst Case