Thread: Quick sort

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Quick sort

    I'm trying to write my own quick sort, and its not wanting to work, what wrong?


    Code:
    void quick_sort(int *a, int size, bool (*cmp) (int, int))
    {
    	int p = partition(a,size,cmp);
    	int lhs=(p+1);
    	int rhs= (size-(p+1));
    	if(lhs>1)
    		quick_sort(a, lhs, cmp);
    	if(rhs>1)
    		quick_sort(a+(p+1), rhs, cmp);
    }
    
    int partition(int* a, int size, bool (*cmp) (int, int))
    {
    	int p=0, oe=(size-1), temp=0;
    	while(p!=oe)
    	{
    		while(p<oe)
    		{
    			if(cmp(a[p],a[oe]))
    			{
    				temp=a[p];
    				a[p]=a[oe];
    				a[oe]=temp;
    				temp=p;
    				p=oe;
    				oe=temp;
    				oe++;
    			}
    			else
    			{
    				oe--;
    			}
    		}
    
    		
    		while(p>oe)
    		{
    			if(cmp(a[oe],a[p]))
    			{
    				temp=a[p];
    				a[p]=a[oe];
    				a[oe]=temp;
    				temp=p;
    				p=oe;
    				oe=temp;
    				oe++;
    			}
    			else
    			{
    				oe++;
    			}
    		}
    	}
    	return p;
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    does the program compile? can you show us the output if it does compile? if there's no output, how do you know that it's messing up?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    heres the source for the whole program.

    Main.cpp is the main file, its writen in c++, but the function could be either.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    you should know that it's against the rules to cross-post. keep this thread to one forum, and delete the other one.

    after sucessfully compiling your program, when running, it shows a segmentation fault at the quick sort. the error is definately in partition(). i know this because of gdb. a seg fault usually results from an illegal access of memory. you could be dealing with uninitialized pointers. (or it could just be a typo.) i'll let you know if i find anything more that could help.
    Last edited by ygfperson; 07-30-2002 at 10:15 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Originally posted by ygfperson
    you should know that it's against the rules to cross-post. keep this thread to one forum, and delete the other one.
    .

    done and done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Sort
    By chriscolden in forum C Programming
    Replies: 3
    Last Post: 06-07-2006, 06:44 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. Help On A Quick Sort Program
    By nick4 in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2004, 10:51 AM
  4. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM