I need help in making my sorting algorithim idea work
Author |
Message |
Nyrd
|
Posted: Wed Oct 13, 2004 8:58 pm Post subject: I need help in making my sorting algorithim idea work |
|
|
I'm making a sorting algorithim that compares a randomly generated number to a group of numbers that are in ascending order, and then puts the number into an array full of 0's. I got it to organize numbers, but the problem is that it only works if a group of numbers is all there. (12234 won't work, but 52314 does) I'd apprieciate it someone could help, caus i just can't figure it out
code: |
const arraysize : int := 10000
var list : array 1 .. arraysize of int
var upnum : array 0 .. arraysize of int
var sorted : array 1 .. arraysize of int
var count1 : int := 0
var counter : int := 1
var start, stop : int
for i : 1 .. arraysize
list (i) := Rand.Int (1, arraysize)
sorted (i) := 0
put " ", list (i) ..
end for
for i : 0 .. arraysize - 1
upnum (i) := (i + 1)
% put " ", upnum (i)
end for
clock (start)
for b : 1 .. arraysize
for i : 1 .. arraysize
if upnum (0 + count1) = list (i) then
sorted (b) := list (i)
end if
end for
count1 := count1 + 1
end for
clock (stop)
put "..................."
for i : 1 .. arraysize
put " ", sorted (i) ..
end for
put " It took ", (stop - start) / 1000, " seconds to sort ", arraysize,
" numbers"
|
here's my code sofar to help you help me |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Delos
|
Posted: Thu Oct 14, 2004 5:58 pm Post subject: (No subject) |
|
|
Let me get this straight? This code is supposed to take x numbers, sort them, and return x numbers?
If that's the case then you've got quite a problem...since you're only considering one instance of each number [0..x] at most x numbers will be returned, but on average less than x numbers will be returned (due to repititions, resulting in several numbers only counting as one number) and if not all numbers in the range are given, a bunch of zeros will be returned...
Ok, if you're going to stay with your current implementation, add another array to count the occurences of each number in the range [0, x]
So for the numbers: 0, 1, 5, 7, 4, 4, 6, 2, 3
The sorted list will be: 0, 1, 2, 3, 4, 5, 6, 7
The counted list will be: 1, 1, 1, 1, 2, 1, 1, 1
And the returned list will be: 0, 1, 2, 3, 4, 4, 5, 6, 7
Get it? Got it? Good. |
|
|
|
|
|
Nyrd
|
Posted: Sun Oct 17, 2004 7:24 pm Post subject: (No subject) |
|
|
would the code for the array mentioned above be something like ..
code: |
if unpnum (b) = list (i+1)
then newarray (i) := 2
else
end if
|
|
|
|
|
|
|
|
|