冒泡排序

来源:互联网 发布:小米路由器mac地址克隆 编辑:程序博客网 时间:2024/05/16 13:48
using System;using System.Collections.Generic;using System.Text;namespace 冒泡排序{    class Program     {       static void Main( string [] args)         {      // 取长度最长的词组 -- 冒泡法             Console .WriteLine( "请输入元素个数 N=" );             int N = int .Parse( Console .ReadLine());             int [] myArray = new int [N];             for ( int x = 0; x < N; x++)             {   Console .WriteLine( "输入第{0}个元素值:" ,x+1);                 myArray[x] = int .Parse( Console .ReadLine());             }                 for ( int j = 1; j < myArray.Length; j++)                 { for ( int i = 0; i < myArray.Length - 1; i++)                     {   // 如果 myArray[i] > myArray[i+1] ,则 myArray[i] 上浮一位                         if (myArray[i] > myArray[i + 1])                         {    int temp = myArray[i];                             myArray[i] = myArray[i + 1];                             myArray[i + 1] = temp;                         }                     }                 }             Console .WriteLine( "\n排序结果为:" );             for ( int i = 0; i < myArray.Length; i++)             {    Console .WriteLine( "A{0}={1}" ,i+1, myArray[i]);             }             Console .ReadLine();         }     }}

0 0