.生成一个排序类,可以对一个数组进行从大到小和从小到大的排序,要求使...
发布网友
发布时间:2024-10-07 01:09
我来回答
共2个回答
热心网友
时间:2分钟前
这么巧啊,我们今天C#上机也是这个题目,正好给你参考参考 O(∩_∩)O哈哈~
using System;
using System.Collections.Generic;
using System.Text;
namespace 排序
{
public enum comparsion { True = 1, False = 0 }
public delegate void sortDelegate(int[] a);
class ArraySort
{
public static void arraysort(int[] array1)
{
for (int i = 0; i < array1.Length; i++)
for (int j = i + 1; j < array1.Length; j++)
{
if (array1[i] > array1[j])
{
int temp;
temp = array1[i];
array1[i] = array1[j];
array1[j] = temp;
}
}
}
}
class Program
{
public static void printSort(sortDelegate sort, comparsion m, int[] array)
{
sort(array);
if (m == comparsion.True)
{
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
}
if (m == comparsion.False)
{
for (int i = array.Length - 1; i >= 0; i--)
{
Console.Write(array[i] + " ");
}
}
}
static void Main(string[] args)
{
int[] array = new int[10] { 9, 88, 32, 4, 7, 1, 25, 11, 56, 10 };
printSort(ArraySort.arraysort, comparsion.True, array);
Console.WriteLine();
printSort(ArraySort.arraysort, comparsion.False, array);
Console.ReadLine();
}
}
}
那10个数字你可以随便改。
希望帮到你(*^__^*) 嘻嘻……
热心网友
时间:4分钟前
delegate void sort1(int[]n);
class p
{
public static int[] Ascending(int [] number,sort1 sort)
{
sort(number);
return number;
}
public static int[] Descending(int[] number,sort1 sort)
{
sort(number);
return number;
}
}
class Program
{
static void Main(string[] args)
{
sort1 sort = new sort1(Array.Sort);
sort1 sort2 = new sort1(Array.Reverse);
int[] num = new int[] { 12,45,23,7,95};
int[]m=p.Ascending(num,sort);
for (int i = 0; i < m.Length; i++)
{
Console.Write(" " + m[i]);
}
Console.WriteLine();
int[] n = p.Descending(m,sort2);
for (int i = 0; i < n.Length; i++)
{
Console.Write(" " + n[i]);
}
Console.WriteLine();
}
}