冒泡排序:
using System;
using System.Collections.Generic;
using System.Text;
namespace BubbleSorter
{
class BubbleSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
BubbleSort(myArray);
}
public static void BubbleSort(int[] myArray)
{
for (int i = 0; i < myArray.Length - 1; i++)
{
for (int j = 0; j < myArray.Length - 1 - i; j++)
{
if (myArray[j] > myArray[j + 1])
{
Swap(ref myArray[j], ref myArray[j + 1]);
}
}
}
}
private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
}
static void Main(string[] args)
{
int[] a = new int[] { 1,2,5,69,8,4,5,6,-51,9};
BubbleSorter.Sort(a);
for (int i = 0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
System.Console.ReadLine();
}
}
}