Sorting algorithms/Selection sort: Difference between revisions

Line 699:
30 '
40 ' VAR
50 OPTIONDEFINT BASE 1A-Z
60 OPTION BASE 1
6070 I=0: J=0: IMAX = 0: ITP! = 0: TL! = 0
70 '
80 ' CONST
90 ' CONST
90100 FALSE=0: TRUE=NOT FALSE
100 '
110 CLS'
120 CLS
120130 PRINT "This program does the Selection Sort Algorithm"
130 INPUT "Number of elements to sort (Max=500, Enter=10)";IMAX
140 IFINPUT IMAX"Number =of 0elements THENto IMAXsort (Max=1000, Enter=10)";IMAX
150 IF IMAX >= 5000 THEN IMAX = 50010
160 DIMIF N(IMAX) > 1000 THEN IMAX = 1000
170 DIM N(IMAX)
170180 ' Creates and shows the unsorted list
180190 RANDOMIZE TIMER
190 FOR I=1 TO IMAX
200 FOR N(I) =1 ITO IMAX
210 NEXT N(I) = I
220 FORNEXT I=1 TO IMAX
230 FOR I=1 JTO = INT(RND*IMAX)+1
240 SWAPJ N(I),= NINT(JRND*IMAX)+1
250 NEXT SWAP N(I), N(J)
260 NEXT I
260270 PRINT: PRINT "Unsorted list:";
270 FOR I=1 TO IMAX: PRINT N(I);: NEXT I
280 PRINTFOR I=1 TO IMAX: PRINT N(I);: NEXT I
380290 PRINT: PRINT "Sorted list:";
290300 ' Sorts the list through the Selection Sort Algorithm and shows the results
300 PRINT "Sorting"; IMAX; "numbers";
310 TL! = TIMER
310 COLOR 7+16: X = POS(0): PRINT"...";: COLOR 7
300320 PRINT "Sorting"; IMAX; "numbers";
320 ITP = 0
310330 COLOR 7+16: X = POS(0): PRINT"...";: COLOR 7
330 FOR I=1 TO IMAX-1
340 ITP FOR J=I+1 TO IMAX0
190350 FOR I=1 TO IMAX-1
350 IF N(I)>N(J) THEN SWAP N(I), N(J): ITP = ITP+1
360 NEXT J, FOR J=I+1 TO IMAX
350370 IF N(I)>N(J) THEN SWAP N(I), N(J): ITP! = ITP! + 1
370 LOCATE ,X: PRINT ". Done!"
380 NEXT J, I
380 PRINT: PRINT "Sorted list:";
390 FORLOCATE I=1 TO IMAX,X: PRINT N(I);: NEXT". IDone!"
400 PRINT: PRINT "Sorted list:";
400 ' Final results
410 PRINT:FOR I=1 PRINTTO IMAX: PRINT "Numbers sortedN(I);:"; IMAXNEXT I
400420 ' Final results
420 PRINT "Total permutations done:";ITP
430 PRINT: PRINT: PRINT "Numbers sorted:"; IMAX
430 PRINT
440 PRINT "EndTotal ofpermutations programdone:";ITP!
450 PRINT "Time lapse:"; TIMER-TL!; "seconds."
450 END
430460 PRINT
470 PRINT "End of program"
450480 END
</lang>
 
58

edits