CS 171: Introduction to Computer Science II Simple Sorting · 2013. 1. 17. · •Elementary...

37
CS 171: Introduction to Computer Science II Simple Sorting Li Xiong

Transcript of CS 171: Introduction to Computer Science II Simple Sorting · 2013. 1. 17. · •Elementary...

  • CS 171: Introduction to Computer Science II

    Simple Sorting

    Li Xiong

  • Bonus question Give the big Oh cost of the following code fragments:

    a. int sum = 0;

    for (int i = N; i > 0; i /= 2)

    for (int j = 0; j < i; j++)

    sum++;

    b. int sum = 0;

    for (int i = 1; i < N; i *= 2)

    for(int j = 0; j < i; j++)

    sum++;

    c. int sum = 0;

    for (int i = 1; i < N; i *= 2)

    for (int j = 0; j < N; j++)

    sum++;

  • Useful Approximations

    • Harmonic sum

    1 + 1/2 + 1/3 + … + 1/N ~ lnN

    • Triangular sum

    1 + 2 + 3 + … + N = N(N+1)/2 ~ N2/2

    • Geometric sum

    1 + 2 + 4 + … + N = 2N -1 ~ 2N when N = 2n

    • Stirling’s approximation

    lg N! = lg1 + lg2 + lg3 + … + lgN ~ NlgN

  • Today

    • Elementary sorting algorithms

    –Bubble sort

    –Selection sort

    –Insertion sort

  • Sorting problem

  • Sorting Problem

  • Sorting Problem

    • sorting a hand of bridge cards

  • Sorting any type of data

  • The Comparable Interface

    10

    // This interface is defined in

    // java.lang package

    package java.lang;

    public interface Comparable {

    public int compareTo(Object o);

    }

  • String and Date Classes

    • Many classes (e.g., String and Date) in the Java library implement Comparable to define a natural order for the objects

    11

    public class String extends Object

    implements Comparable {

    // class body omitted

    }

    public class Date extends Object

    implements Comparable {

    // class body omitted

    }

  • Interface

  • Two useful sorting abstractions

  • Bubble Sort

    • Intuition: – Find the biggest number.

    – Find the second biggest number.

    – Find the third biggest number.

    – …

    • Bubble sort achieves this by repeatedly swapping two adjacent numbers.

  • • 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’.

    Bubble Sort

  • • 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

  • Analysis of Bubble Sort

    • Number of comparisons?

    • Number of swaps?

  • = O(N )

    = 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

    N(N -1)

    2

    N(N -1)

    4

    O(1)

  • 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.

  • Selection Sort

  • Selection Sort

  • Selection Sort Implementation

  • Selection Sort

    • Online demo –http://www.sorting-algorithms.com/selection-sort

    • Gypsy dance demo –http://www.youtube.com/watch?v=Ns4TPTC8whw

    http://www.sorting-algorithms.com/selection-sorthttp://www.sorting-algorithms.com/selection-sorthttp://www.sorting-algorithms.com/selection-sorthttp://www.sorting-algorithms.com/selection-sorthttp://www.sorting-algorithms.com/selection-sorthttp://www.youtube.com/watch?v=Ns4TPTC8whw

  • Selection Sort

    • Number of comparisons?

    • Number of swaps?

  • O(N2)

    Selection Sort

    • Number of comparisons?

    • Number of swaps?

    O(N)

  • Insertion Sort

    • How do you sort a hand of poker cards?

  • 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.

  • Insertion Sort Implementation

  • Insertion Sort

    • Online demo –http://www.sorting-algorithms.com/insertion-sort

    • Romanian dance demo –http://www.youtube.com/watch?v=ROalU379l3U

    http://www.sorting-algorithms.com/insertion-sorthttp://www.sorting-algorithms.com/insertion-sorthttp://www.sorting-algorithms.com/insertion-sorthttp://www.sorting-algorithms.com/insertion-sorthttp://www.sorting-algorithms.com/insertion-sorthttp://www.youtube.com/watch?v=ROalU379l3U

  • Insertion Sort

    • Number of comparisons?

    • Number of exchangess?

  • Insertion sort

    • Best case

    –N-1 comparisons

    –0 exchanges

    • Worst case

    –~N2/2 comparisons

    –~N2/2 exchanges

    • Average case

    –~N2/4 comparisons

    –~N2/4 exchanges

  • Summary

    • Selection and insertion sort are comparison

    based.

    • Both have an average comparison cost

    • Later we will learn several faster sorting algorithms, with a typical cost of O(N logN)

    of O(N2)

  • Java’s Sorting Methods

    • Primitive Type Arrays: Arrays.sort(int[]);

    Arrays.sort(int[], int fromIdx, int toIdx);

    Arrays.sort(float[]);

    ……

  • Java’s Sorting Methods • Object Type Arrays:

    Arrays.sort(Object[], Comparator);

    Arrays.sort(Object[], int fromIdx, int toIdx, Comparator);

    ……

    • Comparator is used to define how to compare two objects (i.e. which is bigger / smaller).

    Arrays.sort(Object[]);

    int compare(T o1, T o2)

    boolean equals(Object obj)

    http://download.oracle.com/javase/6/docs/api/java/util/Comparator.htmlhttp://download.oracle.com/javase/6/docs/api/java/util/Comparator.htmlhttp://download.oracle.com/javase/6/docs/api/java/lang/Object.html