Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

sort.c

Go to the documentation of this file.
00001 #define NRANSI
00002 #include "nrutil.h"
00003 #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
00004 #define M 7
00005 #define NSTACK 50
00006 
00007 void sort(unsigned long n, float arr[])
00008 {
00009         unsigned long i,ir=n,j,k,l=1;
00010         int jstack=0,*istack;
00011         float a,temp;
00012 
00013         istack=ivector(1,NSTACK);
00014         for (;;) {
00015                 if (ir-l < M) {
00016                         for (j=l+1;j<=ir;j++) {
00017                                 a=arr[j];
00018                                 for (i=j-1;i>=1;i--) {
00019                                         if (arr[i] <= a) break;
00020                                         arr[i+1]=arr[i];
00021                                 }
00022                                 arr[i+1]=a;
00023                         }
00024                         if (jstack == 0) break;
00025                         ir=istack[jstack--];
00026                         l=istack[jstack--];
00027                 } else {
00028                         k=(l+ir) >> 1;
00029                         SWAP(arr[k],arr[l+1])
00030                         if (arr[l+1] > arr[ir]) {
00031                                 SWAP(arr[l+1],arr[ir])
00032                         }
00033                         if (arr[l] > arr[ir]) {
00034                                 SWAP(arr[l],arr[ir])
00035                         }
00036                         if (arr[l+1] > arr[l]) {
00037                                 SWAP(arr[l+1],arr[l])
00038                         }
00039                         i=l+1;
00040                         j=ir;
00041                         a=arr[l];
00042                         for (;;) {
00043                                 do i++; while (arr[i] < a);
00044                                 do j--; while (arr[j] > a);
00045                                 if (j < i) break;
00046                                 SWAP(arr[i],arr[j]);
00047                         }
00048                         arr[l]=arr[j];
00049                         arr[j]=a;
00050                         jstack += 2;
00051                         if (jstack > NSTACK) nrerror("NSTACK too small in sort.");
00052                         if (ir-i+1 >= j-l) {
00053                                 istack[jstack]=ir;
00054                                 istack[jstack-1]=i;
00055                                 ir=j-1;
00056                         } else {
00057                                 istack[jstack]=j-1;
00058                                 istack[jstack-1]=l;
00059                                 l=i;
00060                         }
00061                 }
00062         }
00063         free_ivector(istack,1,NSTACK);
00064 }
00065 #undef M
00066 #undef NSTACK
00067 #undef SWAP
00068 #undef NRANSI

Generated at Sun Feb 24 09:57:16 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001