//求n个数中的最小k个数
public static void TestMin(int k, int n)
{
Random rd = new Random();
int[] myArray = new int[n];
int[] newArray = new int[k];
for (int i = 0; i < n; i++)
{
// randomNumber = rd.Next(10000, 10000000);
myArray[i] = rd.Next(1, 1000);
Console.WriteLine(myArray[i]);
}
Console.WriteLine("**********************");
for (int i = 0; i < k; i++)
{
newArray[i] = myArray[i];
Console.WriteLine(newArray[i]);
}
Console.WriteLine("**********************");
Array.Sort(newArray);
for (int j = k; j < n; j++)
{
int max = newArray[k - 1];
// Console.WriteLine("The last five" + newArray[i]);
if (max < myArray[j])
{
continue;
}
else
{
newArray[k - 1] = myArray[j];
Array.Sort(newArray);
}
}
foreach (int i in newArray)
Console.WriteLine(i);
}
//求n个数中的最大k个数
public static void TestMax(int k, int n)
{
Random rd = new Random();
int[] myArray = new int[n];
int[] newArray = new int[k];
for (int i = 0; i < n; i++)
{
// randomNumber = rd.Next(10000, 10000000);
myArray[i] = rd.Next(1, 1000);
Console.WriteLine(myArray[i]);
}
Console.WriteLine("**********************");
for (int i = 0; i < k; i++)
{
newArray[i] = myArray[i];
Console.WriteLine(newArray[i]);
}
Console.WriteLine("**********************");
Array.Sort(newArray);
for (int j = k; j < n; j++)
{
int max = newArray[0];
// Console.WriteLine("The last five" + newArray[i]);
if (max > myArray[j])
{
continue;
}
else
{
newArray[0] = myArray[j];
Array.Sort(newArray);
}
}
foreach (int i in newArray)
Console.WriteLine(i);
}