Algorithm(Robert Sedgewick)-Day04 | Elementary sorts

Sorting problem
Rearrange array of N items into ascending order.

Goal. Sort any type of data.

Sort random real numbers.
Sort strings from file in alphabetical order.
Sort the files in a given directory by filename.

public static void main(String[] args)
{
	File directory = new File(args[0]);
	

Callback = reference to executable code.

Client passes array of objects to sort() function.
The sort() function calls back object’s compareTo() method as needed.

Implementing callbacks.
Java: interfaces.
C: function pointers.
C++: class-type functors.
C#: delegates.
Python, Perl, ML, Javascript: first-class functions.

Total order
A binary relation <= that satisfies
Antisymmetry
Transitivity
Totality

Compararble API
Implement compareTo() so that v.compareTo(w)
is a total order
returns a negative integer, zero, or positive integer
if v is less than, equal to, or greater than w, respectively
Throws an exception if incompatible types(or either is null)

Two useful sorting abstractions
compares and exchanges

Selection Sort
In iteration i, find index min of smallest remaining entry.
Swap a[i] and a[min].

Algorithm. scans from left to right.

public class Selection
  {
     public static void sort(Comparable[] a)
     {  // Sort a[] into increasing order.
        int N = a.length;               // array length
        for (int i = 0; i < N; i++)
        {  // Exchange a[i] with smallest entry in a[i+1...N).
           int min = i;                 // index of minimal entr.
           for (int j = i+1; j < N; j++)
              if (less(a[j], a[min])) min = j;
           exch(a, i, min);
} }
     // See page 245 for less(), exch(), isSorted(), and main().
}

Selection sort uses ?N2/2 compares and N exchanges to sort an array of length N.

Insertion Sort
In iteration i, swap a[i] with each larger entry to its self.

Algorithm. scans form left to right.

 public class Insertion
  {
     public static void sort(Comparable[] a)
     {  // Sort a[] into increasing order.
        int N = a.length;
        for (int i = 1; i < N; i++)
        {  // Insert a[i] among a[i-1], a[i-2], a[i-3]... ..
           for (int j = i; j > 0 && less(a[j], a[j-1]); j--)
              exch(a, j, j-1);
} }
     // See page 245 for less(), exch(), isSorted(), and main().
}

Insertion sort uses? N2/4 compares and ?N2/4 exchanges to sort a randomly ordered array of length N with distinct keys, on the average. The worst case is ?N2/2 compares and ?N2/2 exchanges and the best case is N ? 1 compares and 0 exchanges.

partially sorted array

Shellsort

Idea. Move entires more than one position at a time by h-sorting the array.

h-sorting. Insertion sort, with strie length h.
Why insertion sort?
Big increments => small subarray
Small increment => nearly in order. [stay tuned]

Proposition. A g-sorted array remains g-sorted after h-sorting it.

Shuffle sort
Generate a random real number for each array entry. (useful for shuffling columns in a spreadsheet)
Sort the array.

Goal. Rearrange array so that result is a uniformly random permutation in linear time.

Knuth shuffle
In iteration i, pick integer r between 0 and i uniformly at radom.
Swap a[i] and a[r].

Convex hull
The convex hull of a set of N points is the smallest perimeter fence cenclosin the points.

你可能感兴趣的:(Algorithm)