I'm having problems writing a program in Qbasic that will perform this
particular sort...
Define an array like this:
1 = 2
2 = 1
3 = 3
...then sort and display them so that only the original positions of the numbers are visible...
2, 1, 3
Please help
Array Sorting Routine
Array Sorting Routine
Another question I got in my email. Have at it!
-
- Veteran
- Posts: 703
- Joined: Sun Nov 14, 2004 7:36 am
- Contact:
He wants to know where the number was when it was unsorted from what I'm understanding .
To what I know, you need a copy of the original unsorted array to search them afterwards as to where they used to be before it was sorted...here's one way that works:
At the end of this it will print the sorted array in the form of
<SortedNumber> -> <PositionInUnsorted>
like so:
To what I know, you need a copy of the original unsorted array to search them afterwards as to where they used to be before it was sorted...here's one way that works:
Code: Select all
DIM Unsorted!(1 TO 5)
DIM Sorted!(1 TO 5)
Unsorted!(1) = 4
Unsorted!(2) = 1
Unsorted!(3) = 10
Unsorted!(4) = 2
Unsorted!(5) = 7
' COPY ARRAY
Sorted!(1) = Unsorted!(1)
Sorted!(2) = Unsorted!(2)
Sorted!(3) = Unsorted!(3)
Sorted!(4) = Unsorted!(4)
Sorted!(5) = Unsorted!(5)
' DO THE SORTING
DO
OutOfOrder = False% 'assume it's sorted
FOR X = 1 TO UBOUND(Sorted!) - 1
IF Sorted(X) > Sorted!(X + 1) THEN
SWAP Sorted!(X), Sorted!(X + 1) 'if we had to swap
OutOfOrder = True% 'we may not be done
END IF
NEXT
LOOP WHILE OutOfOrder
CLS
PRINT "ORIGINAL UNSORTED VALUES"
PRINT "------------------------"
FOR Counter = 1 TO 5
PRINT Unsorted(Counter)
NEXT Counter
PRINT "SORTED ARRAY WITH POSITION IN UNSORTED ARRAY"
PRINT "--------------------------------------------"
FOR Outer = 1 TO 5
FOR Inner = 1 TO 5
IF Unsorted!(Inner) = Sorted!(Outer) THEN
PRINT Sorted!(Outer); "->"; Inner
EXIT FOR
END IF
NEXT Inner
NEXT Outer
<SortedNumber> -> <PositionInUnsorted>
like so:
Code: Select all
ORIGINAL UNSORTED VALUES
------------------------
4
1
10
2
7
SORTED ARRAY WITH POSITION IN UNSORTED ARRAY
--------------------------------------------
1 -> 2
4 -> 1
2 -> 4
7 -> 5
10 -> 3
When God created light, so too was born, the first Shadow!
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Code: Select all
DATA 7,4,8,11,2
PRINT
FOR i = 1 TO 5
READ x(i)
NEXT i
FOR k = 1 TO 5
imax = 1: xmax = x(1)
FOR i = 2 TO 5
IF x(i) > xmax THEN imax = i: xmax = x(i)
NEXT i
x(imax) = -99
PRINT imax; " ";
NEXT k
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
A digital kilt? What's your last name? I can dig it up for ya...Nathan1993 wrote:scotland? I have ancestors from scotland... I want a kilt so bad. Will you mail me one? wait... thats old fashioned, email it to me.Pete wrote:That's done it nicely. Thank you very much.
Greetings and respect from Scotland!
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
-
- Veteran
- Posts: 1055
- Joined: Sun Jan 02, 2005 2:11 pm
- Location: At my computer
- Contact:
Yuck.... I had Mitth EMail me a watermelon awhile back,.. tasted nasty afterward,. sorta like pixels and whatnot... :barf:Nathan1993 wrote:scotland? I have ancestors from scotland... I want a kilt so bad. Will you mail me one? wait... thats old fashioned, email it to me.Pete wrote:That's done it nicely. Thank you very much.
Greetings and respect from Scotland!
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
(x.t.r.GRAPHICS)
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
A kilt is not to eat... you have to wear it... just beware it doesn't get wet... Cause you'll be getting a wettie...
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
I was going along... but incase you change your mind, I should be able to dig your tartan up for ya!
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.