 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
I've been trying to do a simple sort, but it doesn't seem to be working as I
remember. What am I doing wrong?
#declare R1 = seed(321321);
#declare Spread = array[6]
/// populate
#declare I = 0;
#while ( I < 5 )
#declare Spread[I] = rand(R1);
#declare I = I+1;
#end
/// verify
#declare I = 0;
#while ( I < 5 )
#debug concat(str(Spread[I],2,2), "\n")
#declare I = I+1;
#end
#debug "\n"
/// sort
#declare I = 0;
#while ( I < 5 )
#declare First = Spread[I];
#declare P = I+1;
#while (P < 5)
#declare Second = Spread[P];
#if ( Second < First )
#declare Storage = First;
#declare First = Second;
#declare Second = Storage;
#end
#declare P = P+1;
#end
#declare I = I+1;
#end
/// re-display
#declare I = 0;
#while ( I < 5 )
#debug concat(str(Spread[I],2,2), "\n")
#declare I = I+1;
#end
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On Mon, 1 Jan 2001 17:46:47 -0500, "Bill Dewitt" <bde### [at] cfl rr com> wrote:
>I've been trying to do a simple sort, but it doesn't seem to be working as I
>remember. What am I doing wrong?
>
Not putting in comments to tell us what is going on!
----------------------
dav### [at] hamiltonite com
http://hamiltonite.com/
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Bill Dewitt <bde### [at] cfl rr com> wrote:
> I've been trying to do a simple sort, but it doesn't seem to be working as
I
> remember. What am I doing wrong?
[code snipped]
The mistake comes in trying to use the First and Second variables as if they
were pointers (i.e. your code assumes modifying these two variables will
modify the original array members you declared them to). If you change the
code not to use these variables:
#declare I = 0; #while ( I < 5 )
#declare P = I+1; #while (P < 5)
#if (Spread[P] < Spread[I])
#declare Storage = Spread[I];
#declare Spread[I] = Spread[P];
#declare Spread[P] = Storage;
#end
#declare P = P+1; #end
#declare I = I+1; #end
you should find it works as expected.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"Chris Colefax" <chr### [at] tag povray org> wrote :
>
> (i.e. your code assumes modifying these two variables will
> modify the original array members you declared them to).
< Slaps head > Thanks...
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
By the way, that's one of the slowest way of sorting an array...
--
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Warp wrote:
> By the way, that's one of the slowest way of sorting an array...
But bubble sorting is the easiest for a lot of people to understand and
write :)
My brother gave me a big lecture about different array sorting methods, like
binary search tree etc...
How easy is it to implement something like that with POV-Ray?
--
Lance.
http://come.to/the.zone
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On Mon, 1 Jan 2001 18:25:17 -0500, Bill Dewitt wrote:
>
>"Chris Colefax" <chr### [at] tag povray org> wrote :
>>
>> (i.e. your code assumes modifying these two variables will
>> modify the original array members you declared them to).
>
>< Slaps head > Thanks...
A slap head with false teeth! Don't sound much like a Viking to me.
--
Cheers
Steve email mailto:ste### [at] zeropps uklinux net
%HAV-A-NICEDAY Error not enough coffee 0 pps.
web http://www.zeropps.uklinux.net/
or http://start.at/zero-pps
11:05am up 14 days, 22:27, 3 users, load average: 1.39, 1.22, 1.10
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
"Warp" <war### [at] tag povray org> wrote in message
news:3a51b517@news.povray.org...
> By the way, that's one of the slowest way of sorting an array...
Well... being as how I am only sorting 6 things....
But if you would like to publish an array sorting macro, I will use it
with great happiness and thanks.
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Lance Birch <-> wrote:
: But bubble sorting is the easiest for a lot of people to understand and
: write :)
Actually insertion sort is a lot more intuitive than bubble sort.
If you ask someone who doesn't know anything about sorting to make a
sorting algorithm, he will most probably make an insertion sort (or a very
close variant), not a bubble sort.
Bubble sort is just awful. It's slow even for an O(n^2) algorithm.
Insertion sort is also an O(n^2) algorithm (that is, very slow), but quite
faster than bubble sort.
O(n^2) algorithms are hopelessly slow when there are even a slight amount
of items (like 1000 or so). If one wants a good sorting algorithm, it's
necessary to use an O(n*log n) algorithm. Those beat O(n^2) algorithms 10-0
with large amount of items.
A merge sort might be the easiest to implement in povray.
--
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
OK, I can see how an insertion algorithm is just as easy to make (and why
it's more intuitive).
But then again there's no real need to have it super fast anyway if it's
going to be just on the parse phase...
I understand all the stuff about O(n^2) and O(n*log n) too, he talked for a
fair while, hehe, actually I'm amazed I remember most of it... I wasn't
really paying attention at the time... hehe
--
Lance.
http://come.to/the.zone
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |