Thread: is my C++ quicksort implementation correct?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    is my C++ quicksort implementation correct?

    hi can you please tell me if this is correct?

    Code:
    void quickSort(int A[], int left, int right){
        
    	int i = left, j = right, pivot = A[left];
    
    	while(i<=j){
    
    		while(i!=(right + 1) && A[i]<=pivot) i++;
    		while(A[j]>pivot) j--;
    		if(j>i){
    			swap(A[i],A[j]);
    			j--;
    			i++;
    		}
    
    	}
    	swap(A[left],A[j]);
    	if(left<j)
    	quickSort(A,left,j-1);
    	if(right>i)
    	quickSort(A,i,right);
    
    	
    }
    im not sure about this part

    Code:
     while(i!=(right + 1) && A[i]<=pivot) i++;
    i put i!=(right + 1)

    because for input

    3,9,3,8,4

    the i will check values from index = 5, 6 of the array which do not exist at all,

    but i dont need to check the same thing for j since at most the index of j will be the same of the pivot's index
    Last edited by nik; 04-05-2011 at 11:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with quicksort!
    By coni in forum C Programming
    Replies: 3
    Last Post: 10-03-2009, 02:29 AM
  2. Help needed to perform quicksort on list
    By lazyme in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2009, 02:38 AM
  3. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  4. Scalability problems with Quicksort
    By hbejkosa in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 11:26 PM
  5. Problems with Quicksort and Pointers
    By algatt in forum C Programming
    Replies: 3
    Last Post: 10-10-2007, 04:24 AM