Download Elementary Sorts-Constructing Algorithms and Representing Data-Lecture Slides and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
3.1 Elementary Sorts
2
Basic Terms
Ex: student record in a University.
Sort: rearrange sequence of objects into ascending order.
3
Rules of the Game
Goal. Write robust sorting library that can sort any type of data
into sorted order using the data type's natural order.
Callbacks.
! Client passes array of objects to sorting routine.
! Sorting routine calls back object's comparison function as needed.
Implementing callbacks.
! Java: interfaces.
! C: function pointers.
! C++: functors.
! C#: delegates.
! Lisp: first class functions.
client data type sorting library construct callbacks compare sort 4
Comparable Interface
Comparable interface. Require a method so that v.compareTo(w)returns:
! A negative integer if v is less than w.
! A positive integer if v is greater than w.
! Zero if v is equal to w.
Consistency. It is the programmer's responsibility to ensure that
compareTo() specifies a total order.
! Transitivity: if a < b and b < c, then a < c.
! Trichotomy: either (i) a < b or (ii) b < a or (iii) a = b.
Built-in comparable types. String, Double, Integer, Date, File.
User-defined comparable types. Implement the Comparable interface.
5
Implementing the Comparable Interface: Date
public class Date implements Comparable { private int month, day, year; public Date(int m, int d, int y) { month = m; day = d; year = y; } public int compareTo(Date b) { Date a = this; if (a.year < b.year ) return - 1 ; if (a.year > b.year ) return + 1 ; if (a.month < b.month) return - 1 ; if (a.month > b.month) return + 1 ; if (a.day < b.day ) return - 1 ; if (a.day > b.day ) return + 1 ; return 0 ; } } only compare dates to other dates 6
Two Array Sorting Abstractions
Helper functions. Refer to data only through two operations.
! Less. Is v less than w?
! Exchange. Swap object in array at index i with the one at index j.
private static boolean less(Comparable v, Comparable w) { return (v.compareTo(w) < 0 ); } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
Check if Sorted
Example usage. Is the input sorted?
public static boolean isSorted(Comparable[] a) { for (int i = 1 ; i < a.length; i++) if (less(a[i], a[i- 1 ])) return false; return true; }
Insertion Sort
13
Selection Sort
Selection sort.
!! scans from left to right.
! Elements to the left of! are fixed and in ascending order.
! No element to left of! is larger than any element to its right.
in final order! 14
Selection Sort Example
15
Selection Sort Inner Loop: Maintaining the Invariant
Selection sort inner loop.
! Identify index of minimum item.
! Exchange into position.
int min = i; for (int j = i+ 1 ; j < N; j++) if (less(a[j], a[min])) min = j; exch(a, i, min);
16
Selection Sort: Java Implementation
public class Selection { private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0 ; } private static void exch(Comparable[] a, int i, int j) { Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; } public static void sort(Comparable a[]) { for (int i = 0 ; i < a.length; i++) { int min = i; for (int j = i+ 1 ; j < a.length; j++) if (less(a[j], a[min])) min = j; exch(a, i, min); } } } selection sort a[]
import java.io.File; public class Files { public static void main(String[] args) { File directory = new File(args[ 0 ]); File[] files = directory.listFiles(); Selection.sort(files); for (int i = 0 ; i < files.length; i++) System.out.println(files[i]); } } % java Files. Insertion.class Insertion.java InsertionX.class InsertionX.java Selection.class Selection.java Shell.class Shell.java ShellX.class ShellX.java index.html
Selection Sort: Sample Application
List files. List the files in the current directory, sorted by file name.
Analysis
19
Performance for Randomly Ordered Files
Selection.
! Always search through right part.
! (1 + 2 + ... + N) " N^2 / 2 compares.
" N exchanges.
Insertion.
! Each element moves halfway back.
! (1 + 2 + ... + N) / 2 " N^2 / 4 compares.
" N^2 / 4 exchanges.
Bottom line: insertion, selection similar.
Sorting Challenges
29
Visual Sorting Puzzle
1. Insertion sort.
2. Selection sort.
3. Bubble sort.
random sorted reverse sorted