CS2 - PowerPoint PPT Presentation

1 / 61
About This Presentation
Title:

CS2

Description:

... technique similar to how you arrange playing cards in your hand ... a birthday card in the desperate hope that they might. come back. 12/18. 6/9. 11/17. 12/21 ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 62
Provided by: ccGa
Category:
Tags: birthday | cards | cs2

less

Transcript and Presenter's Notes

Title: CS2


1
CS2
  • Module 37
  • Category CS Concepts
  • Topic Sorting
  • Objectives
  • Insertion sort
  • More on Comparable
  • QuickSort

2
CS 2
  • Introduction to
  • Object Oriented Programming
  • Module 37
  • CS Concepts
  • Sorting

3
Insertion Sort
  • Insertion sort is a very basic technique
    typically used when sorting very few objects.
  • Lets look at Insertion Sort
  • Not efficient
  • Good example of linked list Comparable
    interface
  • Insertion sort is a technique similar to how you
    arrange playing cards in your hand

4
You might sort by suit...
5
You might sort by first name...
6
You might sort by age...
12/18/1980
6/9/1981
11/17/2791
12/21/1981
22
22
21
-789
7
Or you could sort them in the order their
birthdays occur during the year. That way you can
send them a birthday card in the desperate hope
that they might come back.
(Okay some of them.)
The point is that you decide how you want the
data sorted.
8
Let's write a date class
  • Note Java actually has a date class

9
  • class Date implements Comparable
  • private int month
  • private int day
  • private int year
  • public Date(int month, int day, int year)
  • setMonth(month)
  • setDay(day)
  • setYear(year)
  • public String toString()
  • return "" month "/" day "/" year
  • public int composDate()
  • return year 10000
  • month 100
  • day

10
  • // class Date (continued)
  • public void setMonth(int month)
  • this.month month
  • public int getMonth()
  • return month
  • public void setDay(int day)
  • this.day day
  • public int getDay()
  • return day
  • public void setYear(int year)
  • this.year year
  • public int getYear()
  • return year

11
  • // class Date (continued)
  • public int compareTo(Object o)
  • Date d (Date)o
  • return composDate() - d.composdate()

12
Questions?
13
Let's create a girlfriend card class
14
  • class Girlfriend implements Comparable
  • private String name
  • private Date birthday
  • public Girlfriend
  • (String name, int month, int day, int year)
  • this(name, new Date(month, day, year))
  • public Girlfriend(String name, Date birthday)
  • setName(name)
  • setBirthday(birthday)
  • public void setName(String name)
  • this.name name
  • public String getName()
  • return name

15
  • // class Girlfriend
  • public void setBirthday(Date birthday)
  • this.birthday birthday
  • public Date getBirthday()
  • return birthday
  • public String toString()
  • return "Girlfriend " name " Birthday "
  • birthday
  • public int composMonDay()
  • return getMonth() 100 getDay()

16
  • // class Girlfriend
  • public int compareTo(Object o)
  • Girlfriend gf (Girlfriend)o
  • return composDate() - gf.composDate()

17
Questions?
18
Next a DataNode
19
  • class DataNode implements Comparable
  • private Comparable data
  • public DataNode(Comparable data)
  • setData(data)
  • public void setData(Comparable data)
  • this.data data
  • public Comparable getData()
  • return data
  • public String toString()
  • return "" data
  • public int compareTo(Object o)
  • DataNode dn (DataNode)o
  • return this.getData().compareTo(dn.getData())

20
  • // class DataNode (continued)
  • public boolean equals(Object o)
  • DataNode dn (DataNode)o
  • return getData().equals(dn.getData())
  • public static void main(String args)
  • DataNode dn1 new DataNode("Node 1")
  • DataNode dn2 new DataNode("Node 2")
  • DataNode nul new DataNode(null)
  • System.out.println(dn1)
  • System.out.println(dn2)
  • System.out.println(nul)
  • System.out.println("dn1.compareTo(dn2)"
  • dn1.compareTo(dn2))
  • System.out.println("dn2.compareTo(dn1)"
  • dn2.compareTo(dn1))
  • DataNode dngf new DataNode
  • (new Girlfriend("Chewie", 11, 17, 2791))

21
Questions?
22
ListNode
23
  • class ListNode extends DataNode implements
    Comparable
  • private ListNode next
  • public ListNode(Comparable data)
  • this(data, null)
  • public ListNode(Comparable data, ListNode next)
  • super(data)
  • setNext(next)
  • public void setNext(ListNode next)
  • this.next next
  • public ListNode getNext()
  • return next
  • public String toString()
  • return "Data " getData() " Next\n"
    next

24
  • // class ListNode (continued)
  • public static void main(String args)
  • ListNode ln1 new ListNode("abc")
  • ListNode ln2 new ListNode("xyz")
  • ListNode lnBS new ListNode(
  • new Girlfriend("Brittany", 12, 21,
    1981))
  • ListNode lnCA new ListNode(
  • new Girlfriend("Christina", 12, 18,
    1980))
  • System.out.println(ln1)
  • System.out.println(ln2)
  • System.out.println(lnBS)
  • System.out.println(lnCA)

25
  • // class ListNode (continued)
  • System.out.println("ln1.compareTo(ln2) "

  • ln1.compareTo(ln2))
  • System.out.println("ln2.compareTo(ln1) "

  • ln2.compareTo(ln1))
  • System.out.println("lnBS.compareTo(lnCA) "

  • lnBS.compareTo(lnCA))
  • System.out.println("lnCA.compareTo(lnBS) "

  • lnCA.compareTo(lnBS))
  • System.out.println
  • (("Brittany").compareTo("Christina"))
  • //System.out.println(lnBS.compareTo(ln1))
  • ListNode n3 new ListNode("Third")
  • ListNode n2 new ListNode("Second", n3)
  • ListNode n1 new ListNode("First", n2)
  • ListNode head new ListNode("Head", n1)

