Array Sorting Routine

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!
Post Reply
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Array Sorting Routine

Post 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
m2j

Post 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
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post 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
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
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Post 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.
Quibbler

Post 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
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Post by Pete »

That's done it nicely. Thank you very much.

Greetings and respect from Scotland!
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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.
Image
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post 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...
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.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post 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:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post 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...
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.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

You guys DO know that I was joking, right?
Image
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth »

Duh!!!
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.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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...)...
Image
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth »

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.
Post Reply