wanhuo_haihai 于 2016.01.25 20:01 提问

C#冒泡排序算法，调试不通过，求大神

7个回答

leilba   2016.01.26 10:54

``````     public void BubbleSort()
{
int temp;
for (int outer = 0; outer < upper; outer++)
{
for (int inner = 0; inner < upper - outer; inner++)
{

if ((int)arr[inner] > arr[inner + 1])
{
temp = arr[inner];
arr[inner] = arr[inner + 1];
arr[inner + 1] = temp;
}
}
}
}
``````

96 15 66 90 35 94 71 61 34 14

14 15 34 35 61 66 71 90 94 96

caozhy      2016.01.25 20:15
``````using System;
class Carray
{
private int[] arr;
private int upper;
private int numElements;
public Carray(int size)
{
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int i = 0; i <= upper; i++)
Console.Write(arr[i] + " ");
}
public void BubbleSort()
{
int temp;
for (int outer = 0; outer < upper; outer++)
{
for (int inner = outer; inner < upper; inner++)
{

if ((int)arr[inner] > arr[inner + 1])
{
temp = arr[inner];
arr[inner] = arr[inner + 1];
arr[inner + 1] = temp;
}
}
}
}
static void Main()
{
Carray nums = new Carray(10);
Random rnd = new Random(100);//100以内的随机数
for (Int16 i = 0; i < 10; i++)
{
nums.Insert(rnd.Next(0, 100));
}
Console.WriteLine("排序之前:");
nums.DisplayElements();
Console.WriteLine("\n正在排序:");
nums.BubbleSort();
Console.WriteLine("\n排序之后:");
nums.DisplayElements();
}
}
``````
caozhy      2016.01.25 20:38

caozhy      2016.01.25 20:38
`````` 排序之前:
96 15 66 90 35 94 71 61 34 14

15 66 35 61 14 34 71 90 94 96
``````

wanhuo_haihai   2016.01.25 20:29

wanhuo_haihai 回复caozhy: 要不你截图发给我看看，或者分享你的代码地址

wanhuo_haihai 回复caozhy: 后面两个数字没有纳入排序，不对啊

caozhy 我用你的网站测试，怎么看是否通过，看运行结果也是对的。

lostmostar   2016.01.26 17:50
`````` using System;
class Carray
{
private int[] arr;
private int upper;
private int numElements;
public Carray(int size)
{
arr = new int[size];
upper = size - 1 ;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int i = 0; i <= upper; i++)
Console.Write(arr[i] + " ");
}
public void BubbleSort()
{
//int temp;
//外循环,从最后一个数字开始
for (int outer = upper; outer > 0; outer--)
{
//内循环，从数组第0个位置开始
for (int inner = 0; inner <= outer - 1; inner++)
{
if (arr[inner] > arr[inner + 1])
{
arr[inner] = arr[inner] ^ arr[inner+1];
arr[inner + 1] = arr[inner] ^ arr[inner+1];
arr[inner] = arr[inner] ^ arr[inner+1];
/**
temp = arr[inner];
arr[inner] = arr[inner + 1];
arr[inner] = temp;
**/
}
}
}
}
static void Main()
{
Carray nums = new Carray(10);
Random rnd = new Random(100);//100以内的随机数
for (Int16 i = 0; i < 10; i++)
{
nums.Insert(rnd.Next(0, 100));
}
Console.WriteLine("排序之前:");
nums.DisplayElements();
Console.WriteLine("\n正在排序:");
nums.BubbleSort();
Console.WriteLine("\n排序之后:");
nums.DisplayElements();
}
}
``````
enpterexpress   2016.01.25 21:28
`````` public void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length - 2; i++)
{
for (int j = array.Length - 1; j >= 1; j--)
{
if (array[j] < array[j - 1])
{
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
``````