APPLICATIONSDIVERS ★ SELECTION SORT (COMPUTING WITH THE AMSTRAD) ★

LET'S SORT IT ALL OUT... (Computing with the Amstrad)Bubble quickly or shell out? (Computing with the Amstrad)
★ Ce texte vous est présenté dans sa version originale ★ 
 ★ This text is presented to you in its original version ★ 
 ★ Este texto se presenta en su versión original ★ 
 ★ Dieser Text wird in seiner Originalfassung präsentiert ★ 

SIMON MONK and SUE BRADSHAW end their series on sorting data in Basic

WE looked last month at two methods for sorting data into order. Now we'll investigate three more, and round off with some timings and guidelines for the various algorithms.

Bubble sort

This is a well-used and easy-to-implement sorting method. It is called bubble sort because the larger items are made to bubble up to the right. This can be a fast way of sorting if the data are in some form of order before you begin.

1: Set a counter i to 1.
2: If the rth element of data is greater than the i+1th element, swap them. Add one to i
3: Repeat step three until the end of the data set is reached.
4: If no elements were exchanged in previous steps, stop because the data is sorted. Otherwise go back to Step one.

An example:

10 3 8 5 9 6
* *

Swap 10 and 3 because they are out of order.

3 10 8 5 9 6
* *

Swap 10 and 8. Notice how 10 is bubbling up to the right.

3 8 10 5 9 6
* *

And so on.

3 8 5 10 9 6
* *

3 8 5 9 10 6
* *

3 8 5 9 6 10
*

Now the end of the data set has been reached. We have done some exchanging, so we are not finished. The whole process is therefore repeated.

3 8 5 9 6 10
* *

3 and 8 are in order so do not swap them.

3 8 5 9 6 10
* *

8 and 5 need swapping.

3 5 8 9 6 10
* *

3 5 8 9 6 10
* *

3 5 8 6 9 10
* *

3 5 8 6 9 10
*

The end of the data has been reached again, but two swaps have taken place so we have not finished yet.

3 5 8 6 9 10
* *

3 5 8 6 9 10
* *

3 5 8 6 9 10
* *

Swap 8 and 6.

3 5 6 8 9 10
* *

Although the data is now sorted, the algorithm has to finish this pass and complete the next in which no items will need to be exchanged.

To see the bubble sort in action merge the next program with last month's test program.

i
swapped
temp
Pointer to an element of the array.
Is true if a swap has occured.
Temporary variable.

Table I lists the variables the sort routine uses.

Quick sort

As the name implies, quick sort's best feature is its speed. It is usually written as a recursive program, that is one which works by repeatedly calling itself. Unfortunately Locomotive Basic
cannot do recursion, so some way round this has to be found. Here a list is kept of pointers to sections of the array that need to be sorted. Of the sorting algorithms we have looked at, quick sort is the most difficult to program. It works like this:

1: Divide the data set into two parts, where all the numbers in the right hand side are greater than the numbers in the left hand side. This is done by moving two pointers from each edge towards the middle, exchanging the elements under the pointers if the element under the left hand one is greater than the element under the right hand one.

2: The same treatment is now applied to each half of the data set, and so on, until the data set is so divided that every individual element is sorted.

Here's a worked example:

7 10 3 8 / 5 9 6 11

Swap 5 with 8, and 6 with 10 so all the right hand side are greater than all the left.

7 6/35/8 9/ 10 11

Swap 3 with 6 and 5 with 7. No swaps needed on the right.

5/3/6/7/8/9/10/11

Swap 3 with 5. No further swaps needed. The next program and Table II detail the appropriate subroutine.

left
right
middle
aux
temp
stack(100)
p
Left edge.
Right edge.
Middle of left and right.
Auxiliary pointer for partitioning.
Temporary variable.
Push and pop stack array.
Stack pointer.

Table II: Variables used in quick sort

Shell sort

We have already mentioned that some sorting methods work faster if the data is already partly in order. What is needed is some method of getting the smaller numbers down to the left hand side of the data set as quickly as possible. The final program, shell sort, is an attempt to do just this.

Instead of just sorting adjacent numbers, it works on numbers separated by some distance. This distance is reduced until on the final pass the distance is one. The data is then sorted properly. This method is a modification of the insertion sort.

The distance separating the items to be sorted is often derived by successively applying the formula:

Distance - 3 x Distance + 1

Where Distance starts at one. The formula has been found by trial and error, and mathematicians have struggled in vain to find why it works so well. Using it we can calculate, for instance, the distances for sorting 50 items as 40,13,4,1. Here's how the algorithm works:

1: Find the largest distance that will just fit.

2: Do insertion sort at that distance.

3: Decrease the separation to the next step down.

4: Repeat steps two and three until the data set has been sorted at distance

one. The data will now be in order. Here's an example:
7 10 3 8 5 9 6 11 2

There are only nine elements so using the sequence 40 13 4 1, four is the first distance we can use. So:

7 10 3 8 5 9 6 11 2
* * *

First 7, 5 and 2 are sorted giving:

2 10 3 8 5 9 6 11 7
* *

Now 10 and 9:

2 9 3 8 5 10 6 11 7
* *

Next 3 and 6:

2 9 3 8 5 10 6 11 7
* *

The first pass through the data is complete. The distance is now reduced to one, and the data is insertion sorted in the normal fashion. Notice how after the first pass the data is now partly sorted, allowing the insertion sort to run quickly.

i
j
distance
elem
Pointer to array
Auxiliary pointer
Sorting distance
An element of the array

Table III: Variables used in shell sort

We have used the testing program to do some timings on each of the algorithms described. The results are shown in Table IV. Although these are for Basic, the conclusions apply to implementations in any language. The timings are in seconds and those in italics are predicted rather than measured. Note that insertion sort, which is the fastest algorithm apart from quick and shell sort, would take about 33 hours to process 10,000 items compared to 20 minutes for quick sort. Some rules of thumb based on the number of items needing to be sorted can be drawn from this table:

  • Five items or less: Use bubble sort, it only takes a few lines of program, but is only slightly slower than more complicated algorithms.
  • Between 5 an 15: Preferably use selection sort as it is easy to program, but if speed is very important use insertion sort.
  • Between 15 and 25: Use insertion sort because it is quicker than any other method. It is also the simplest, excepting bubble sort.
  • Between 25 and 700: Use shell sort. It is slightly slower than quick sort but much easier to program.
  • Over 700: There is nothing to touch quick sort for speed.

These rules of thumb are only intended as a rough guide and it is worth noting that quick sort works well in almost any situation.

We hope that this introduction to data sorting has proved both interesting and painless. In the near future we'll publish a machine code sort routine which works on string arrays.

Number of ItemsSelectionInsertion Bubble Quick Shell
5
10
20
50
100
200
500
1000
2000
5000
10000
0.18
0.46
1.4
7.17
26.63
102.2
-
-
-
-
-
0.2
0.3
0.98
5.1
17.31
64.4
331.0
1230.0
6300.0
23400.0
120300.0
0.31
0.88
2.64
24.33
83.7
347.0
-
-
-
-
-
0.25
0.5
1.12
3.35
7.5
16.5
45.7
94.0
204.0
587.0
1226
0.33
0.35
1.06
4.08
8.7
22.0
66.2
146.0
349.0
929.0
2013.0

Table III: Variables used in shell sort

CWTA

★ PUBLISHER: Computing with the Amstrad
★ YEAR: 1988
★ CONFIG: 64K + AMSDOS
★ LANGUAGE:
★ LiCENCE: LISTING
★ AUTHORS: SIMON MONK and SUE BRADSHAW

★ AMSTRAD CPC ★ DOWNLOAD ★

Type-in/Listing:
» Selection  Sort    (Computing  with  the  Amstrad)    ENGLISHDATE: 2020-07-31
DL: 197
TYPE: ZIP
SiZE: 6Ko
NOTE: 40 Cyls
.HFE: Χ

★ AMSTRAD CPC ★ A voir aussi sur CPCrulez , les sujets suivants pourront vous intéresser...

Lien(s):
» Applications » Sortierte CAT-Liste (Happy Computer)
» Applications » Triclone
» Applications » Gráficos de empresa
» Applications » Sort (Schneider Aktiv)
» Applications » All sorted out (Popular Computing Weekly)
» Applications » Sort-Flo (Happy Computer)
Je participe au site:
» Vous avez des infos personnel, des fichiers que nous ne possédons pas concernent ce programme ?
» Vous avez remarqué une erreur dans ce texte ?
» Aidez-nous à améliorer cette page : en nous contactant via le forum ou par email.

CPCrulez[Content Management System] v8.7-desktop/c
Page créée en 613 millisecondes et consultée 1718 fois

L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent  du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.