A Pascal Quicksort

28
{***************************************************************************** * A Pascal quicksort. *****************************************************************************} PROGRAM Sort(input, output); CONST { Max array size. } MaxElts = 50; TYPE { Type of the element array. } IntArrType = ARRAY [1..MaxElts] OF Integer; VAR { Indexes, exchange temp, array size. } i, j, tmp, size: integer; { Array of ints } arr: IntArrType; { Read in the integers. } PROCEDURE ReadArr(VAR size: Integer; VAR a: IntArrType); BEGIN size := 1; WHILE NOT eof DO BEGIN readln(a[size]); IF NOT eof THEN size := size + 1 END END; { Use quicksort to sort the array of integers. } PROCEDURE Quicksort(size: Integer; VAR arr: IntArrType); { This does the actual work of the quicksort. It takes the parameters which define the range of the array to work on, and references the array as a global. } PROCEDURE QuicksortRecur(start, stop: integer); VAR m: integer; { The location separating the high and low parts. } splitpt: integer; { The quicksort split algorithm. Takes the range, and returns the split point. } FUNCTION Split(start, stop: integer): integer; VAR left, right: integer; { Scan pointers. }

Transcript of A Pascal Quicksort

Page 1: A Pascal Quicksort

{***************************************************************************** * A Pascal quicksort. *****************************************************************************}PROGRAM Sort(input, output); CONST { Max array size. } MaxElts = 50; TYPE { Type of the element array. } IntArrType = ARRAY [1..MaxElts] OF Integer;

VAR { Indexes, exchange temp, array size. } i, j, tmp, size: integer;

{ Array of ints } arr: IntArrType;

{ Read in the integers. } PROCEDURE ReadArr(VAR size: Integer; VAR a: IntArrType); BEGIN size := 1; WHILE NOT eof DO BEGIN readln(a[size]); IF NOT eof THEN size := size + 1 END END;

{ Use quicksort to sort the array of integers. } PROCEDURE Quicksort(size: Integer; VAR arr: IntArrType); { This does the actual work of the quicksort. It takes the parameters which define the range of the array to work on, and references the array as a global. } PROCEDURE QuicksortRecur(start, stop: integer); VAR m: integer;

{ The location separating the high and low parts. } splitpt: integer;

{ The quicksort split algorithm. Takes the range, and returns the split point. } FUNCTION Split(start, stop: integer): integer; VAR left, right: integer; { Scan pointers. } pivot: integer; { Pivot value. }

{ Interchange the parameters. } PROCEDURE swap(VAR a, b: integer); VAR t: integer; BEGIN t := a; a := b; b := t

Page 2: A Pascal Quicksort

END;

BEGIN { Split } { Set up the pointers for the hight and low sections, and get the pivot value. } pivot := arr[start]; left := start + 1; right := stop;

