目前我的快速排序算法在情況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