Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 20th, 2003, 08:59 AM
 colombani
hum...

Join Date: Mar 2003
Location: Brazil
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
array sorting

I need a array with x numbers (1 to x) mixed. Like this:

Using 4 numbers (1 to 4), i need:

array(1) = 3
array(2) = 2
array(3) = 4
array(4) = 1

This is random. I make this:

Code:
```x = 4
randomize

array(1)=Int((x) * Rnd + 1)

array(2)=Int((x) * Rnd + 1)
while array(2) = array(1)
array(2)=Int((x) * Rnd + 1)
wend

array(3)=Int((x) * Rnd + 1)
while (array(3) = array(2)) or (array(3) = array(1))
array(3)=Int((x) * Rnd + 1)
wend

array(4)=Int((x) * Rnd + 1)
while (array(4) = array(3)) or (array(4) = array(2)) or (array(4) = array(1))
array(4)=Int((x) * Rnd + 1)
wend```

But, in this case, if x is a big number (like 100), the code will be very big too...
Any idea?

Ricardo

#2
May 20th, 2003, 11:02 AM
 imbrokn
Contributing User

Join Date: Sep 2001
Location: NJ
Posts: 428
Time spent in forums: 11 h 34 m 8 sec
Reputation Power: 16
lets see if i remember by bubble sort from my programming classes in school

PHP Code:
``` function BubbleSort(arr)   for i = 0 to uBound(arr)       for a = i to Ubound(arr)          if arr(a) < arr(i) then          'swap values          tmp = arry(i)          arr(i) = arr(a)           arr(a) = tmp        end if       next    next   BubbleSort = arr end function  ```

I just tested it and it works. if you do a search on google for bubble sort it will tell you how it works. Basicall it finds the lowest value and puts it in the 1st index position, then the second lowest and puts it in the second index position and so on. This is a pretty effective way to sort an array and pretty common.

Last edited by imbrokn : May 20th, 2003 at 11:05 AM.

#3
May 20th, 2003, 01:06 PM
 colombani
hum...

Join Date: Mar 2003
Location: Brazil
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry, I didn't express myself well.
I want a array with shuffled numbers, and not ordened numbers. The array contains numbers, 1 to x, ordered randomly (each time the order is different), like in my code (above).

Thank you

Regards,
Ricardo

 Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > array sorting