Docsity

Sorting Algorithms: An Analysis of Bubblesort and Selection Sort, Study notes of Computer Science

Partial preview of the text

Download Sorting Algorithms: An Analysis of Bubblesort and Selection Sort and more Study notes Computer Science in PDF only on Docsity!

Today

  • Sorting (cont)

Sorting Algorithms – Attempt 1

A “naive” sorting algorithm

  • Try all orderings of a list
  • Return the one that is sorted

Q: Is this a “good” sorting algorithm?

  • ... depends on metrics used to define “good”

Why is this algorithm bad (i.e., inefficient)?

  • We usually consider “worst cases”
  • Lets say the list is of length n
  • It is easy to check if a list is sorted (n-1 comparisons)
  • But finding all orderings (permutations) is expensive, namely:
    • n × (n − 1) × (n − 2) × · · · × (n − (n − 1))
    • which is n!
    • that is, you have to create n! lists of size n
    • computer scientists call this worst-case behavior “order n!” (i.e., O(n!))

Sorting Algorithms – Bubblesort

A Python Implementation of Bubblesort

def swap(a_list, i, j): tmp = a_list[i] a_list[i] = a_list[j] a_list[j] = tmp

def bubble_sort(a_list): n = len(a_list) for i in range(1, n): # passes 1 to n - 1 for j in range(n - i): # comparisons of pass if a_list[j] > a_list[j + 1]: swap(a_list, j, j + 1)

Note we can analyze the procedure to determine the cost

  • Bubblesort’s number of comparisons depends fully on the size of the list
  • The inner loop iterates: (n − 1) + (n − 2) + · · · + (n − (n − 1)) times
  • This is the sum^1 : n∑− 1 i=

i = n(n − 1) 2

n^2 − n 2

  • In the worst case, there are 3 times this many swaps
  • But together, this is still much better than n!
    • computer scientists say bubble sort is worst-case “order n^2 ” (i.e., O(n^2 ))

(^1) Note that ∑ni=1 i = n(n 2 +1)

Sorting Algorithms – Attempt 3

An even better (but still not good) sorting algorithm: Selection sort

The basic idea:

  • On each pass, find the largest item
  • Swap it with the last element of the list
  • Repeat for n − 1 passes (shrinking the list by 1)

A simple example

Initial list: 29 10 14 13 ( n =4)

Pass 1 Pass 2

13 10 14 29

13 10 14 29 13 10 14 29

Pass 3 13 10 14 29 10 13 14 29

  • How many comparisons, and how many moves?

A Python Implementation of Selection Sort def selection_sort(a_list): n = len(a_list) for i in range(0, n - 1): index = 0 for j in range(1, n - i): if a_list[j] >= a_list[index]: index = j swap(a_list, index, n - i - 1)

  • Why is this better than bubblesort?
  • We only swap n − 1 times! ... (i.e., O(n) swaps versus O(n^2 ) swaps)