26
  • // class ListNode (continued)
  • n1 null
  • n2 null
  • n3 null
  • System.out.println(head)
  • ListNode a new ListNode(
  • new Girlfriend("Albertina",
    1,1,100))
  • ListNode b new ListNode(
  • new Girlfriend("Zoe", 12, 31,
    3000))
  • System.out.println(a.compareTo(b))
  • // main
  • // class

27
Questions?
28
SortedList
29
  • class SortedList
  • private ListNode head
  • public SortedList()
  • head null
  • public String toString()
  • return "SortedList\n" head

30
  • // class SortedList (continued)
  • public void add(Comparable data)
  • ListNode temp new ListNode(data)
  • if(head null)
  • head temp
  • else if(head.compareTo(temp) 0)
  • temp.setNext(head)
  • head temp
  • else
  • add(head, temp)

31
  • // class SortedList (continued)
  • private void add(ListNode current, ListNode
    temp)
  • if(current.getNext() null)
  • current.setNext(temp)
  • else if(current.getNext().compareTo(temp) 0)
  • temp.setNext(current.getNext())
  • current.setNext(temp)
  • else
  • add(current.getNext(), temp)

32
  • // class SortedList (continued)
  • public static void main(String args)
  • SortedList sl1 new SortedList()
  • s1.add("abc")
  • s1.add("xyz")
  • System.out.println(s1)
  • s1.add("aaa")
  • s1.add("zzz")
  • s1.add("mmm")
  • System.out.println(s1)

33
  • // class SortedList (continued)
  • // main (continued)
  • SortedList s2 new SortedList()
  • s2.add(new Girlfriend("Brittany", 12, 21,
    1981))
  • Date d new Date(6, 9, 1981)
  • Girlfriend gf2 new Girlfriend("Natalie", d)
  • s2.add(gf2)
  • s2.add(new Girlfriend("Christina", 12, 18,
    1980))
  • s2.add(new Girlfriend("Chewie", 11, 17, 2791))
  • s2.add(new Girlfriend("First", 1,1,3000))
  • s2.add(new Girlfriend("Last", 12,31,1000))
  • System.out.println(s2)
  • // main

34
Questions?
35
QuickSort
  • MergeSort provided a high performance sorting
    algorithm ( O(n log n) ) but requires twice as
    much space as there are keys
  • HeapSort allowed sorting in place while still
    performing at a ( O(n log n) ) level
  • But n log n only classifies algorithms at a
    certain "rough" level
  • In 1961 Tony Hoare published his QuickSort
    algorithm which provided better performance than
    HeapSort

36
QuickSort
  • Very similar to Merge Sort
  • Divide and conquer
  • Very slick design improvement to minimize effort
  • use the data values to choose the best place to
    divide
  • Pass which has O(N) is just comparing values, not
    moving elements
  • Moving elements is done by swaps
  • not necessary to merge results
  • avoids the inefficiency of the extra copies
  • But
  • depends on data values being irregular
  • performance is O(N2) or worse if the array is
    already sorted
  • There are modifications to overcome these
    limitations
  • We won't cover them

37
QuickSort
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

38
QuickSort
45
23
37
14
98
67
33
42
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

39
QuickSort
pivot
45
23
37
14
98
67
33
42
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

40
QuickSort
pivot
45
23
37
14
98
67
33
42
L
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

41
QuickSort
pivot
45
23
37
14
98
67
33
42
L
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

42
QuickSort
pivot
45
23
37
14
98
67
33
42
L
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

43
QuickSort
pivot
45
23
37
14
98
67
33
42
L
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

44
QuickSort
pivot
45
23
37
14
98
67
33
42
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

45
QuickSort
pivot
45
23
37
14
98
67
33
42
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

46
QuickSort
pivot
45
23
37
14
42
67
33
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

47
QuickSort
pivot
45
23
37
14
42
67
33
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

48
QuickSort
pivot
45
23
37
14
42
67
33
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

49
QuickSort
pivot
45
23
37
14
42
67
33
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

50
QuickSort
pivot
45
23
37
14
42
67
33
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

51
QuickSort
pivot
45
23
37
14
42
33
67
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

52
QuickSort
pivot
45
23
37
14
42
33
67
98
L
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

53
QuickSort
pivot
45
23
37
14
42
33
67
98
R
L
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

54
QuickSort
pivot
45
23
37
14
42
33
67
98
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

55
QuickSort
pivot
33
23
37
14
42
45
67
98
R
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

56
QuickSort
pivot
33
23
37
14
42
67
98
45
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

57
QuickSort
pivot
33
23
37
14
42
67
98
45
  • Pick a pivot value
  • Go left to right looking for a larger or equal
    element
  • Go right to left looking for a smaller or equal
    element
  • Swap the two elements found
  • Keep doing that until they cross
  • Swap the pivot element with the element from the
    right
  • The array has now been divided so that everything
    smaller than the pivot element is to its left,
    and everything larger is to its right in O(N)
    with minimum swaps
  • Now divide and conquer by doing the left and
    right parts separated by the new pivot element

58
Questions
59
Thoughts on Sorting
  • Sorting techniques with O(n2) such as insertion
    sort are only used in applications where n is
    very small
  • Mergesort which is a O(nlgn) sort requires 2n
    space
  • Heapsort (covered with heaps) was a sorting
    technique which allowed "sorting in place"
  • Quicksort is generally more efficient still
    O(nlgn) except in cases where data is already
    sorted or "almost" sorted.

60
Questions?
61
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com