CS2_33_Sorting

CS2_33_Sorting - CS2 Module 33 Category: CS Concepts Topic:...

Info iconThis preview shows pages 1–20. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2 Module 33 Category: CS Concepts Topic: Sorting Objectives Insertion sort More on Comparable QuickSort CS 2 Introduction to Object Oriented Programming Module 33 CS Concepts Sorting 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 You might sort by suit... You might sort by first name... You might sort by age... 12/18/1980 6/9/1981 11/17/2791 12/21/1981 20 19 19-791 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. 12/18 6/9 11/17 12/21 (Okay some of them.) The point is that you decide The point is that you decide how you want the data sorted. how you want the data sorted. Let's write a date class Note: Java actually has a date class 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; } // 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; } // class Date (continued) public int compareTo(Object o) { Date d = (Date)o; return composDate() - d.composdate(); } } Questions? Let's create a girlfriend card class 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; } // 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(); } // class Girlfriend public int compareTo(Object o) { Girlfriend gf = (Girlfriend)o; return composDate() - gf.composDate(); } } Questions? Next a DataNode 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; }...
View Full Document

Page1 / 61

CS2_33_Sorting - CS2 Module 33 Category: CS Concepts Topic:...

This preview shows document pages 1 - 20. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online