Selection algorithm giving ridiculous output

I'm trying to learn C++, and in the course of trying to program an algorithm to grab the ith element in an array, I started getting some stupid outputs, like 7844221 from an array with only numbers between 1 and 10,000 in it. By sticking in a few couts, I'm pretty confident that the algorithms are ok, but something is wrong, and I just don't know what.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int partition(int *A, int l, int r)
{
     int i, j, x, p;
     p = A[l];
     i = l + 1;
     for(j=l+1;j<=r;j++)
                       {
                             if(A[j]<p)
                                 {
                                       x = A[j];
                                       A[j] = A[i];
                                       A[i] = x;
                                       i++;
                                 }
                       }
     x = A[i-1];
     A[i-1] = A[l];
     A[l] = x;
     cout<<i-1<<" "<<A[i-1]<<endl;
     return i-1;
}

int RSelect(int *A, int l, int r, int i)
{
     int p;
     cout<<i<<" ";
     if(l-r==0)
        {
               cout<<"wibble";
            return A[l];
        }
     p = partition(A, l, r);
     if(p==i)
            {
                       cout<<"plibble"<<A[p]<<endl;
            return A[p];
            }
     else if(p>i)
          RSelect(A, l, p, i);
     else if(p<i)
          RSelect(A, p+1, r, i);
}

int main(int argc, char *argv[])
{
    int i = 0, j, A[10000], k, p;
    for(k=0;k<10000;k++)
            A[k]=0;
    fstream input;
    input.open ("QuickSort.txt", fstream::in | fstream::out);    
    if (input.is_open())
    {
    while(input.good())
    {
                       input >> j;
                       A[i] = j;
                       i++;
    }
    }
    else
        cout << "Error opening file\n";
    input.close();
    cout <<i<<" Which index element do you want? ";
    cin >> j;
    for(k=0;k<i;k++)
            cout <<A[k]<<endl;
    p = RSelect(A, 0, i-1, j-1);
    cout <<p<<" is the "<<j<<"th element of the array."<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


QuickSort.txt is a file with the natural numbers from 1 to 10000 in it out of order. Hope that trying to find whatever I've messed up is i) possible and ii) not onerous.
Last edited on
After this loop

1
2
3
4
5
6
    while(input.good())
    {
                       input >> j;
                       A[i] = j;
                       i++;
    }


i is 10001.

In this call, p = RSelect(A, 0, i-1, j-1);, i-1 is 10000.








Last edited on
But when I output i here:
cout <<i<<" Which index element do you want? ";
it's 10000, and so in
p = RSelect(A, 0, i-1, j-1);
it'll be 9999, which should be fine, as the array I'm looking through is from 0 to 9999. Or am I mistaken?

With the slightly weird bit here cout<<"plibble"<<A[p]<<endl;return A[p]; I'm getting it to output the value it should be returning, and then that value should be what's assigned to p, and then it should output p; but instead I get some other value. I've definitely missed something, but I have no idea what.
Last edited on
I only tried with a file of ten unsorted numbers so will check my code again.

EDIT: My mistake was to have an empty line at the end of the numbers file.
Last edited on
Topic archived. No new replies allowed.