Algorithm is fast choice, specific code is as follows:
# include & lt; Iostream>
# include & lt; Fstream>
# include & lt; Vector>
using namespace std;
Int the pivot (vector
Int QuickSelect (vector
Int main ()
{
Int N, k, buffer, ak.
Ifstream infile (" input2. TXT ");
Infile> N;
Infile> k;//read from the file
Vector
for(int i=0; I
Infile> Buffer;
Amy polumbo ush_back (buffer);//array is read from the file
}
Ak=QuickSelect (a, 0, a.s considering () - 1, k);
Cout
Int the pivot (vector
{
Vector
Int k, j=0, the pivot;
For (int I=left; I
K=QuickSelect (a, I, I + 4, 3);
P. ush_back (k);
j++;
}
The pivot=QuickSelect (b, 0, b.s considering () - 1, (b.s considering () - 1)/2);//to find after group to find the median of the median
Return the pivot;
}
Int QuickSelect (vector
{
Int v.
V=the pivot (a, left, right);
Int I=left, j=right, t;
While (I & lt; J)//big put behind, put in front of the small
{
While (a [j] & gt;=v & amp; & I & lt; J)
j--;
While (a [I] <=v & amp; & I & lt; J)
i++;
T=a [I];
A [I]=a, [j].
A [j]=t;
}
If (k & lt; I + 1)
Return QuickSelect (a, left, I - 1, k);//if smaller than I, in the front looking
Else if (k & gt; I + 1)//if the ratio I big, in the back looking
Return QuickSelect (a, I + 1, right, k);
The else
Return a [I];
}
The file content:
10 3
1, 3, 11, 4, 5, 8 June 9 July 10
CodePudding user response:
Stack overflow the Stack benefitsQuickSelect () and pivot () a little logic problems these two functions, change yourself
CodePudding user response:
Agree with upstairsQuickSelect () and pivot () call each other, form a loop, cause function stack overflow
Single-step tracking, find the place with wrong logic,
CodePudding user response:
I changed once, use two vector to deposit is smaller than the pivot and the number ofBut it appeared the error C2660: "QuickSelect:" function does not accept two parameters
The QuickSelect clearly is I wrote two parameters of ah.. Why is this so? Rookie please comment...
# include & lt; Iostream>
# include & lt; Fstream>
# include & lt; Vector>
using namespace std;
Int the pivot (vector
Int QuickSelect (vector
Int main ()
{
Int N, k, buffer, ak.
Ifstream infile (" input2. TXT ");
Infile> N;
Infile> k;
Vector
for(int i=0; I
Infile> Buffer;
Amy polumbo ush_back (buffer);
}
Ak=QuickSelect (a, 0, a.s considering () - 1, k);
Cout
Int the pivot (vector
{
Vector
Int k, j=0, the pivot;
for(int i=0; I
Vector
TMP. The push_back (a [I]);
TMP. The push_back (a [I + 1));
TMP. The push_back (a [I] + 2);
TMP. The push_back (a + 3 [I]);
TMP. The push_back (a [I + 4]);
K=QuickSelect (TMP, 3);
P. ush_back (k);
j++;
}
The pivot=QuickSelect (b, (b.s considering () + 1)/2);
Return the pivot;
}
Int QuickSelect (vector
{
Int v.
Vector
If (a.s considering () & gt; 4)
V=the pivot (a);
The else
V=a, [0].
for(int i=0; I
If (a [I] <=v)
{
L.p ush_back (a [I]);
}
If (a [I] & gt; V)
{
R.p ush_back (a [I]);
}
}
If (k & lt; L.s considering ())
Return QuickSelect (L, k);
Else if (k & gt; L.s considering ())
Return QuickSelect (R, k - L.s considering ());
The else
Return v.
}
CodePudding user response: