Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Quick Sort in Commodore Basic #3449

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions archive/c/commodore-basic/quick-sort.bas
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
10 DIM A(100)
5 REM
10 DIM A(99)
15 DIM SK(299): REM Need to potentially stack 3 values for each array element
20 GOSUB 2000: REM Get array
25 REM Error if invalid, not end of input/value, or less that 2 items
30 IF V = 0 OR C >= 0 OR NA < 2 THEN GOTO 200
40 GOSUB 3000: REM Perform quick sort
40 SP = -1: GOSUB 3000: REM Perform quick sort
50 GOSUB 3500: REM Show array
60 END
200 Q$ = CHR$(34): REM quote
Expand Down Expand Up @@ -65,48 +67,45 @@
2010 NA = 0
2020 GOSUB 1000: REM Read input value
2030 IF V = 0 THEN RETURN: REM invalid
2040 NA = NA + 1
2050 A(NA) = NR
2040 A(NA) = NR
2050 NA = NA + 1
2060 IF C < 0 THEN RETURN: REM end of input or value
2070 IF C = 44 THEN GOTO 2020: REM comma, get next value
2080 V = 0
2090 RETURN
3000 REM Quick sort
3001 REM Commodore Basic does not really support recursion because
3002 REM everything is a global variable. However, recursion can be simulated
3001 REM Commodore Basic does not really support recursion because everything
3002 REM is a global variable. However, recursion can be simulated with
3003 REM a "stack". This "stack" is just an array, SK, and a stack index, SP.
3004 REM Source:
3005 REM https://en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme
3006 REM Inputs:
3007 REM - A contains array to sort
3008 REM - NA contains size of array
3009 REM Outputs: A contains sorted array
3010 REM Need to potentially stack 3 values for each array element
3020 DIM SK(NA * 3)
3030 SP = 0
3040 LO = 1
3050 HI = NA
3060 REM Recursive portion of algorithm
3061 REM Inputs:
3062 REM - A contains array to sort
3063 REM - LO contains low index
3064 REM - HI contains high index
3065 REM Outputs: A contains partially sorted array
3070 IF LO >= HI OR LO < 1 THEN RETURN
3080 GOSUB 3300: REM Parition array and get pivot index (P)
3090 SP = SP + 1: SK(SP) = LO: REM Push LO
3100 SP = SP + 1: SK(SP) = HI: REM Push HI
3110 SP = SP + 1: SK(SP) = P: REM Push P
3120 HI = P - 1: GOSUB 3060: REM Sort left side of partition
3130 P = SK(SP): SP = SP - 1: REM Pop P
3140 HI = SK(SP): SP = SP - 1: REM Pop HI
3150 SP = SP + 1: SK(SP) = HI: REM Push HI
3160 SP = SP + 1: SK(SP) = P: REM Push P
3170 LO = P + 1: GOSUB 3060: REM Sort right side of partition
3180 SP = SP - 1: REM Pop P (don't store)
3190 HI = SK(SP): SP = SP - 1: REM Pop HI
3200 LO = SK(SP): SP = SP - 1: REM Pop LO
3210 RETURN
3010 LO = 0
3020 HI = NA - 1
3030 REM Recursive portion of algorithm
3031 REM Inputs:
3032 REM - A contains array to sort
3033 REM - LO contains low index
3034 REM - HI contains high index
3035 REM Outputs: A contains partially sorted array
3040 IF LO >= HI OR LO < 0 THEN RETURN
3050 GOSUB 3300: REM Parition array and get pivot index (P)
3060 SP = SP + 1: SK(SP) = LO: REM Push LO
3070 SP = SP + 1: SK(SP) = HI: REM Push HI
3080 SP = SP + 1: SK(SP) = P: REM Push P
3090 HI = P - 1: GOSUB 3030: REM Sort left side of partition
3100 P = SK(SP): SP = SP - 1: REM Pop P
3110 HI = SK(SP): SP = SP - 1: REM Pop HI
3120 SP = SP + 1: SK(SP) = HI: REM Push HI
3130 SP = SP + 1: SK(SP) = P: REM Push P
3140 LO = P + 1: GOSUB 3030: REM Sort right side of partition
3150 SP = SP - 1: REM Pop P (don't store)
3160 HI = SK(SP): SP = SP - 1: REM Pop HI
3170 LO = SK(SP): SP = SP - 1: REM Pop LO
3180 RETURN
3300 REM Partition array
3301 REM Inputs:
3302 REM - A contains array to partition
Expand Down Expand Up @@ -135,11 +134,11 @@
3501 REM A contains array
3502 REM NA contains size of array
3510 IF NA < 1 THEN GOTO 3590
3520 FOR I = 1 TO NA
3520 FOR I = 0 TO NA - 1
3530 S$ = STR$(A(I))
3540 IF A(I) >= 0 THEN S$ = MID$(S$, 2): REM strip leading space
3550 PRINT S$;
3560 IF I < NA THEN PRINT ", ";
3560 IF I < (NA - 1) THEN PRINT ", ";
3570 NEXT I
3580 PRINT
3590 RETURN