Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort Variables in Descending Order
#1
Here's my code so far. I'm making a high score program for my girlfriend for InkLink (She's obsessed). I have run into this problem before in other programs and I can't beat it.

I have input all the high scores that will be saved to a .txt file, but then I need to organize those top scores in descending order. That's where the problem starts. How can I take 10 or so string variables and order them in descending order without making 500 if statements? I could go on forever if D$(1) > D$(2) and D$(1) > D$(3), etc. But I don't even know if that could work, and there has to be an easier way. I tried to read them in using the Data command, but I still can't get them in order.

Any help is appreciated. Please use the simplest code possible. I don't know all the advanced commands.

CLS

COLOR 11

PRINT ""
PRINT " Amie's InkLink top score program. Version 1.0"
PRINT ""
PRINT ""
PRINT ""



PRINT " To ADD a top score press the 'a' key at the prompt."
PRINT ""
PRINT " To see your TOP scores press the 't' key at the prompt."
PRINT ""
INPUT B$

SELECT CASE B$
CASE a

PRINT ""
PRINT " Great job Amie! Type your high score at the prompt."
PRINT ""
INPUT C$
PRINT ""
PRINT " Your new high score is"; C$
PRINT ""

OPEN "InkLinks.txt" FOR INPUT AS #1

DIM D$(1 TO 10)

FOR E = 1 TO 10
INPUT #1, D$(E)
NEXT

'Here's where I need to sort which string is the highest. I'm lost as to
'how to do it.

'Here's how I did it:

FOR E = 1 TO 10
FOR F = 1 TO 10
IF D$(E) > D$(F) THEN
SWAP D$(E), D$(F)
ELSE
END IF
NEXT
NEXT

FOR E = 1 TO 10
PRINT D$(E)
NEXT

CASE t

end

Below is the contents of the InkLinks.txt file:
=============================
100
500
600
200
700
900
300
150
000
000
Reply
#2
ok, what you wanna do is sort stuff. Most people use bubble sort of algos for that, since its very intuitive. Here are some of the types of sorting algos:

1. Bubble sort
2. Heap sort
3. Insertion sort
4. Merge sort
5. Quick sort
6. Selection sort
7. Shell sort

All algos vary in complexity. But for your need selection sort would be sufficient. It's fairly efficient Wink

Lets suppose you have an array containing 5 elements which need to be sorted in descending order.

1. 5
2. 7
3. 3
4. 9
5. 8

Heres are the steps:
1. Take the first element and compare to the rest of the array finding the maximum of them.
2. When maximum is found, swap it with the first element.
3. Take the second element and compare with the rest of the array, again finding the largest of them and swapping with the seconds element.
4. Continue doing this until you reach the n-1 element Wink.

Heres the code:
[syntax="qbasic"]CLS
DIM arr%(1 TO 10)
DIM temp%, max%
max% = 1

arr%(1) = 5
arr%(2) = 7
arr%(3) = 3
arr%(4) = 9
arr%(5) = 8
arr%(6) = 0
arr%(7) = 4
arr%(8) = 1
arr%(9) = 2
arr%(10) = 6


FOR i% = 1 TO 9 'Always 1 less than the total no. of elements in the array Wink
FOR j% = i% TO 10
IF arr%(j%) > arr%(max%) THEN max% = j%
NEXT

PRINT "Pass "; i%
FOR ai% = 1 TO 10
PRINT arr%(ai%)
NEXT

SWAP arr%(max%), arr%(i%)
SLEEP
NEXT

PRINT "Sorted array is:"
FOR i% = 1 TO 10
PRINT arr%(i%)
NEXT[/syntax]
Reply
#3
Thanks, i'll digest that.
Reply
#4
I decided instead to create my own and it actually works!! I discovered the SWAP command today. Yay!

I don't understand why it works, but i'll get it to output to a .txt file and figure out why.

Thanks for the help. After reading your post about names for types of sorting, I started looking up online different ways of sorting in QBasic, but none were easy and all hard to understand. I'm glad I found my own. At least I understand what's going on..,.. somewhat.

FOR E = 1 TO 10
FOR F = 1 TO 10
IF D$(E) > D$(F) THEN
SWAP D$(E), D$(F)
ELSE
END IF
NEXT
NEXT

FOR E = 1 TO 10
PRINT D$(E)
NEXT
Reply
#5
that my friend is called bubble sort Wink. how exactly did you discover it?
Reply
#6
I think selection sort is better to understand than bubble sort, but ok Wink

Btw, there is a SORTDEMO.BAS file in the QB root folder, which came with the QB distribution package. If you don't have it, try downloading QB45 from QBNZ Wink
Reply
#7
Quote:I think selection sort is better to understand than bubble sort, but ok Wink

Btw, there is a SORTDEMO.BAS file in the QB root folder, which came with the QB distribution package. If you don't have it, try downloading QB45 from QBNZ Wink

Yeah I got PDS 7.1, not QB4.5
Reply
#8
Quote:that my friend is called bubble sort Wink. how exactly did you discover it?

Umm... I just wanted to compare each variable with the other... so to do that I needed a for-next loop inside a for next loop. Then I saw on another website that you could swap 2 variables with the swap command like so:

a = 1000
b = 500

if a > b then
swap a, b
else
end if

Then I just crossed my fingers and hoped for the best as that went through the loop. And it worked!

I read that "Bubble Sort" can be really slow if working with lots of variables. I didn't realize this was "Bubble Sort."

Thanks for the help!

Here was the final code:

The girlfriend likes the program. Smile
Only problem is, that I want to use the Inkey$ command so she doesn't have to hit enter after typing her choice. I don't know how to do it yet. I'll figure it out.
===============================================


CLS

COLOR 6

PRINT ""
PRINT ""
PRINT " Amie's InkLink Top Score Program. Version 1.0"
PRINT ""

start:

COLOR 10

PRINT ""
PRINT " Type 'A' then ENTER to ADD a top score."
PRINT ""
PRINT " Type 'T' then ENTER to see TOP scores"
PRINT ""
PRINT " Type 'Q' then Enter to QUIT."
PRINT ""
INPUT B$

SELECT CASE B$
CASE "a", "A"

PRINT ""
PRINT " Great job Ames! Type your high score at the prompt and hit ENTER."
PRINT ""
INPUT C
PRINT ""
CLS
PRINT ""
COLOR 12
PRINT " Your new high score of "; C; " has been recorded."

OPEN "InkLinkS.txt" FOR INPUT AS #1
REDIM D(1 TO 10)
FOR E = 1 TO 10
INPUT #1, D(E)
NEXT
CLOSE #1

FOR E = 1 TO 10
FOR F = 1 TO 10
IF D(E) > D(F) THEN
SWAP D(E), D(F)
ELSE
END IF
NEXT
NEXT

FOR E = 1 TO 10
IF C > D(E) THEN
SWAP C, D(E)
ELSE
END IF
NEXT

OPEN "InkLinks.txt" FOR OUTPUT AS #2
FOR E = 1 TO 10
PRINT #2, D(E)
NEXT
CLOSE #2

GOTO start


CASE "t", "T"

CLS
OPEN "InkLinkS.txt" FOR INPUT AS #1
REDIM D(1 TO 10)
FOR E = 1 TO 10
INPUT #1, D(E)
NEXT
CLOSE #1

COLOR 13
PRINT ""
PRINT " Top Scores:"
PRINT ""

FOR E = 1 TO 10
IF E = 10 THEN
COLOR 13
PRINT " "; E; "=";
COLOR 11
PRINT D(E)
ELSE
COLOR 13
PRINT " "; E; " =";
COLOR 11
PRINT D(E)
END IF
NEXT

GOTO start

CASE "q", "Q"

COLOR 6
PRINT ""
PRINT " Thanks for using another fine TrevorSoft program."
PRINT ""
SYSTEM

CASE ELSE

CLS
GOTO start

SYSTEM

END SELECT
Reply
#9
Quote: Only problem is, that I want to use the Inkey$ command so she doesn't have to hit enter after typing her choice. I don't know how to do it yet. I'll figure it out.
I hope you won't mind when I post this small block of code for you... Wink
[syntax="QBasic"]DO
K$ = LCASE$(INKEY$)
LOOP UNTIL K$ = "q" OR K$ = "t" OR K$ = "a"
'check the value of k$ here[/syntax]
Reply
#10
Quote:
Licentia Wrote:Only problem is, that I want to use the Inkey$ command so she doesn't have to hit enter after typing her choice. I don't know how to do it yet. I'll figure it out.
I hope you won't mind when I post this small block of code for you... Wink
[syntax="QBasic"]DO
K$ = LCASE$(INKEY$)
LOOP UNTIL K$ = "q" OR K$ = "t" OR K$ = "a"
'check the value of k$ here[/syntax]

Not at all, thanks! :bounce:
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)