CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
-
Upload
meghan-ross -
Category
Documents
-
view
225 -
download
3
Transcript of CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
Roadmap
Algorithm Analysis Simple sorting algorithms
Bubble sort Selection sort Insertion sort
Advanced sorting algorithms
Warm-up questions Give the big Oh cost of the following code fragments:
a. int sum = 0; for (int i = 1; i < N; i *= 2)
for (int j = 0; j < N; j++) sum++;
b. int sum = 0; for (int i = 1; i < N; i *= 2)
for(int j = 0; j < i; j++) sum++;
Consult previous tattoo! “Remembered these, you have, hmm?”
∑𝑖=1
𝑛
𝑖=¿1+2+3+⋯+𝑛=𝑛(𝑛+1)
2¿
∑𝑖=0
𝑛
𝑥 𝑖=¿ 𝑥0+𝑥1+⋯+𝑥𝑛= 𝑥𝑛+1−1𝑥−1
¿
∑𝑖=0
log (𝑛)
2𝑖=¿1+2+4+⋯+2 log (𝑛)≈2𝑁 ¿
Triangular sum
Geometric sum
More tattoos (other arm?)
Harmonic sum
Stirling’s approximation
∑𝑖=1
𝑛1𝑖=¿1+ 1
2+ 1
3+⋯+ 1
𝑛≈ log (𝑛)¿
log (𝑛 ! )=log (1 )+ log (2 )+⋯+ log (𝑛)≈𝑛 log (𝑛)
Roadmap
Algorithm Analysis Simple sorting algorithms
Bubble sort Selection sort Insertion sort
Advanced sorting algorithms
Rules of the Game Goal
rearrange items such that their keys are in ascending (increasing) order
Algorithms Based on comparisons of and exchanges between
items Sorting cost model
count compares and exchanges Whether extra memory needed
in place (no extra memory) need extra memory to hold another copy of the
array to be sorted.
Java Comparable Interface
We can sort any data items as long as items implements Comparable interface v.compareTo(w) returns an integer that is negative,
zero, or positive when v<w, v=w, v>w
Example class that implements Comparable interface: Date.java
Bubble Sort and Selection Sort
Intuition: Find the biggest (smallest) number Find the second biggest (smallest) number … keep going
Bubble sort Repeatedly swap two adjacent numbers in each
pass ▪ Biggest number pushed to the end!
Selection sort Select the biggest (smallest) number in each pass
• After one pass, we find the biggest number.
• It’s like the biggest ‘bubble’ floats to the topof the surface, hence the name ‘bubble sort’.
• Examples: 3 5 2 7 1 6 4
Bubble Sort
• In the second pass, we repeat the sameprocess, but now we only have N-1 numbersto work on.
• The third pass is the same, with only N-2numbers.
• …• Repeat until all players are in order.
Bubble Sort
= O(N )
= O(N )
Analysis of Bubble Sort
• Number of comparisons?
• Number of swaps?best case:
worst cast:
average:
2N(N -1)
2
= O(N2)
2
N(N -1)
2
N(N -1)
4
O(1)
Selection Sort
1. Keep track of the index of the smallestnumber in each round.
2. Swap the smallest number towards thebeginning of the array.
3. Repeat the above two steps.
Selection Sort
Online demo http://www.sorting-algorithms.com/selection-sort
Gypsy dance demo http://www.youtube.com/watch?v=Ns4TPTC8wh
w
Insertion Sort
• Idea– Assume the left portion of the array is partially
sorted (however, unlike selection sort, theelements are not necessarily in their finalpositions)
– For each remaining element on the right portion,insert it to the left portion (similar to insertion inan ordered array).
– Repeat until done.
Insertion Sort
Online demo http://www.sorting-algorithms.com/insertion-sort
Romanian dance demo http://www.youtube.com/watch?v=ROalU379l3U
Insertion Sort in Class!
Each class row to be sorted by height Tallest people to my left
Need two people for i and j in each row Count number of comparisons and swaps
Foreshadowing
http://www.sorting-algorithms.com/random-initial-order
Summary
Bubble sort uses repeated comparisons and swaps to find the
biggest (or smallest) element in each pass Selection sort
selects the smallest element in each pass and reduces the cost of exchanges
Insertion sort inserts each element in the left assorted portion
and reduces the cost of comparisons All have average comparison cost of O(N2)