11_Sorting - Click to edit Master subtitle style © 2009 Dr...

Info iconThis preview shows pages 1–10. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style © 2009 Dr. Tim Data Structures and Algorithms I Introduction to Sorting Dr. Tim Margush University of Akron © 2009 © 2009 Dr. Tim Goals • Be able to describe sorting problems • Know the strategies and relative efficiencies of the basic sorting algorithms • Be able to define O(n) • Be able to write methods implementing the basic sorting algorithms © 2009 Dr. Tim © 2009 Dr. Tim Simple Sorting • Sorting Lists – Given an ordinal collection • First element, second element, … • Array, ArrayList, LinkedList, List • Simple sorting is based on comparisons – Objects must be Comparable or have a Comparator • elt_a.compareTo(elt_b) • cptr.compare(elt_a, elt_b) – Primitives are compared using <, <=, © 2009 Dr. Tim © 2009 Dr. Tim Sorting Requires Effort • Comparisons take time – s1.compareTo(s2) • Moving things in lists takes time – temp = theList.get(k) ;a swap – theList.set(k, theList.get(j)) – theList.set(j, temp) • Compare sort algorithm efficiencies by counting comparisons or data movements © 2009 Dr. Tim © 2009 Dr. Tim Analysis of Algorithms • Experimental Analysis – Depends on efficiency of the program – Depends on compiler – Depends on operating system – Depends on hardware – Depends on test data • Analytical Analysis – Depends on algorithm only © 2009 Dr. Tim © 2009 Dr. Tim Vocabulary • Computational problem – Problem to be solved using an algorithm • Input -> Algorithm -> Output • Problem instance – One particular input set • Size of instance – Memory needed to hold input set • Basic steps – Execution time is independent of the size of the instance © 2009 Dr. Tim © 2009 Dr. Tim Example • Problem: Find the smallest int in a list • Input: A list of ints of size n • Output: the smallest int in the list • Algorithm… – for(int i = 0; i<a.size; i++) • if (a.get(i).compareTo(smallestsofar)<0)… – for(T x:a) • if (x.compareTo(smallestsofar)<0)… © 2009 Dr. Tim © 2009 Dr. Tim Counting Basic Steps //Find an element smaller than target for (int k = 0; k < a.size(); k++){ if (a.get(k).compareTo(target)<0) return k; } return -1; © 2009 Dr. Tim © 2009 Dr. Tim Worst, Best, Average • Worst case – An input that causes the most basic steps • Maximum steps required over all instance (of this size) • Best case – An input resulting in fewest basic steps...
View Full Document

This note was uploaded on 02/22/2011 for the course CS 463 taught by Professor Dr. tim margush during the Fall '09 term at The University of Akron.

Page1 / 37

11_Sorting - Click to edit Master subtitle style © 2009 Dr...

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

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