直接上代碼,代碼比較簡單
1 class Program 2 { 3 private delegate string GetAString(); 4 static void Main(string[] args) 5 { 6 7 int[] a = { 0, 5, 6, 2, 1 }; 8 BubbleSorter.Sort(a, BubbleSorter.Compa); 9 foreach (var item in a) 10 { 11 Console.Write(item); 12 } 13 Console.ReadLine(); 14 } 15 16 public static void SortA(int[] sortArray) 17 { 18 bool swapped = true; 19 do 20 { 21 swapped = false; 22 for (int i = 0; i < sortArray.Length - 1; i++) 23 { 24 if (sortArray[i] < sortArray[i + 1]) 25 { 26 int temp = sortArray[i]; 27 sortArray[i] = sortArray[i + 1]; 28 sortArray[i + 1] = temp; 29 swapped = true; 30 } 31 } 32 } 33 while (swapped); 34 } 35 } 36 37 class BubbleSorter 38 { 39 static public void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison) 40 { 41 bool swapped = true; 42 do 43 { 44 swapped = false; 45 for (int i = 0; i < sortArray.Count - 1; i++) 46 { 47 if (comparison(sortArray[i + 1], sortArray[i])) 48 { 49 T temp = sortArray[i]; 50 sortArray[i] = sortArray[i + 1]; 51 sortArray[i + 1] = temp; 52 swapped = true; 53 } 54 } 55 } while (swapped); 56 } 57 58 public static bool Compa(int i, int j) 59 { 60 return i < j; 61 } 62 }
using System;
using System.Collections.Generic;
using System.Text;
namespace GanggangApplication
{
class Program
{
static void Main(string[] args)
{
SortedNumbers();
}
/// <summary>
/// 該方法獲得需要排序的數組,表調用排序方法進行排序
/// </summary>
public static void SortedNumbers()
{
int numberCount;
int[] numbers;
Console.WriteLine("請問您要對多少個數字進行排序?");
numberCount = Convert.ToInt32(Console.ReadLine());
numbers = new int[numberCount];
Console.WriteLine("請輸入您要進行排序的這{0}個數字:", numberCount);
for (int i = 0; i < numberCount; i++)
{
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n您要進行排序的{0}個數字分別為:", numberCount);
for (int i = 0; i < numberCount; i++)
{
Console.Write(numbers[i].ToString() + "\t");
}
Console.WriteLine("\n您要對這{0}個數字進行什麼排序?(1表示冒泡排序,2表示選擇排序)", numberCount);
int method = Convert.ToInt32(Console.ReadLine());
while (method != 1 && method != 2)
{
Console.WriteLine("只能輸入1或者2,請您重新輸入!");
method = Convert.ToInt32(Console.ReadLine());
}
//調用排序方法
ExecuteSortedMe......余下全文>>
我n年前上學用的,你看看把,呵呵。希望對你有幫助
public class Test
{
public void Sx(int[] t)
{
for(int i = 0;i < t.length;i++)
{
if(t[i] < 10)
{
System.out.print("0"+t[i]+" ");
}
else
{
System.out.print(t[i]+" ");
}
if((i+1) % 6 == 0)
{
System.out.println();
}
}
}
public static void main(String[] args)
{
Test m = new Test();
int a[] = new int[36];
out1: for(int i = 0;i < 36;i++)
{
a[i] = (int)(Math.random() * 36 + 1);
for(int j = 0; j < 36 && j != i;j++)
{
if(a[j] == a[i])
{
i--;
continue out1;
}
}
}
m.Sx(a);
System.out.println();
//Arrays.sort(a);
//冒泡法
int temp;
for(int i=0;i<a.length;i++){
for(int j=0;j+1<a.length-i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
m.Sx(a);
}
}...余下全文>>