- using System;
- namespace BubbleSorter
- {
- /// <summary>
- /// 冒泡排序
- /// 作者:范中磊
- /// </summary>
- public class BubbleSorter
- {
- public void Sort1(int[] list)
- {
- int i, j, temp;
- bool done = false;
- j = 1;
- while ((j < list.Length) && (!done))
- {
- done = true;
- for (i = 0; i < list.Length - j; i++)
- {
- if (list[i] > list[i + 1])
- {
- done = false;
- temp = list[i];
- list[i] = list[i + 1];
- list[i + 1] = temp;
- }
- }
- j++;
- }
- }
- public void Sort2(int[] list)
- {
- int i, j, temp;
- for (i = 0; i < list.Length; i++)
- {
- for (j = 0; j < list.Length - i - 1; j++)
- {
- if (list[j] > list[j + 1])
- {
- temp = list[j];
- list[j] = list[j + 1];
- list[j + 1] = temp;
- }
- }
- }
- }
- public static void Main(string[] args)
- {
- int[] iArray = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
- BubbleSorter bs = new BubbleSorter();
- bs.Sort1(iArray);// or Sort2
- for (int m = 0; m < iArray.Length; m++)
- {
- Console.Write("{0}", iArray[m]);
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
- }
C# 冒泡排序
最新推荐文章于 2024-07-20 15:30:04 发布