Thread: Quick Sorting ?!?!?

  1. #1
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104

    Unhappy Quick Sorting ?!?!?

    Hi!
    I am really in trouble with this code
    I am not sure where is the mistake. Waiting for some good help

    Code:
    #include<iostream.h>
    
    
    template<class T> void quickSort(T* a, int lo, int hi)
    {
    	if (lo >= hi)
    		return;
    	T pivot = a[hi];
    
    	int i = lo - 1;
    	int j = hi;
    
      while (i < j)
      {
    		while (a[++i] < pivot) ;
    		while (j >= 0 && a[--j] > pivot) ;
    
    		if (i < j)
    			swap(a[i], a[j]);
      }
    
      swap(a[i], a[hi]);
    
      quicksort(a, lo, i-1);
      quicksort(a, i+1, hi);
    }
    
    template<class T> void sort(T* a, int n)
    {
    	quicksort(a, 0, n-1);
    }
    
    
    int main()
    {
    	const int n = 7;
    
    	int ary[n] = {9, 5, 1, 8, 3, 4, 6};
    
    	cout << " Before soring : ";
    
    	for(int i=0;i<n;++i)
    		cout << ary[i] << '\t';
    
    	sort(ary,n);
    
    	cout << "\n After sorting : ";
    
    	for(int i=0;i<n;++i)
    		cout << ary[i] << '\t';
    
    	return 0;
    }
    Last edited by Moni; 03-07-2003 at 07:52 PM.
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    #include<iostream.h>
    
    template<class T> void quickSort(T* a, int lo, int hi)
    {
    	if (lo >= hi)
    		return;
    	T pivot = a[hi];
    
    	int i = lo - 1;
    	int j = hi;
    
      while (i < j)
      {
    		while (a[++i] < pivot) ;
    		while (j >= 0 && a[--j] > pivot) ;
    
    		if (i < j)
    			swap(&a[i],&a[j]);
      }
    
      swap(&a[i], &a[hi]);
    
      quickSort(a, lo, i-1);
      quickSort(a, i+1, hi);
    }
    
    template<class T> void sort(T* a, int n)
    {
    	quickSort(a, 0, n-1);
    }
    
    template<class T> void swap(T *a, T *n)
    {
    	T temp = *a;
    	*a = *n;
    	*n = temp;
    }
    
    int main()
    {
    	const int n = 7;
    	int i;
    
    	int ary[n] = {9, 5, 1, 8, 3, 4, 6};
    
    	cout << " Before soring : ";
    
    	for( i = 0;i<n;++i)
    		cout << ary[i] << '\t';
    
    	sort(ary,n);
    
    	cout << "\n After sorting : ";
    
    	for( i=0;i<n;++i)
    		cout << ary[i] << '\t';
    
    	return 0;
    }
    If you really couldn't find the errors yourself, then you shouldn't be dealing with templates at this stage.
    Last edited by The Dog; 03-08-2003 at 02:20 AM.

  3. #3
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Sorry! I thought It would work!
    Code:
    #include <algorithm>
    
    template <class T>
       void swap (T& a, T& b);
    But I didn't included it!!!
    Now will it be ok?
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  4. #4
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    How can I make many of my functions within TEMPLATEs ?
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. Quick question on sorting a singly linked list
    By Ham in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 11:26 PM