Thread: Sorting/Dynamic Array's

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Sorting/Dynamic Array's

    Hey hey,

    So I've encountered a weird problem that I am not sure how to work around. I'm trying to calculate distances between particles in a system, store them in an array, sort it, and extract certain information from it. However, my distance array contains no zeros before the sort, yet after the first element is always 0. I don't think it's the sorting algorithm as it works fine on other arrays, but I have included it anyway.

    Code:
    void quickSort(double *arr, int elements) {
    	
    #define  MAX_LEVELS  300
    	
    	int  beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R;
    	float swap, piv;
    	
    	beg[0]=0; end[0]=elements;
    	while (i>=0) {
    		L=beg[i]; R=end[i]-1;
    		if (L<R) {
    			piv=arr[L];
    			while (L<R) {
    				while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R];
    			while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L]; }
    			arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L;
    			if (end[i]-beg[i]>end[i-1]-beg[i-1]) {
    				swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap;
    			swap=end[i]; end[i]=end[i-1]; end[i-1]=swap; }}
    		else {
    		i--; }}}
    /* End QuickSort()  */
    Here is where I think the problem is, and I feel like it has something to do with the indexing of the array but I can't seem to fix it.

    Code:
    	for(k = 0; k < 1; k++){
    		
    		SIZE = NUM_A-k-1-1;
    		rA = (double *)malloc(SIZE * sizeof(double));
    		printf("Size = %d\n", SIZE);
    
    		for (i=k+1; i < NUM_A; i++ ){
    			
    			//if (k==i){continue;}
    			
    			rx = (x[k] - x[i])- Lx*rintl((x[k] - x[i])/Lx);
    			ry = (y[k] - y[i])- Ly*rintl((y[k] - y[i])/Ly);
    			rz = (z[k] - z[i])- Lz*rintl((z[k] - z[i])/Lz);
    			
    			rA[i] = sqrt( rx*rx + ry*ry + rz*rz);
    			printf("r[%d][%d] = %lf\n", k, i, rA[i]);
    			
    		}
    		
    		quickSort(rA, SIZE);
    		printf("Sorted\n");
    		
    		for(i = 0; i < SIZE; i++){ printf("r[%d][%d] = %lf\n", k, i, rA[i]); }
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    i definitely starts at 1 for no good reason, given that all arrays start at 0.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    I have a good reason for that, however, in thinking deeper about your comment it is true that I need to do this a different way.

    I just figured it out thanks to that.

    Thanks!
    Paddon
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your sorting algorithm is correct, but some of the data types don't make sense.
    You're swapping ints using the 'swap' variable which is of type float.
    You've storing a double value from the array as a float value in the pivot 'piv'. That may cause it to sort incorrectly for values that are different as doubles, but close enough to appear equal as floats.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM