Docstoc

Tutoriaal 5

Document Sample
Tutoriaal 5 Powered By Docstoc
					            UNIVERSITEIT VAN STELLENBOSCH
              FAKULTEIT INGENIEURSWESE
                                  Rekenaarstelsels 214 - 2002

                                            Prakties 6

Doel:      Implementeer binere soek en QuickSort algoritmes.

Kursus webblad:        http://learn.sun.ac.za



Opdrag 1
Skryf ‘n metode wat die binere soek algoritme implementeer.                        Die metode
moet die volgende as paramters aanvaar:
Die skikking waarin die lys waardes gestoor is.
Die waarde waarna gesoek moet word.

Demonstreer die korrekte werking van hierdie metode deur dit te roep vanaf
die main metode van die program met ‘n gesorteerde lys waardes (minimum
40 waardes).

Opdrag 2
Skryf ‘n metode wat die QuickSort algoritme implementeer. Verwys na die
bylae tot die prakties vir ‘n verderre verduideliking van QuickSort, meer
spesifiek oor hoe om die omruiling effektief te doen.

Demonstreer die werking van die metode deur ‘n skikking van grootte 500 met
lukrake getalle te vul, en dan te sorteer met ‘n roep na die metode.

Bonus

Skryf ook ‘n metode wat Bubblesort implementeer en sorteer dieselfde lys met
hierdie metode.

Vergelyk die hoeveelheid stappe wat elke algoritme neem om die lys te
sorteer. ‘n Stap is wanneer 2 elemente vergelyk word.

Herhaal die eksperiment en kyk of jy kan aflei dat QuickSort gemiddeld in die
orde van n*log(n) stappe neem, en Bubblesort gemiddeld in die orde van n2
stappe.

Bylae A QuickSort verduideliking
Idea

First, the sequence to be sorted a is partitioned into two parts, such that all elements of the
first part b are less than or equal to all elements of the second part c (divide). Then the two
parts are sorted separately by recursive application of the same procedure (conquer).
Recombination of the two parts yields the sorted sequence (combine). Figure 1 illustrates this
approach.




Figure 1: Quicksort(n)

The first step of the partition procedure is choosing a comparison element x. All elements of
the sequence that are less than x are placed in the first part, all elements greater than x are
placed in the second part. For elements equal to x it does not matter into which part they
come. In the following algorithm it may also happen that an element equal to x remains
between the two parts.

Algorithm Partition

Input: sequence a0, ..., an-1 with n elements
Output:      permutation of the sequence such that all elements a0, ..., aj are less than or equal
to all elements ai, ..., an-1
         (i > j)
Method:      choose the element in the middle of the sequence as comparison element x
             let i = 0 and j = n-1
             while i <=j
                  search the first element ai which is greater than or equal to x
                  search the last element aj which is less than or equal to x
                  if i <=j
                       exchange ai and aj
                       let i = i+1 and j = j-1

After partitioning the sequence, Quicksort treats the two parts recursively by the same
procedure. The recursion ends whenever a part consists of one element only.

				
DOCUMENT INFO
Shared By:
Tags: Tutoriaal
Stats:
views:13
posted:12/5/2009
language:Afrikaans
pages:2
Description: Tutoriaal 5