POV-Ray : Newsgroups : povray.general : Sort : Re: Sort Server Time
8 Aug 2024 18:12:54 EDT (-0400)
  Re: Sort  
From: Micha Riser
Date: 2 Jan 2001 14:43:37
Message: <3A52302F.EDC9DB8@datacomm.ch>
Here is an implementation of the quicksort algorithm in pov. It uses
recurisve macro calls. 

It sorted 10000 numbers in less than a minute. With megapov it goes
about twice as fast.

Hope it is of some use.
- Micha Riser


-------------- begin code ------------------------------

#declare a=array[10000]

////////////////////////////////////////////////////////
#macro Quicksort(L,R)

  #local i=L;
  #local j=R;
  #local c=a[int((L+R)/2)];

  #while(i<j)
    #while(a[i]<c) #local i=i+1; #end
    #while(a[j]>c) #local j=j-1; #end
    #if (i<j)
      #local tmp=a[i];
      #declare a[i]=a[j];
      #declare a[j]=tmp;
      #local i=i+1;
      #local j=j-1;  
    #end  
  #end

  #if (i=j)  #local i=i+1;  #local j=j-1; #end
  #if (L<j) Quicksort(L,j) #end
  #if (i<R) Quicksort(i,R) #end

#end
/////////////////////////////////////////////////////////////


#declare my_seed=seed(0);

#local i=0;
#while(i<10000)
#declare a[i]=rand(my_seed)*10000;
#local i=i+1;
#end

#debug "starting quick sort\n"
Quicksort(0,9999)
#debug "finished.\n"


#local i=0;
#while(i<10000)
#debug str(a[i],3,3)
#debug "\n"
#local i=i+1;
#end


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.