bubblesort.cs
来自「Microsoft?Visual C#?.NET (Core Reference」· CS 代码 · 共 47 行
CS
47 行
using System;
namespace MSPress.CSharpCoreRef.DelegateSort
{
public class BubbleSort
{
/// <summary>
/// A delegate is used to define the sort order for two
/// elements in the table.
/// </summary>
public delegate bool Order(object first, object second);
/// <summary>
/// Sort elements in an Array object, using the Order
/// delegate to determine how items should be sorted.
/// </summary>
/// <param name="table">Array to be sorted</param>
/// <param name="sortHandler">Delegate to manage
/// sort order.</param>
public void Sort(Array table, Order sortHandler)
{
if(sortHandler == null)
throw new ArgumentNullException();
bool nothingSwapped = false;
int pass = 1;
while(nothingSwapped == false)
{
nothingSwapped = true;
for(int index = 0; index < table.Length - pass; ++index)
{
// Use an Order delegate to determine the sort order.
if(sortHandler(table.GetValue(index),
table.GetValue(index + 1)) == false)
{
nothingSwapped = false;
object temp = table.GetValue(index);
table.SetValue(table.GetValue(index + 1), index);
table.SetValue(temp, index + 1);
}
}
++pass;
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?