Thread: help me with this programme

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    help me with this programme

    i write this programme to find the Kth number in the vector a
    the algorithms just like the quick sort

    but it doesnot work right

    Code:
    void quickSelect(vector<int> & a, int left=0, int right=a.size(), int k) {
    
    	int middle = (left + right) / 2;
    	Comparable pivot = a[middle];
    	int i = left, j = right;
    	for (;;) {
    		while (a[i] < pivot) {
    			i = i + 1;
    		}
    		while (a[j] > pivot) {
    			j = j - 1;
    		}
    
    		if (i < j) {
    			swap(a[i], a[j]);
    			i = i + 1;
    			j = j - 1;
    		} else
    			break;
    	}
    	a[i+1] = pivot;
    
    	if (k <= i)
    		quickSelect(a, left, i - 1, k);
    	else if (k > i + 1)
    		quickSelect(a, i + 1, right, k - i);
    	else
    		cout << "the " << k << "th number is " << pivot << endl;
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    1. Write a more descriptive subject next time (your subject fits pretty much all threads here)
    2. Need more detail. What do you mean it doesn't work? doesn't compile? (post compiler erros) doesn't run? (what happens) doesn't give you the result you expect? (provide the input, output, and expected output)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a programme with cin.get
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 02:35 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM
  5. Simple C++ GUI programme does'nt run from dos
    By Shadowhunt in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:30 AM