您的位置:首页 > 编程语言 > C#

C#学习笔记—数组的选择排序

2010-06-18 00:30 281 查看
//方法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cp4
{
class SelectSorter
{
//按由小到大的顺序,对数组进行排序
public static void SelectSortAscending(int[] arr)
{
for (int i = 0; i < arr.Length-1; i++)
{
for (int j = i+1; j < arr.Length; j++)
{
if (arr[i] > arr[j])    //由大到小:arr[i] < arr[j]
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

//遍历打印出数组
public static void PrintArray(int[] arr)
{
foreach (int pint in arr)
{
Console.WriteLine(pint);
}
}

}
}

//测试类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cp4
{
class Test
{
public static void Main(string[] agrs)
{
int[] a = {45,65,78,12,36};
SelectSorter.SelectSortAscending(a);    //由于是静态方法,不用实例
SelectSorter.PrintArray(a);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: