Radio Shack TRS-80 PC-3 Owner's Manual page 189

Table of Contents

Advertisement

Sort writing
When writing programs, you often need to get items into a particular order, i.e., to sort them. Many different sorting techniques
have been developed, each of which is better or worse for particular circumstances. One of the simplest sorting techniques is the
"sort by search". In this technique the program scans an array of unsorted data looking for the largest item. It puts this in the top
element of a new array and goes back to look for the next largest item. It puts this item in the next element of the array, and so
on. Each element selected in the unsorted array is then set to a very small number so it won't be found on the next search.
Sorting Program
10: INPUT "HOW MANY_ITEMS TO SORT? "; N
20: DIM O(N). S(N)
30: FOR 1= 1 TO N
40: INPUT 0
(I)
50: NEXT I
60: PAUSE "SORTING"
70: FOR 1= 1 TO N
80: T
=
1
90: FOR J
=
1 TO N
100: IF (0 (J)
>
0 (T)) THEN LET T
=
J
110:NEXTJ
120: S
(I)
= 0 (T)
130: 0 (T)
= -
9.9999999991E99
140: NEXT I
Find out how many values this time
Allocate space for an array to hold the data-O ( ) is for unsorted data, S ( )
for sorted
Lines 30-50 are a loop to read in the data
Read in each value
Announce that the sort is starting
Outside loop indexes through S ( ) indicating where the next largest
value is to be put
Arbitrarily set pointer for the largest value to the first element in 0 (
Loop th rough array of unsorted data
If a larger value is found, change T to point
to
the largest so far
Put this value in the next element of the sorted array
Set that element in the unsorted array to the smallest possible number so
that it won't be used again
End of loop on sorted array
189

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents