奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

C語言

c#冒泡排序算法

時間:2025-02-03 13:46:08 C語言 我要投稿
  • 相關推薦

c#冒泡排序算法

  C#中如何實現(xiàn)冒泡排序?下面小編為大家整理了c#冒泡排序算法,希望能幫到大家!

  冒泡排序(Bubble Sort)

  冒泡排序算法的運作如下:

  1.比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。

  2.對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最后一對。在這一點,最后的元素應該會是最大的數(shù)。

  3.針對所有的元素重復以上的步驟,除了最后一個。

  4.持續(xù)每次對越來越少的元素重復上面的步驟,直到?jīng)]有任何一對數(shù)字需要比較。

平均時間復雜度

  復制代碼 代碼如下:

  ///

  /// 冒泡排序

  ///

  ///

  ///

  public static void BubbleSort(int[] arr, int count)

  {

  int i = count, j;

  int temp;

  while (i > 0)

  {

  for (j = 0; j < i - 1; j++)

  {

  if (arr[j] > arr[j + 1])

  {

  temp = arr[j];

  arr[j] = arr[j + 1];

  arr[j + 1] = temp;

  }

  }

  i--;

  }

  }

  //使用例子

  int[] y = new int[] { 1, 32, 7, 2, 4, 6, 10, 8, 11, 12, 3, 9, 13, 5 };

  BubbleSort(y, y.Length );

  foreach (var item in y)

  {

  Console.Write(item+" ");

  }

  //1 2 3 4 5 6 7 8 9 10 11 12 13 32

  簡單且實用的冒泡排序算法的控制臺應用程序。運行界面如下:

  復制代碼 代碼如下:

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Text;

  namespace 冒泡排序

  {

  class Program

  {

  ///

  /// 交換兩個整型變量的值

  ///

  ///要交換的第一個整形變量

  ///要交換的第一個整形變量

  private static void Reverse(ref int a, ref int b)

  {

  int temp = a;

  a = b;

  b = temp;

  }

  static void Main(string[] args)

  {

  while (true)

  {

  string[] strInput;//用來接收用戶輸入的字符串

  int[] intInput;

  string[] separator = { ",", " " };//設置分隔符

  Console.WriteLine("請輸入數(shù)據(jù),以","或空格分隔,或按"q"退出。");

  string str = Console.ReadLine();//接收鍵盤輸入

  if (str == "q")

  {

  return;

  }

  strInput = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);//將用戶輸入的字符串分割為字符串數(shù)組

  intInput = new Int32[strInput.Length];

  //將字符串數(shù)組的每一個元素轉換為整型變量

  //轉換時如果出現(xiàn)格式錯誤或溢出錯誤則提示

  try

  {

  for (int i = 0; i < strInput.Length; i++)

  {

  intInput[i] = Convert.ToInt32(strInput[i]);

  }

  }

  catch (FormatException err)

  {

  Console.WriteLine(err.Message);

  }

  catch(OverflowException err)

  {

  Console.WriteLine(err.Message);

  }

  //排序算法主體

  for (int i = 0; i < intInput.Length - 1; i++)//這里的Length要減1否則會超界

  {

  for (int j = 0; j < intInput.Length - i - 1; j++)//這里的Length要減i以減少重復運算

  {

  //如果元素j比它之后的一個元素大,則交換他們的位置

  //如此循環(huán)直到遍歷完整個數(shù)組

  if (intInput[j] > intInput[j + 1])

  {

  Reverse(ref intInput[j], ref intInput[j + 1]);

  }

  }

  }

  string strOutput = "";//用于輸出的字符串

  foreach (int temp in intInput)

  {

  strOutput += Convert.ToString(temp) + ",";

  }

  Console.WriteLine("排序后的數(shù)據(jù)為:rn{0}rn", strOutput);

  }

  }

  }

  }

【c#冒泡排序算法】相關文章:

c#快速排序算法04-21

C#排序算法之堆排序07-21

C#排序算法之快速排序01-07

C語言冒泡排序算法實例06-15

C++冒泡排序算法實例詳解06-09

c語言中冒泡排序、插入排序、選擇排序算法比較07-11

快速排序算法及C#版的實現(xiàn)示例07-03

冒泡排序算法原理及JAVA實現(xiàn)代碼方法03-20

C語言的冒泡排序方法04-06

主站蜘蛛池模板: 蛟河市| 平武县| 白城市| 澄城县| 大悟县| 磐安县| 库车县| 新安县| 碌曲县| 桐乡市| 阳信县| 象州县| 龙岩市| 枝江市| 水富县| 聂拉木县| 襄垣县| 易门县| 西藏| 香河县| 苏尼特左旗| 饶阳县| 隆子县| 黄大仙区| 河北省| 正安县| 彭水| 万全县| 锡林浩特市| 宜兰县| 苗栗市| 苍梧县| 卫辉市| 鹿泉市| 梁平县| 咸丰县| 章丘市| 晋江市| 南通市| 图们市| 桦甸市|