新手求助,大家帮我看看,我的数组怎么不按照排序后进行输出呢,我应该怎么修改啊?
using System;using System.Collections.Generic;
using System.Text;
namespace BubbleSort1
{
class Program
{
public void Bubblesorta(int[] arrayList)
{
int i, j, temp;
//bool temp;
for (j = 1; j <= arrayList.Length - 1; j++)
for (i = 0; i < arrayList.Length - j; i++)
if (arrayList[i] > arrayList[i + 1])
{
temp = arrayList[i]; arrayList[i] = arrayList[i + 1]; arrayList[i] = temp;
}
}
static void Main(string[] args)
{
int[] arr = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
Program p = new Program();
p.Bubblesorta(arr);
for (int m = 0; m < arr.Length; m++)
Console.WriteLine("{0} ", arr[m]);
Console.WriteLine();
Console.ReadKey();
}
}
} --------------------编程问答-------------------- Bubblesorta 要return arrayList;
或者public void Bubblesorta(ref int[] arrayList)这样声明
--------------------编程问答-------------------- temp = arrayList[i]; arrayList[i] = arrayList[i+1]; arrayList[i+1] = temp;
--------------------编程问答-------------------- 谢谢楼上的,我已经修改好了:) --------------------编程问答--------------------
--------------------编程问答-------------------- 写程序要细心,格式也不注意,为什么不分开行写?
public void Bubblesorta(ref int[] arrayList)
{
int i, j, temp;
//bool temp;
for (j = 1; j <= arrayList.Length - 1; j++)
for (i = 0; i < arrayList.Length - j; i++)
if (arrayList[i] > arrayList[i + 1])
{
temp = arrayList[i]; arrayList[i] = arrayList[i + 1]; arrayList[i] = temp;
}
}
public void Bubblesorta(int[] arrayList)
{
int i, j, temp;
//bool temp;
for (j = 1; j <= arrayList.Length - 1; j++)
{
for (i = 0; i < arrayList.Length - j; i++)
{
if (arrayList[i] > arrayList[i + 1])
{
temp = arrayList[i];
arrayList[i] = arrayList[i + 1];
arrayList[i + 1] = temp;
}
}
}
}
static void Main(string[] args)
{
int[] arr = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
Program1 p = new Program1();
p.Bubblesorta(arr);
for (int m = 0; m < arr.Length; m++)
Console.WriteLine("{0} ", arr[m]);
Console.WriteLine();
Console.ReadKey();
}
--------------------编程问答-------------------- 不过呢,有现成的System.Array.Sort()方法可用,除非你想作排序练习:
class Program
{
static void Main()
{
int[] arr = { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
System.Array.Sort(arr);
foreach (int m in arr)
{
System.Console.WriteLine(m);
}
}
}
--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- Array.Sort()
补充:.NET技术 , C#