{ Look for pairs out of place and swap 'em. } WHILE left <= right DO BEGIN WHILE (left <= stop) AND (arr[left] < pivot) DO left := left + 1; WHILE (right > start) AND (arr[right] >= pivot) DO right := right - 1; IF left < right THEN swap(arr[left], arr[right]); END;

{ Put the pivot between the halves. } swap(arr[start], arr[right]);

{ This is how you return function values in pascal. Yeccch. } Split := right END;

BEGIN { QuicksortRecur } { If there's anything to do... } IF start < stop THEN BEGIN splitpt := Split(start, stop); QuicksortRecur(start, splitpt-1); QuicksortRecur(splitpt+1, stop); END END; BEGIN { Quicksort } QuicksortRecur(1, size) END;

BEGIN { Read } ReadArr(size, arr);

{ Sort the contents. } Quicksort(size, arr);

{ Print. } FOR i := 1 TO size DO writeln(arr[i]) END.

SHELL SORT

Page 3: A Pascal Quicksort

sort( r, lo, up ) ArrayToSort r; int lo, up;

{int d, i, j; ArrayEntry tempr; for ( d=up-lo+1; d>1; ) { if (d=lo; i-- ) { tempr = r[i]; for ( j=i+d; j<=up && (tempr.k>r[j].k); j+=d ) r[j-d] = r[j]; r[j-d] = tempr; } } }

Definition

Recursion, in the computing or mathematical terms, is a method of defining functions in which the function being defined is applied within its own designation. The term is also used more generally to describe the process of repeating objects in a similar pattern.

Implementation

Here below you will find a simple recursive procedure allowing you to sort a table of (n) number of integers using the Shell sorting method:

Procedure Shell_Sort_Rec (Var t: TAB; n,h : integer); Var aux,i : integer; begin If h > 0 Then Begin If n > h Then begin Shell_Sort_Rec (t,n - h,h); If t[n] < t[n - h] Then Begin aux:= t[n]; i := n; Repeat t[i] := t[i - h]; i := i - h; Until (i = h) Or (aux > t[i - h]); t[i] := aux; End; End; Shell_Sort_Rec (t,n,h Div 3); End; End;

Page 4: A Pascal Quicksort

Notes

It is better to test this procedure on small tables, because in the case that the number of calls becomes

too important may spillover the limits of the memory stack allocate to recursive function. You can

increase the size of the test table on increasing the size of the stack:

Wednesday, 15 August 2007The Shell Sort

Yes, I know Delphi comes with a number of sorting routines, but I thought I'd describe a shell sort as it is fascinating little sort routine that is easily

understood and a very fast routine.

[Steve: Although the "Shellsort" algorythm was first published by Donald Shell (as pointed out by Anthony Mills, thanks Anthony), this 'Shell Sort' uses

a different approach that would be closer to a "Bubble Sort" with some changes. I therefore take a little 'artistic licence' in describing it in ways that

people can visually understand]

The name "Shell Sort" comes from those games that are played by the magician, conman, or simply the swift of hand when they try to get money

from the average passerby. Having usually 3 shells, a small coin or marble is placed under one of the shells. The shells are then swapped around at speed

and the passerby is asked to bet on which shell contains the marble.

The shell sort routine does that same swapping around, but only to place all the shells in order. Consider that each shell is numbered, in this case, 1 to 5,

but we'll first get our 'swift of hand' guy to jumble them up a little.

For the purposes of this blog, I'll limit the sort to only 5 integer numbers (or shells) although these could be words or extended reals or whatever. So long

as you can compare them to see which is the greatest, this routine is the same or similar.

Page 5: A Pascal Quicksort

What this routine does is traverse the 5 shells many times. Each time, it will check each shell and see if it is greater than the next shell, if it is, it will swap the two shells. This will have the effect of moving the largest numbers to the

right and the smaller numbers to the left - in other words, sorting them.

Consider the first time through, looking at each shell in turn, and swapping them with the next if it is larger, would, by default, move the largest number

to the end.

Lets take a look at what will happen on the first run through.

35241 (look at 1st number, swap if 3 > 5)35241 (look at 2nd number, swap if 5 > 2)32541 (look at 3rd number, swap if 5 > 4)32451 (look at 4th number, swap if 5 > 1)

32415

This moved the largest number (5) to the end. This means that the next time through, we would only need to look at the first 4 numbers, and so on until

there is only one number left. That will tell us that the shells are sorted. BTW: Did you notice that we only stepped throuigh the first 4 numbers? That's because the last number is, well, it's the last number and doesn't have a

next number to compare to.

Another way to know if they are all sorted (after all, we don't yet know if they are in order before we start) is if we traverse the shells and none needed to

be swapped. If we didn't swap any then its in order and we can stop.

Let's start. Create a new project in Delphi with a form. Drop a TMemo and a TButton on the form. We'll put all the code on the OnClick event of the button, so we can follow it easier. First declare the counters; the shells

themselves; and a true/false variable to tell us that no swaps were made during the last traversal.

var i, LastShell: integer;

Shell : Array[1..5] of integer; NoSwap: boolean;

Page 6: A Pascal Quicksort

Then we'll place a procedure inside the OnClick event (something that I would not normally do, but we're keeping it all tight for you). This procedure will simply display

the current state and order of the shells so we can see what's happening.

procedure ShowShells;var

i: integer; s: string;

begin // display the shell order in the TMemo

s := ''; for i := 1 to 5 do

s := s + IntToStr(Shell[i]); Memo1.lines.Add(s);

end;

Go on, say ShowShells shix times quickly. Then we'll add another procedure. This one will swap a shell with the next and tell us that the swap occured.

procedure Swap(var First, Second: integer);var temp: integer;

begin Temp := First; First := Second; Second := Temp; NoSwap := false;

end;

Note that I declared variable parameters, but I also could have swapped the array directly. Either way would have been acceptable in this situation.

Now we can start writing code in the OnClick event itself. First we'll fill the array with the current order of the shells.

begin Shell[1] := 3; Shell[2] := 5; Shell[3] := 2; Shell[4] := 4; Shell[5] := 1; memo1.Clear; ShowShells;

Now we can do the actual sorting, using the counters. Remember, we are going

Page 7: A Pascal Quicksort

down in the number of shells we need to sort each time (the largest will move to the end each time).

memo1.lines.add('now sorting...'); for LastShell := 5 downto 1 do

begin NoSwap := true; // No swaps this time through yet

// traverse the unsorted shells for i := 1 to LastShell-1 do

begin if Shell[i] > Shell[i+1] then

// its greater than next, swap them Swap(Shell[i], Shell[i+1]);

ShowShells; end;

if NoSwap then // we traversed the shells and didn't need to swap

// any therefore its sorted and we can stop. break;

end; memo1.lines.add('Done');

end;

Note that we only traversed the shells to LastShell-1? that's because we can't look to swap the lastshell with the next one as its the last shell :-)

Here's the full code...

procedure TForm1.Button1Click(Sender: TObject);var

i, LastShell: integer; Shell : Array[1..5] of integer;

NoSwap: boolean;

procedure ShowShells; var

i: integer; s: string;

begin s := '';

for i := 1 to 5 do s := s + IntToStr(Shell[i]);

Memo1.lines.Add(s); end;

procedure Swap(var First, Second: integer); var temp: integer;

begin Temp := First;

First := Second;

Page 8: A Pascal Quicksort

Second := Temp; NoSwap := false;

end;

begin // assign numbers to the array

Shell[1] := 3; Shell[2] := 5; Shell[3] := 2; Shell[4] := 4; Shell[5] := 1; memo1.Clear; ShowShells;

memo1.lines.add('now sorting...'); for LastShell := 5 downto 1 do

begin NoSwap := true; // No swaps this time through yet

// traverse the unsorted shells for i := 1 to LastShell-1 do

begin if Shell[i] > Shell[i+1] then

// its greater than next, swap them Swap(Shell[i], Shell[i+1]);

ShowShells; end;

if NoSwap then // we traversed the shells and didn't need to swap

// any therefore its sorted and we can stop. break;

end; memo1.lines.add('Done');

end;

Shell sort is a sorting algorithm, devised by Donald Shell in 1959, that is a generalization of insertion sort, which exploits the fact that insertion sort works efficiently on input that is already almost sorted. It improves on insertion sort by allowing the comparison and exchange of elements that are far apart. The last step of Shell sort is a plain insertion sort, but by then, the array of data is guaranteed to be almost sorted.

The algorithm is an example of an algorithm that is simple to code but difficult to analyze theoretically.

Description

The principle of Shell sort is to rearrange the file so that looking at every hth element yields a sorted file. We call such a file h-sorted. If the file is then k-sorted for some other integer k, then the file remains h-sorted.[3] For instance, if a list was 5-sorted and then 3-sorted, the list is now not only 3-sorted, but both 5- and 3-sorted. If this were not true, the algorithm would undo work that it had done in previous iterations, and would not achieve such a low running time.

Page 9: A Pascal Quicksort

The algorithm draws upon a sequence of positive integers known as the increment sequence. Any sequence will do, as long as it ends with 1, but some sequences perform better than others.[4]

The algorithm begins by performing a gap insertion sort, with the gap being the first number in the increment sequence. It continues to perform a gap insertion sort for each number in the sequence, until it finishes with a gap of 1. When the increment reaches 1, the gap insertion sort is simply an ordinary insertion sort, guaranteeing that the final list is sorted. Beginning with large increments allows elements in the file to move quickly towards their final positions, and makes it easier to subsequently sort for smaller increments.[3]

Although sorting algorithms exist that are more efficient, Shell sort remains a good choice for moderately large files because it has good running time and is easy to code.

[edit] Shell sort algorithm in pseudocode

The following is an implementation of Shell sort written in pseudocode. The increment sequence is a geometric sequence in which every term is roughly 2.2 times smaller than the previous one:

input: an array a of length n with array elements numbered 0 to n − 1inc ← round(n/2)while inc > 0 do: for i = inc .. n − 1 do: temp ← a[i] j ← i while j ≥ inc and a[j − inc] > temp do: a[j] ← a[j − inc] j ← j − inc a[j] ← temp inc ← round(inc / 2.2)

[edit] Analysis

Although Shell sort is easy to code, analyzing its performance is very difficult and depends on the choice of increment sequence. The algorithm was one of the first to break the quadratic time barrier, but this fact was not proven until some time after its discovery.[4]

The initial increment sequence suggested by Donald Shell was [1,2,4,8,16,...,2k], but this is a very poor choice in practice because it means that elements in odd positions are not compared with elements in even positions until the very last step. The original implementation performs O(n2) comparisons and exchanges in the worst case.[3] A simple change, replacing 2k with 2k-1, improves the worst-case running time to O(N3/2),[4] a bound that cannot be improved.[5]

A minor change given in V. Pratt's book[5] improved the bound to O(n log2 n). This is worse than the optimal comparison sorts, which are O(n log n), but lends itself to sorting networks and has the same asymptotic gate complexity as Batcher's bitonic sorter.

Consider a small value that is initially stored in the wrong end of the array. Using an O(n2) sort such as bubble sort or insertion sort, it will take roughly n comparisons and exchanges to move this value all the way to the other end of the array. Shell sort first moves values using giant step

Page 10: A Pascal Quicksort

sizes, so a small value will move a long way towards its final position, with just a few comparisons and exchanges.

One can visualize Shell sort in the following way: arrange the list into a table and sort the columns (using an insertion sort). Repeat this process, each time with smaller number of longer columns. At the end, the table has only one column. While transforming the list into a table makes it easier to visualize, the algorithm itself does its sorting in-place (by incrementing the index by the step size, i.e. using i += step_size instead of i++).

For example, consider a list of numbers like [ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ]. If we started with a step-size of 5, we could visualize this as breaking the list of numbers into a table with 5 columns. This would look like this:

13 14 94 33 8225 59 94 65 2345 27 73 25 3910

We then sort each column, which gives us

10 14 73 25 2313 27 94 33 3925 59 94 65 8245

When read back as a single list of numbers, we get [ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ]. Here, the 10 which was all the way at the end, has moved all the way to the beginning. This list is then again sorted using a 3-gap sort as shown below.

10 14 7325 23 1327 94 33 39 25 59 94 65 8245

Which gives us

10 14 1325 23 3327 25 59 39 65 73 45 94 8294

Now all that remains to be done is a 1-gap sort (simple insertion sort).

Page 11: A Pascal Quicksort

[edit] Gap sequence

The Shell sort algorithm in action

The gap sequence is an integral part of the Shell sort algorithm.

The gap sequence that was originally suggested by Donald Shell was to begin with N / 2 and to halve the number until it reaches 1. While this sequence provides significant performance enhancements over the quadratic algorithms such as insertion sort, it can be changed slightly to further decrease the average and worst-case running times. Weiss' textbook[6] demonstrates that this sequence allows a worst case O(n2) sort, if the data is initially in the array as (small_1, large_1, small_2, large_2, ...) - that is, the upper half of the numbers are placed, in sorted order, in the even index locations and the lower end of the numbers are placed similarly in the odd indexed locations.

Perhaps the most crucial property of Shell sort is that the elements remain k-sorted even as the gap diminishes.[5]

Depending on the choice of gap sequence, Shell sort has a proven worst-case running time of O(n2) (using Shell's increments that start with 1/2 the array size and divide by 2 each time),

O(n3 / 2) (using Hibbard's increments of 2k − 1), O(n4 / 3) (using Sedgewick's increments of

, or ), or O(nlog2n) (using Pratt's increments 2i3j),

and possibly unproven better running times. The existence of an O(nlogn) worst-case implementation of Shell sort was precluded by Poonen, Plaxton, and Suel.[7]

The best known sequence according to research by Marcin Ciura is 1, 4, 10, 23, 57, 132, 301, 701, 1750.[8] This study also concluded that "comparisons rather than moves should be considered the dominant operation in Shellsort."[9] A Shell sort using this sequence runs faster than an insertion sort, but even if it is faster than a quicksort for small arrays, it is slower for sufficiently big arrays. In his paper Ciura writes "the greatest increments play a minor role in the overall performance of a sequence," making it reasonable to extend Ciura's sequence beyond 701 as a geometric progression with a ratio roughly that of Ciura's last two elements, 1750/701. Taking the successor of each increment g to be floor(g*5/2)+1 for example would then extend Ciura's sequence as 701, 1753, 4383, 10958, 27396, 68491, 171228, 428071, 1070178, …, all pairs of which can be seen by inspection to be well decorrelated, most being relatively prime and none with common divisor greater than 12.

Page 12: A Pascal Quicksort

Another sequence which performs empirically well[citation needed] on large arrays is the Fibonacci numbers (leaving out one of the starting 1's) to the power of twice the golden ratio, which gives the following sequence: 1, 9, 34, 182, 836, 4025, 19001, 90358, 428481, 2034035, 9651787, 45806244, 217378076, 1031612713, ….[10]

Page 13: A Pascal Quicksort

Algorithms analysis

ARRAYS

a.   In Java, 'C' or Pascal, write a complete program to perform the following tasks:   [14 Marks]

1.   To declare a real (floating-point) array which stores ten numbers.

2.   To enter ten values from the keyboard into the array of elements.

3.   To write a method (function or subroutine) called from the main program which

selects and displays the largest array value.

b.   Construct a simple effects table ('dry run'), showing how the program variables change, with

the ten values:   [3 Marks]

o   63,   21,   -9,   64,   206,   184,   -300,   128,   206,   -80

b.   Explain how you would modify your selection method to detect and display duplicate

maximum values as in the test data above.   [3 Marks]

LINKED LISTS

Linked list data structures can be used to create variable length records on disc.

a.   With the aid of a diagram, explain the terms:   [11 Marks]

1.   Linked list.

2.   Control (or header) record.

3.   Pointers.

4.   Freespace (or garbage) blocks.

Page 14: A Pascal Quicksort

b.   Using further diagrams (or flowcharts if you prefer), explain how:   [9 Marks]

1.   A variable length record is extended by one block.

2.   A record is deleted.

A linked list data structure may be used to implement variable length records on disc storage.

a.   With the aid of a diagram, explain how linked list records may be organised in a disc file. If

possible, explain the terms block, pointer, header and freespace.   [10 Marks]

b.   Suggest a suitable application for linked lists.   [2 Marks]

c.   Suggest TWO advantages of using linked lists.   [4 Marks]

d.   Distinguish between singly linked lists and doubly linked lists.   [4 Marks]

The linked list data is a powerful data structure with a wide range of applications.

a.   With the aid of a diagram, explain what is meant by a linked list data structure.   [4 Marks]

b.   Describe briefly the benefits of using linked lists.   [4 Marks]

c.   Distinguish between singly linked lists and doubly linked lists.   [4 Marks]

d.   Describe how a stack could be implemented on a disc storage using a linked list structure.

Explain the purpose of any header block, pointers, and freespace (garbage).   [8 Marks]

a.   What is meant by a linked list structure?   [4 Marks]

b.   Explain how linked lists can be used to support variable length records.   [4 Marks]

c.   By means of a flowchart show how a block can be transferred from the freespace chain to a

variable length file record (the getblock logic).   [10 Marks]

Page 15: A Pascal Quicksort

d.   Suggest, with a reason, a suitable application for linked lists.   [2 Marks]

QUEUES

The queue is an important data structure. Queues are sometimes implemented using a circular store, as

in the case of entering characters from a keyboard.

a.   With the aid of a diagram, explain the meaning of a Queue.   [4 Marks]

b.   Describe briefly how a queue may be implemented using a circular store.   [4 Marks]

c.   Using flowcharts, or otherwise, describe the procedure when:   [10 Marks]

1.   an item is added to the queue.

2.   an item is removed from the queue.

d.   Explain one method for distinguishing between an empty queue and a full queue when using a

circular store.   [2 Marks]

a.   Using a diagram, define the data structure known as a Queue; explain how a queue may be

implemented with a one-dimensional array.   [6 Marks]

b.   Construct flowcharts (or pseudocode) to show how an item of data is:

1.   added to a queue.   [4 Marks]

2.   removed from a queue; (when an item is removed, assume that any remaining items

advance one place so that the head of the queue stays in a fixed position in the array).  

[7 Marks]

c.   Describe briefly TWO applications for queue storage.   [3 Marks]

Page 16: A Pascal Quicksort

a.   With a diagram define the data structure known as a Queue, showing how it may be

implemented using a simple linear array.   [6 Marks]

b.   Construct flowcharts to show how an item is: (in each case, identify any error situations.)   [8

Marks]

1.   Added to a queue.

2.   Removed from a queue.

c.   Suggest a disadvantage of using the simple linear array.   [2 Marks]

d.   Describe briefly ONE alternative method of queue storage management.   [4 Marks]

STACKS

The Stack is an important data structure.

a.   Explain the term Stack and describe its principle of operation. Using a diagram, explain how a

stack may be implemented in a high level programming language.   [8 Marks]

b.   Name TWO processes applicable to stacks. Describe the logic of these processes using

flowcharts or pseudocode.   [6 Marks]

c.   Explain the terms Stack Overflow and Stack Underflow; suggest how each can arise and describe

the likely consequences.   [4 Marks]

d.   Describe briefly TWO uses of stacks.   [2 Marks]

a.   With the aid of a diagram, define the data structure known as the Stack.   [4 Marks]

b.   With the help of a flowchart or pseudocode, explain the PUSH and POP operations.   [6 Marks]

c.   In a high level language such as Java, Pascal or 'C', implement a stack; your program should

include PUSH and POP methods (or functions), and, should input three items of data (X, then Y,

Page 17: A Pascal Quicksort

then Z) to the stack; finally, the program should recover the data from the stack and display the

output.   [10 Marks]

Stacks are widely used in computer systems.

a.   Explain the meaning of the term Stack.   [4 Marks]

b.   By constructing flowcharts, show how a data item is: (in each case, identify any error

conditions which can arise).   [8 Marks]

1.   added to the stack.

2.   removed from the stack.

c.   Describe an application which requires a stack and explain how the stack would be used.   [5

Marks]

d.   The job scheduler (part of an operating system) selects one task at a time for execution. Discuss

the suitability of a stack for storing tasks awaiting processing.   [3 Marks]

TREES

a.   With the aid of a diagram, define the data structure known as a Tree, and distinguish between a

binary tree and a general (multiway) tree.   [8 marks]

b.   With the aid of a flowchart, or otherwise, describe the search process for a binary tree.   [8

marks]

c.   Suggest TWO differences between the tree search and the binary search (binary chop).   [4

marks]

Page 18: A Pascal Quicksort

a.   With the aid of a diagram, define the data structure known as a Tree, and distinguish between a

binary tree and a general (simple) tree.   [8 marks]

b.   With the aid of a flowchart, or otherwise, describe the search process for a binary tree.   [8

marks]

c.   Compare, under TWO headings, the tree search and the binary search (binary chop).   [4 marks]

Tree structures are very useful for storing data and for retrieving records quickly.

a.   With the aid of a diagram, explain carefully the meaning of an Ordered Binary Tree.   [7 Marks]

b.   Using a flowchart, or pseudocode, show the logic of a tree search process (starting from the

entry of the search key).   [9 Marks]

c.   Compare the tree search and the binary search ('binary chop') in terms of:   [4 Marks]

1.   their search time for a given dataset size;

2.   their use in on-line applications.

The tree below contains records about individuals (Mike, Helen, Sue, ...):

a.   Describe in detail, by flowchart or or pseudocode, a process for traversing the records in

alphabetical order (Alan, Helen, Leo, ... Sue, Tom).   [8 Marks]

b.   Construct an effects table ('dry run') to show the steps as your process traverses the tree.   [4

Marks]

Page 19: A Pascal Quicksort

c.   State the conditions necessary for the correct operation of the process.   [4 Marks]

d.   Name and briefly describe TWO other methods of traversing the tree.   [4 Marks]

a.   Define the term Binary Tree.   [6 Marks]

b.   One method of traversing a binary tree is known as lnorder. Name, and describe briefly, the

other TWO methods.   [4 Marks]

c.   Describe in detail a procedure for the inorder traversal of a binary tree; (you may use a

flowchart, pseudocode or a program). Apply the procedure to the binary tree below, showing

the exact traversal sequence.   [10 Marks]

Tree structures are often used in file directory and database systems.

a.   With the aid of a diagram, define the term Tree (include in the definition: node, predecessor,

descendant, root and leaf).   [7 Marks]

b.   State the conditions necessary for a tree to be a Binary Tree.   [3 Marks]

c.   With the aid of a flowchart, or otherwise, describe the Tree Search procedure for an ordered

binary tree.   [8 Marks]

d.   Explain why binary trees are often less useful than other types of tree when searching.   [2

Marks]

Page 20: A Pascal Quicksort

SORTING AND SEARCHING

a.   Distinguish between Sorting and Merging.   [5 Marks]

b.   Describe in detail how a large data file of some 256,000 records, could be sorted (preferably

using a two-way merge process).   [9 Marks]

c.   Explain how the sort time (T) would vary according to the number of records (N) to be sorted.

If the time to sort 256,000 records is 90 minutes, estimate the sort times for:   [6 Marks]

1.   N = 64,000 records.

2.   N = 1,000,000 records.

Sorting processes, whether internal or external, can be very time-consuming.

a.   Suggest TWO reasons for sorting records.   [4 Marks]

b.   Distinguish between an lntemal Sort and an Extemal Sort.   [3 Marks]

c.   Explain briefly why, if there is a choice, an intemal sort would normally be preferable to an

external sort.   [2 Marks]

d.   A data file having 256,000 records, and 128 Mbytes in volume, needs to be sorted on a single

name field. Describe in detail a suitable sort process.   [8 Marks]

e.   Discuss whether the benefits of sorted data sets can be obtained without the need for a lengthy

sorting process.   [3 Marks]

Some internal sorting methods, such as Shell or Quicksort, are usually much faster than Selection or

Exchange methods.

a.   Explain the term Internal Sort.   [2 Marks]

b.   With the aid of a dry run (effects table), describe EITHER Shell sort OR Quicksort on the

following numbers:   [12 Marks]

Page 21: A Pascal Quicksort

  9   14   16   18   2   7   3   5  

c.

d.   Suggest how the sorting time depends on the number of items (N) to be sorted. Explain why

Shell or Quicksort are quicker than Selection or Exchange sorts.   [6 Marks]

The time to sort N items using an internal sort depends, for a given computer, on the value of N and on

the sort algorithm. Using the numeric list shown, with N=8 :

  10   3   6   11   5   12   9   8  

  Define the term Internal Sort.   [2 Marks]

  With a flowchart, or otherwise, describe a simple internal sort procedure; name the procedure

chosen.   [6 Marks]

  By means of a dry run (effects table), show in detail the action of your procedure on the list above;

identify as accurately as possible the main processes which contribute to the overall sort time.   [8

Marks]

  Discuss how the sort time would vary as N increased.   [4 Marks]

The merge, search, sort and critical path network analysis are well-known processes. The time to

complete each process in the computer depends partly on the number of data items (N) in the data

structure.

a.   Describe briefly a method (algorithm) for THREE of the following:   [14 Marks]

1.   a two-way merge of N records.

2.   a search for a key field in a database of N records.

3.   a sort of a file containing N records.

4.   a project analysis where there are N activities.

Page 22: A Pascal Quicksort

b.   If the process time was 10 seconds for each of the above methods when N = 1000, estimate the

process times for your three chosen algorithms when:   [6 Marks]

1.   N = 32000 (three estimates).

2.   N = 1000000 (three estimates).

Internal sorting methods vary in efficiency. Partition methods, such as Shell or Quicksort, are far

superior both to exchange methods such as 'bubblesort' and to insertion methods.

a.   Explain the term Internal Sort.   [2 Marks]

b.   With the aid of a simple effects table ('dry run') show how the set of numbers below would be

sorted using:

1.   A simple exchange sort (or an insertion sort).   [6 Marks]

2.   A partition sort.   [8 Marks]

  38   31   18   50   12   10   44   16  

3.

c.   Give reasons why partition sorts are more efficient than exchange and insertion sorting

methods.   [4 Marks]

There are many methods of sorting data; the time taken to complete a sort process depends on several

factors.

a.   Distinguish between an lntemal Sort and an Extemal Sort.   [3 Marks]

b.   Explain how a large data file having some 512,000 records could be sorted, preferably using a

merge process.   [7 Marks]

c.   Discuss FIVE factors which influence the time required to sort a set of N records (where 'N' is

the number of records to be sorted).   [10 Marks]

Page 23: A Pascal Quicksort