Reputation: 49
I work with GWBASIC but unable to figure out a simple way to sort (numbers) I only have 4 numbers to deal with (range 150-200) After some calculations I get these 4 numbers and I need to have them sorted in ascending order to go to the next step.Right now I do this by hand looking at the output on the screen and look for the smallest number and type it in ascending order. Lots of wasted time, never mind the strain on the eye! I been searching all over trying formulas, but unable to find one that works for me. This is what it looks like: SM1 = 121.50 SM2 = 123.65 SM3 = 117.80 SM4 = 119.50 The new order values should be: XS1 = 117.80 XS2 = 119.50 XS3 = 121.50 XS4 = 123.65
I don't need this to print or see it on the screen it should justs move over to the next stage in the program. I would be so grateful for a very simple process so I could input in my BASIC program. Thank you !
Upvotes: 1
Views: 1450
Reputation: 1006
I think you can use simple bubble sort on an array.
10 Dim a(3)
20 a(0)=121.50
30 a(1)=123.65
40 a(2)=117.80
50 a(3)=119.50
60 For L1 = 0 to 2
70 For L2 =L1+1 to 3
80 If a(L1)>a(L2) then swap a(L1), a(L2)
90 Next L2
100 Next L1
110 For n = 0 to 3
120 print a(n)
130 Next n
140 End
Just use the logic if agree
Upvotes: 1
Reputation: 39516
First solution only valid for 4 numbers.
A=SM1 : B=SM2 : C=SM3 : D=SM4
If B<A Then Swap A,B
If C<A Then Swap A,C : Swap B,C Else If C<B Then Swap B,C
If D<A Then Swap A,D : Swap B,D : Swap C,D Else If D<B Then Swap B,D : Swap C,D Else If D<C Then Swap C,D
XS1=A : XS2=B : XS3=C : XS4=D
Second solution using an array. This can easily be adapted to sort more numbers.
Dim A(3) : A(0)=SM1 : A(1)=SM2 : A(2)=SM3 : A(3)=SM4
For J%=1 To 3 : REM *** 3 is one less than the number of values
A=A(J%)
For I%=0 To J%-1
If A<A(I%) Then While I%<J% : Swap A,A(I%) : I%=I%+1 : Wend : A(I%)=A
Next
Next
XS1=A(0) : XS2=A(1) : XS3=A(2) : XS4=A(3)
Upvotes: 2