# SORTING ALGORITHM AnnyT F G Bubble sort Pseudocode

SORTING ALGORITHM Anny@T. F. G

Bubble sort

Pseudocode implementation The algorithm can be expressed as: procedure bubble. Sort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: if A[i-1] > A[i] then swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure

pseudocode procedure bubble. Sort( A : list of sortable items ) n = length(A) repeat newn = 0 for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) newn = i end if end for n = newn until n = 0 end procedure

Bubble sort Worst case performance O(n 2) Best case performance O(n) Average case performance O(n 2) Worst case space complexity O(1) auxiliary

Selection sort The algorithm works as follows: 1. 2. 3. Find the minimum value in the list Swap it with the value in the first position Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)

Selection Sort - Analysis Worst case performance О(n 2) Best case performance О(n 2) Average case performance О(n 2)

64 25 12 22 11

Insertion sort Sorting is typically done in-place. The resulting array after k iterations has the property where the first k + 1 entries are sorted. In each iteration the first remaining entry of the input is removed, inserted into the result at the correct position, thus extending the result:

Insertion sort - Analysis Worst case performance Best case performance Average case performance Worst case space complexity О(n 2) O(n) О(n 2) О(n) total, O(1) auxiliary

- Slides: 10