Algorithms Flashcards

1
Q

Write a quicksort algorithm in pseudo-code that will sort the contents of a one-dimensional string array.

A

Declare Subprocedure QuickSort (myArray is string, indexLow is integer, indexHi is
integer)

Pivot is string
tmpSwap is string
tmpLow is integer
tmpHi is integer

tmpLow = indexLow
tmpHi =indexHi

pivot = myArray (int(indexLow + indexHi)/2))

while (tmpLow <= tmpHi)

    while (myArray(tmpLow) < pivot and tmpLow < indexHi)
            tmpLow = tmpLow + 1
    wend

    while (pivot < myArray(tmpHi=i) and tmpHi > indexLow)
            tmpHi = tmpHi – 1
    wend
        if (tmpLow <= tmpHi) then
                tmpSwap = myArray(tmpLow)
                myArray(tmpLow) = myArray(tmpHi)
                myArray(tmpHi) = tmpSwap
                tmpLow = tmpLow + 1
                tmpHi = tmpHi -1
       end if
wend

if (indexLow < tmpHi) then QuickSort (myArray , indexLow, tmpHi)
if (tmpLow < indexHi) then QuickSort (my Array, tmpLow, indexHi)

end sub

How well did you know this?
1
Not at all
2
3
4
5
Perfectly