{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

100lecture33

# 100lecture33 - Introduction to Computation and Problem...

This preview shows pages 1–5. Sign up to view the full content.

Class 33: Sorting December 6, 2005 Introduction to Computation and Problem Solving Class 33: Sorting December 6, 2005 Prof. Steven R. Lerman and Dr. V. Judson Harward Why Is Sorting Interesting? Sorting is an operation that occurs as part of many larger programs. There are many ways to sort, and computer scientists have devoted much research to devising efficient sorting algorithms. Sorting algorithms illustrate a number of important principles of algorithm design, some of them counterintuitive. We will examine a series of sorting algorithms in this session and try and discover the tradeoffs between them. 2 1

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

View Full Document
Sorting Order Sorting implies that the items to be sorted are ordered. Such items should implement the Comparable<T> interface by having a method compareTo(T o) that compares object o with current object. We will sort Objects of generic type <T> that implement the Comparable<T> interface. Signature is of form public static <T extends Comparable<T>> sortMethod(T[ ] data) Classes such as Integer and String implement Comparable<T>. For general objects, we need to provide implementation of compareTo( ). 3 Sorting Methods There are times when we want to sort only part of a collection. A Sort must supply two methods, one to sort an entire array of objects and one to sort a portion of an array specified by start and end elements (a true closed interval, not half open): public static <T extends Comparable<T>> void sort(T[] d,int start, int end); public static <T extends Comparable<T>> void sort(Object[] d); 4 2
Insertion Sort Insertion sorting is a formalized version of the way most of us sort cards. In an insertion sort, items are selected one at a time from an unsorted list and inserted into a sorted list. It is a good example of an incremental or iterative algorithm. It is simple and intuitive, but we can still do a little to optimize it. To save memory and unnecessary copying we sort the array in place. We do this by using the low end of the array to grow the sorted list against the unsorted upper end of the array. 5 6 Insertion Sort Diagram 7 4 3 6 3 Initially Sorted 4 7 3 6 3 After 1 insertion Sorted 4 7 3 6 3 After 3 insertions Sorted . . . Unsorted Unsorted Unsorted 3

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

View Full Document
Insertion Sort Code public class Item implements Comparable<Item> { public double key; public String value; public Item(double k, String v) { key= k; value= v; } public int compareTo(Item o) { if (key < o.key ) return -1; else if (key > o.key) return 1; else return 0; } } 7 Insertion Sort Code p.2 public class InsertionSortMain { public static void main(String[ ] args) { Item a= new Item(77.0, "Al");
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

100lecture33 - Introduction to Computation and Problem...

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

View Full Document
Ask a homework question - tutors are online