11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8 • Chapter 2: Procedures, Variables, and Extending Mapleencounter situations in following sections of this chapter in which it is necessaryto nest procedures. Since the next step is to partition each of thetwo subarrays by calling quicksort recursively, partition must returnthe location of the element which divides the partition.Example This example illustrates the role of nested procedures. Theouter procedure, quicksort, contains the inner procedure, partition.> quicksort := proc(A::array(1, numeric),> m::integer, n::integer)> local partition, p;>> partition := proc(m,n)> i := m;> j := n;> x := A[j];> while i if A[i]>x then> A[j] := A[i];> j := j-1;> A[i] := A[j];> else> i := i+1;> end if;> end do;> A[j] := x;> p := j;> end proc:>> if m=n there is nothing to do> p:=partition(m, n);> quicksort(A, m, p-1);> quicksort(A, p+1, n);> end if;>> eval(A);> end proc:Warning, ‘i‘ is implicitly declared local to procedure‘partition‘Warning, ‘j‘ is implicitly declared local to procedure‘partition‘Warning, ‘x‘ is implicitly declared local to procedure‘partition‘> a := array( [2,4,1,5,3] );a := [2, 4, 1, 5, 3]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!