Page 1 of 1

Array Sorting Routine

Posted: Thu Sep 29, 2005 1:28 am
by Pete
Another question I got in my email. Have at it!
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

Posted: Thu Sep 29, 2005 4:33 am
by m2j
It sounds a little like homework... But it also doesn't seem to make any sense...

There's no sorting nessesary, they're in order to begin with(unless I don't get what he's asking)...

matt

Posted: Thu Sep 29, 2005 7:19 am
by MystikShadows
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:

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

At the end of this it will print the sorted array in the form of

<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

Posted: Thu Sep 29, 2005 10:57 am
by Pete
I got another response:
MystikShadows is very close but its not that.

Perhaps I should explain further. I meant the column on the left sorted
according to it's value on the right so that if:

1 = 7
2 = 4
3 = 8
4 = 11
5 = 2

The output would be

4, 3, 1, 2, 5.

Posted: Thu Sep 29, 2005 11:32 am
by Quibbler

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

Posted: Thu Sep 29, 2005 11:57 am
by Pete
That's done it nicely. Thank you very much.

Greetings and respect from Scotland!

Posted: Thu Sep 29, 2005 2:18 pm
by {Nathan}
Pete wrote:
That's done it nicely. Thank you very much.

Greetings and respect from Scotland!
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.

Posted: Thu Sep 29, 2005 2:26 pm
by SebMcClouth
Nathan1993 wrote:
Pete wrote:
That's done it nicely. Thank you very much.

Greetings and respect from Scotland!
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.
A digital kilt? What's your last name? I can dig it up for ya...

Posted: Thu Sep 29, 2005 3:16 pm
by Rattrapmax6
Nathan1993 wrote:
Pete wrote:
That's done it nicely. Thank you very much.

Greetings and respect from Scotland!
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.
Yuck.... I had Mitth EMail me a watermelon awhile back,.. tasted nasty afterward,. sorta like pixels and whatnot... :barf:

:lol:

Posted: Thu Sep 29, 2005 3:26 pm
by SebMcClouth
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...

Posted: Thu Sep 29, 2005 5:06 pm
by {Nathan}
You guys DO know that I was joking, right?

Posted: Fri Sep 30, 2005 1:12 pm
by SebMcClouth
Duh!!!

Posted: Fri Sep 30, 2005 2:15 pm
by {Nathan}
SebMcClouth wrote:Duh!!!
Well, sometimes people see me as... special, so they don't treat me seriously (even when I am joking, they don't do joking serius...)...

Posted: Fri Sep 30, 2005 2:40 pm
by SebMcClouth
I was going along... but incase you change your mind, I should be able to dig your tartan up for ya!