CS2 - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

CS2

Description:

... the same technique you use to 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:68
Avg rating:3.0/5.0
Slides: 38
Provided by: ccGa
Category:
Tags: birthday | cards | cs2

less

Transcript and Presenter's Notes

Title: CS2


1
CS2
  • Module 33
  • Category CS Concepts
  • Topic Sorting
  • Objectives
  • Insertion sort

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

3
Insertion Sort
  • Insertion sort is a very basic technique
    typically used when sorting very small numbers of
    objects
  • Today we'll look at Insertion Sort.
  • Not because it's efficient (it isnt)
  • Because it is a good example of linked list
    operations in conjunction with the comparable
    interface
  • Insertion sort is the same technique you use to
    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
20
19
19
-791
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 static int composDate(Date date)
  • return date.year 10000
  • date.month 100
  • date.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)
  • int retval 0
  • Date d (Date)o
  • int thisOne composDate(this)
  • int otherOne composDate(d)
  • if(thisOne otherOne)
  • retval 1
  • else if(thisOne
  • retval -1
  • else
  • retval 0
  • return retval

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 static int composMonDay(Date date)
  • return date.getMonth() 100 date.getDay()

16
  • // class Girlfriend
  • public int compareTo(Object o)
  • int retval
  • Girlfriend gf (Girlfriend)o
  • int thisOne composMonDay(this.getBirthday())
  • int otherOne composMonDay(gf.getBirthday())
  • if(thisOne
  • retval -1
  • else if(thisOne otherOne)
  • retval 1
  • else
  • retval 0
  • return retval

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 int compareTo(Object o)
  • ListNode ln (ListNode)o
  • return
  • getData().compareTo(((ListNode)o
    ).getData())
  • 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)

Do we really need ListNode to be Comparable?
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()
  • sl1.add("abc")
  • sl1.add("xyz")
  • System.out.println(sl1)
  • sl1.add("aaa")
  • sl1.add("zzz")
  • sl1.add("mmm")
  • System.out.println(sl1)

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

34
Questions?
35
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
    "almost" sorted.

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