Previous Contents Next

2   Sortieren

Literatur: Kapitel 7 und 8 aus [CLR90]. Knuth [Knu73b] enthält mehr über Sortieren als man wissen will. Heapsort stammt von Williams [Wil64], Quicksort von Tony Hoare [Hoa62]
Inhalt Heaps ·Heapsort ·Quicksort
Heap-Datenstruktur


Implementierung: Heaps als Array


Heapsort: Aufbau
Einfügen in den Heap




Heapify (A, i)                  
  l := Left(i); r := Right(i);  // Left(i), Right(i) are heaps   

  if l <= heapsize(A) and A[l] > A[i]   // finde das maximum
     then largest := l  else largest := i;
  if r <= heapsize(A) and  A[r] > A[largest]
     then largest := r;

  if largest != i                 // Verletzung der Heapbedingung?
     then exchange (A[i] , A[largest]);
          Heapify(A, largest);





Aufbau des Heaps


Build-Heap(A)  
  heapsize[A]  := length[A];
  for i= floor(length(A)/2)) downto 1  // bottom-up
  do  heapify(A,i);





Heapsort






Heapsort(A)

   build_heap(A);                       // build initial heap
   for i = length(A) downto 2           
   do  exchange A[1] A[i];              // extract maximum
       heapsize[A] := heapsize[A] - 1;  // decrease size of heap
       Heapify(A,1);                    // repair heap condition



Quicksort


Quicksort (2)


language=Ocaml,escapeinside=(**)

(* $Id: quicksort.ml,v 1.8 2001/11/11 12:40:19 softtech Exp $ *)

(* A functional implementation of quicksort. The divide-and-conquer
   approach is visible in the recursive inner $\ocwlowerid{sort}$-function:
   It chooses as pivot the first element. Using the auxiliary function
   $\ocwlowerid{filter}$,\footnote{The function is directly available from
   the standard-library. It's included here for sake of reference.} it
   splits the remainder of the list into two halves, the elements less or
   equal the pivot and those larger than the pivot.  The sublists are
   sorted and, together with the pivot in the middle, put together to the
   sorted list.

*)

(*i*)
open Basic;;
open Sort;;
(*i*)



module QuicksortFun (E: ORDERED) : (SORT with type elem = E.t) =
    struct
      type elem = E.t 
     
      let rec filter p l = match l with  (* auxiliary function *)
     []     -> []
      | x :: tl ->  
   if   (p x) 
   then  x::(filter p tl)
   else  filter p tl
     
      let rec sort (l : elem list) =   
 match l with  
          []  -> []
 | x :: tl  ->                                   (* x = pivot *)
     sort (filter (function y -> E.lt(y,x)) tl)
     @ [x] @                                     
     sort (filter (function y -> E.geq(y,x)) tl)
    end







Quicksort mit arrays (1)




Quicksort(A,p,r)
if  p < r 
    then   q := Partition(A,p,r)   
           Quicksort(A,p,q);
           Quicksort(A,q+1,r);





Quicksort mit arrays: Partitionieren


I1 A[i'] £ x £ A[j'] für alle i'< i, j' > j.
I2 A[i'] £ x £ A[j'] für alle i'£ i, j' ³ j.




Partition(A,p,r)                      /* p <= r         */
  x := A[p];
  i := p-1;
  j := r+1;
 
  while true                          /* Invariante I_2 */
  do     repeat j := j-1
           until A[j] <= x;
         repeat i := i+1
           until A[i] >= x;
         if   i < j                   /* Invariante I_1 */
         then exchange A[i] <-> A[j]  /* Invariante I_2 */
         else return   j;             /* j <= i         */
  od;
  
         



Analyse von Quicksort: worst case
Analyse von Quicksort: best case




February 5, 2002
Previous Contents Next