Description (for beginners):
This program demonstrates how to sort numbers in PlayBASIC.
* First, it fills an array with random numbers.
* It prints the numbers before sorting so we can see them mixed up.
* Then, it uses a simple method called insertion sorting to put the numbers in order.
* Each time a new number is added, the program checks if it should move backwards to fit in the correct place.
Finally, it prints the numbers again, showing them sorted from smallest to largest.
This program demonstrates how to sort numbers in PlayBASIC.
* First, it fills an array with random numbers.
* It prints the numbers before sorting so we can see them mixed up.
* Then, it uses a simple method called insertion sorting to put the numbers in order.
* Each time a new number is added, the program checks if it should move backwards to fit in the correct place.
Finally, it prints the numbers again, showing them sorted from smallest to largest.
PlayBASIC Code:
; --------------------------------------------------- ; Example: Insertion Sort with Before / After Output ; --------------------------------------------------- ; Create an array to hold 10 numbers Dim Numbers(10) ; Fill the array with random numbers between 0 and 100 For lp = 1 To 10 Numbers(lp) = Rnd(100) Next ; ----------------------------------------------- ; Show the list BEFORE sorting ; ----------------------------------------------- Print "Numbers BEFORE sorting:" For lp = 1 To 10 Print Numbers(lp) Next Print "---------------------------------" ; ----------------------------------------------- ; Sort the numbers using Insertion Sort ; ----------------------------------------------- For lp = 1 To 10 ; Bubble the current number backwards if needed For SearchLP = lp To 2 Step -1 ; Compare with the number before it If Numbers(SearchLP) < Numbers(SearchLP - 1) ; Swap the numbers if out of order A = Numbers(SearchLP) B = Numbers(SearchLP - 1) Numbers(SearchLP) = B Numbers(SearchLP - 1) = A EndIf Next Next ; ----------------------------------------------- ; Show the list AFTER sorting ; ----------------------------------------------- Print "Numbers AFTER sorting:" For lp = 1 To 10 Print Numbers(lp) Next ; Wait for a key before closing Sync WaitKey