asp.net 冒泡排序问题

2024-12-01 17:22:33
推荐回答(2个)
回答1:

protected void Page_Load(object sender, EventArgs e)
{
//if(!IsPostBack)
//print();
} public void print()
{
int temp;
int[] InputValues = new int[]{80, 70, 60, 50, 40, 30, 120, 150, 160 }; for (int j = 0; j < InputValues.Length; j++)
{
for (int i = 0; i < InputValues.Length-1; i++)
{
if(InputValues[i] < InputValues[i+1])
{
temp = InputValues[i];
InputValues[i] = InputValues[i+1];
InputValues[i+1] = temp;
}
}
}
for (int i = 0; i < InputValues.Length; i++)
{
//Response.Write( string.Format("InputValues[{0}] = {1} ",i,InputValues[i].ToString())+"
");
Response.Write(InputValues[i].ToString() + "
");
}
} protected void Button1_Click(object sender, EventArgs e)
{
print();
}

回答2:

int[] myArray = new int[] { 10, 8, 3, 5, 6, 7, 4, 6, 9 };
for( int j=1;j{
for(int i=0;i {
if( myArray[i] {
int temp = myArray[i];
myArray[i] = myArray[i+1];
myArray[i+1] = temp;
}
}
}