如何使我的快速排序算法以升序和降序对数组进行排序? [英] How can I make my Quick Sort Algorithm sort the arrays in both Ascending and Descending order?
问题描述
当前,我的快速排序算法在情况1中以升序对数组进行排序,但是我想这样做,以便当用户选择选项2(情况2)时,它以降序对数组进行排序.是否需要为每种情况创建2个单独的算法?还是有一种更简单,更有效的方法?
Currently my Quick Sort algorithm sorts the array in Ascending order in case 1 but I would like to make it so that when the user selects option 2 (case 2) then it sorts the array in Descending order. Do I have to create 2 separate algorithms for each case? Or is there a simpler and more efficient way?
感谢帮助.
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);
}
推荐答案
您可以将Quicksort方法更改为接受IComparer<T> comparer
,然后使用该方法进行比较.
You can change your Quicksort method to accept an IComparer<T> comparer
, and then use that to make the comparisons.
然后,如果需要默认的比较顺序,则可以使用Comparer<T>.Default
,也可以使用Comparer<T>.Create()
创建自定义(例如反向)比较.
Then you can use Comparer<T>.Default
if you want the default comparison order, or you can use Comparer<T>.Create()
to create a custom (e.g. reversed) comparison.
可编译的示例:
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
int[] data = {6, 7, 2, 3, 8, 1, 9, 0, 5, 4};
QuickSort(data);
Console.WriteLine(string.Join(", ", data)); // Prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
QuickSort(data, Comparer<int>.Create((a, b) => b.CompareTo(a)));
Console.WriteLine(string.Join(", ", data)); // Prints 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
}
public static void QuickSort<T>(T[] data)
{
Quick_Sort(data, 0, data.Length - 1, Comparer<T>.Default);
}
public static void QuickSort<T>(T[] data, IComparer<T> comparer)
{
Quick_Sort(data, 0, data.Length - 1, comparer);
}
public static void Quick_Sort<T>(T[] data, int left, int right, IComparer<T> comparer)
{
int i, j;
T pivot, temp;
i = left;
j = right;
pivot = data[(left + right) / 2];
do
{
while ( (comparer.Compare(data[i], pivot) < 0) && (i < right)) i++;
while ( (comparer.Compare(pivot, 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, comparer);
if (i < right) Quick_Sort(data, i, right, comparer);
}
}
}
这有两个优点:
- 类型
T
不需要实现IComparable<T>
.您可以传入进行比较的IComparer<T>
对象. - 您可以通过传入不同的自定义
IComparer<T>
对象来以多种方式对同一数据进行排序.
- The type
T
does NOT need to implementIComparable<T>
. You can pass in anIComparer<T>
object that does the comparison. - You can sort the same data in multiple ways, by passing in different custom
IComparer<T>
object.
这是许多Linq IEnumerable扩展采用的方法,例如
This is the approach adopted by a number of the Linq IEnumerable extensions, for example Enumerable.OrderBy()
这篇关于如何使我的快速排序算法以升序和降序对数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!