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();
}
}
}
冒泡排序
最新推荐文章于 2021-06-24 11:47:51 发布