Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.

Similar presentations


Presentation on theme: "CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson."— Presentation transcript:

1 CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson

2  Algorithm Analysis  Simple sorting algorithms  Bubble sort  Selection sort  Insertion sort  Advanced sorting algorithms

3 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++;

4  “Remembered these, you have, hmm?”  Triangular sum  Geometric sum

5  Harmonic sum  Stirling’s approximation

6  Algorithm Analysis  Simple sorting algorithms  Bubble sort  Selection sort  Insertion sort  Advanced sorting algorithms

7 Sorting problem

8 Sorting Problem

9  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.

10  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  Example class that implements Comparable interface:  Date.java

11

12 Sorting Problem

13  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

14

15 After one pass, we find the biggest number. It’s like the biggest ‘bubble’ floats to the top of the surface, hence the name ‘bubble sort’. Examples: 3 5 2 7 1 6 4 Bubble Sort

16 In the second pass, we repeat the same process, but now we only have N-1 numbers to work on. The third pass is the same, with only N-2 numbers. … Repeat until all players are in order. Bubble Sort

17 Analysis of Bubble Sort Number of comparisons? Number of swaps?

18  O(N ) Analysis of Bubble Sort Number of comparisons? Number of swaps? best case: worst cast: average: 2 N(N  1) 2  O(N2)2 O(N2)2 N(N  1) 2 N(N  1) 4 O(1)

19 Selection Sort 1. Keep track of the index of the smallest number in each round. 2. Swap the smallest number towards the beginning of the array. 3. Repeat the above two steps.

20

21

22

23 Selection Sort Number of comparisons? Number of swaps?

24 O(N2)O(N2) Selection Sort Number of comparisons? Number of swaps? O(N) O(N)

25  Online demo  http://www.sorting-algorithms.com/selection-sort http://www.sorting-algorithms.com/selection-sort  Gypsy dance demo  http://www.youtube.com/watch?v=Ns4TPTC8wh w http://www.youtube.com/watch?v=Ns4TPTC8wh w

26 Insertion Sort How do you sort a hand of poker cards?

27 Insertion Sort Idea – Assume the left portion of the array is partially sorted (however, unlike selection sort, the elements are not necessarily in their final positions) – For each remaining element on the right portion, insert it to the left portion (similar to insertion in an ordered array). – Repeat until done.

28

29

30

31  Online demo  http://www.sorting-algorithms.com/insertion-sort http://www.sorting-algorithms.com/insertion-sort  Romanian dance demo  http://www.youtube.com/watch?v=ROalU379l3U http://www.youtube.com/watch?v=ROalU379l3U

32  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

33 Insertion Sort Number of comparisons? Number of exchanges?

34  Best case  N-1 comparisons  0 exchanges  Worst case  ~N 2 /2 comparisons  ~N 2 /2 exchanges

35  http://www.sorting-algorithms.com/random- initial-order http://www.sorting-algorithms.com/random- initial-order

36

37  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(N 2 )


Download ppt "CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson."

Similar presentations


Ads by Google