現在のところ、私のクイックソートアルゴリズムは、ケース1の昇順で配列をソートしますが、ユーザがオプション2(ケース2)を選択すると、降順で配列がソートされるようにしたいと考えています。それぞれの場合に2つの別個のアルゴリズムを作成する必要がありますか?または、より簡単で効率的な方法がありますか?クイックソートアルゴリズムでは、配列を昇順と降順の両方で並べ替えることができますか?
ヘルプを評価してください。
static void Main(string[] args)
{
Console.WriteLine("Analysis of Seismic Data.\n");
Console.WriteLine("Selection of Arrays:");
//Display all the options.
Console.WriteLine("1-Year");
Console.WriteLine("2-Month");
Console.WriteLine("3-Day");
Console.WriteLine("4-Time");
Console.WriteLine("5-Magnitude");
Console.WriteLine("6-Latitude");
Console.WriteLine("7-Longitude");
Console.WriteLine("8-Depth");
Console.WriteLine("9-Region");
Console.WriteLine("10-IRIS_ID");
Console.WriteLine("11-Timestamp\n\n");
Console.WriteLine("Use numbers to select options.");
//Read in user's decision.
Console.Write("Select which array is to be analysed:");
int userDecision = Convert.ToInt32(Console.ReadLine());
//Selected which array is to be analyzed
switch (userDecision)
{
case 1:
Console.WriteLine("\nWould you like to sort the Array in Ascending or Descending order?");
Console.WriteLine("1-Ascending");
Console.WriteLine("2-Descending");
//Create another switch statement to select either ascending or descending sort.
int userDecision2 = Convert.ToInt32(Console.ReadLine());
switch (userDecision2)
{
case 1:
//Here algorithm sorts my array in ascending order by default.
QuickSort(Years);
Console.WriteLine("Contents of the Ascending Year array: ");
foreach (var year in Years)
{
Console.WriteLine(year);
}
break;
case 2:
//How do I sort the same array in Descending order when Option 2 is selected?
//QuickSort(Years) Descendingly.
Console.WriteLine("Contents of the Descending Year array: ");
foreach (var year in Years)
{
Console.WriteLine(year);
}
break;
}
break;
case 2:
Console.WriteLine("\nWould you like to sort the Array in Ascending or Descending order?");
Console.WriteLine("1-Ascending");
Console.WriteLine("2-Descending");
//Create another switch statement to select either ascending or descending sort.
int userDecision3 = Convert.ToInt32(Console.ReadLine());
switch (userDecision3)
{
case 1:
QuickSort(Years);
Console.WriteLine("Contents of the Ascending Month array: ");
foreach (var month in Months)
{
Console.WriteLine(month);
}
break;
case 2:
//Same problem, how do I sort it in descending order?
Console.WriteLine("Contents of the Descending month array: ");
foreach (var month in Months)
{
Console.WriteLine();
}
break;
}
break;
}
}
public static void QuickSort<T>(T[] data) where T : IComparable<T>
{
Quick_Sort(data, 0, data.Length - 1);
}
public static void Quick_Sort<T>(T[] data, int left, int right) where T : IComparable<T>
{
int i, j;
T pivot, temp;
i = left;
j = right;
pivot = data[(left + right)/2];
do
{
while ((data[i].CompareTo(pivot) < 0) && (i < right)) i++;
while ((pivot.CompareTo(data[j]) < 0) && (j > left)) j--;
if (i <= j)
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
} while (i <= j);
if (left < j) Quick_Sort(data, left, j);
if (i < right) Quick_Sort(data, i, right);
}
比較の結果を変更することができます
また、ソート順を決定するパラメタを渡すこともできます。 Linqでは、マイクロソフトの開発者が独自のメソッドOrderByDescending()を作成することに決めました。 – fubo
組み込みのソート方法は使用できません – Ulfren