30
int[] arr = {800,11,50,771,649,770,240, 9};

int temp = 0;

for (int write = 0; write < arr.Length; write++)
{
    for (int sort = 0; sort < arr.Length - 1; sort++)
    {
        if (arr[sort] > arr[sort + 1])
        {
            temp = arr[sort + 1];
            arr[sort + 1] = arr[sort];
            arr[sort] = temp;
        }       
    }   
    Console.Write("{0} ", arr[write]);  
}

我正在尝试做的只是对这个数组进行简单的冒泡排序。我想弄清楚为什么排序搞砸了。例如,这里是数组时{800,11,50,771,649,770,240, 9}

这是显示的内容: 11, 50, 649, 9, 649, 770, 771, 800

我在想我可能会在比较中遗漏一些东西。

4

18 回答 18

75

不,您的算法有效,但您的Write操作在外循环中放错了位置。

int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };

int temp = 0;

for (int write = 0; write < arr.Length; write++) {
    for (int sort = 0; sort < arr.Length - 1; sort++) {
        if (arr[sort] > arr[sort + 1]) {
            temp = arr[sort + 1];
            arr[sort + 1] = arr[sort];
            arr[sort] = temp;
        }
    }
}

for (int i = 0; i < arr.Length; i++)
    Console.Write(arr[i] + " ");

Console.ReadKey();
于 2013-02-08T07:59:28.270 回答
12

这个对我有用

public static int[] SortArray(int[] array)
{
    int length = array.Length;

    int temp = array[0];

    for (int i = 0; i < length; i++)
    {
        for (int j = i+1; j < length; j++)
        {
            if (array[i] > array[j])
            {
                temp = array[i];

                array[i] = array[j];

                array[j] = temp;
            }
        }
    }

    return array;        
}
于 2013-04-22T10:14:34.520 回答
6
public static void BubbleSort(int[] a)
    {

       for (int i = 1; i <= a.Length - 1; ++i)

            for (int j = 0; j < a.Length - i; ++j)

                if (a[j] > a[j + 1])


                    Swap(ref a[j], ref a[j + 1]);

    }

    public static void Swap(ref int x, ref int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
于 2014-10-14T16:38:37.580 回答
4
int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };

int temp = 0;

for (int write = 0; write < arr.Length; write++)
{
    for (int sort = 0; sort < arr.Length - 1 - write ; sort++)
    {
        if (arr[sort] > arr[sort + 1])
        {
            temp = arr[sort + 1];
            arr[sort + 1] = arr[sort];
            arr[sort] = temp;
        }
    }
}

for (int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " ");

Console.ReadKey();
于 2013-12-01T19:23:54.900 回答
3

我看到有人将此示例用作工作申请测试的一部分。我对他的反馈是,当数组大部分被排序时,它缺乏从外循环的逃逸。

考虑在这种情况下会发生什么:

int[] arr = {1,2,3,4,5,6,7,8};

这是更有意义的事情:

int[] arr = {1,2,3,4,5,6,7,8};

int temp = 0;
int loopCount=0;
bool doBreak=true;

for (int write = 0; write < arr.Length; write++)
{
    doBreak=true;
    for (int sort = 0; sort < arr.Length - 1; sort++)
    {
        if (arr[sort] > arr[sort + 1])
        {
            temp = arr[sort + 1];
            arr[sort + 1] = arr[sort];
            arr[sort] = temp;
            doBreak=false;
        }
        loopCount++;
    }
    if(doBreak){ break; /*early escape*/ }
}

Console.WriteLine(loopCount);
for (int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " ");
于 2013-06-12T15:36:22.127 回答
1

我想在接受的答案中添加一些不同的东西:也可以减少迭代次数,如下所示。

int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };

int temp = 0;
int arrLength = arr.Length;

for (int write = 0; write < arr.Length - 1; write++, arrLength--)
{
    for (int sort = 0; sort < arrLength - 1; sort++)
    {
        if (arr[sort] > arr[sort + 1])
        {
            temp = arr[sort + 1];
            arr[sort + 1] = arr[sort];
            arr[sort] = temp;
        }
    }
}

foreach (var item in arr)
{
    Console.WriteLine(item);
}
于 2019-03-16T11:38:21.690 回答
0

Console.Write("{0} ", arr[write]);的太早了。您在排序仍在进行时打印值。例如,您正在打印9为数组中的索引 3,但在循环的下一次迭代中,它9已移至索引 2 并240已移至索引 3...但您的外部循环已向前移动,因此它649第二次打印并且240永远不会被打印。

于 2013-02-08T08:07:37.007 回答
0
int[] array = new int[10] { 13, 2, 5, 8, 23, 90, 41, 4, 77, 61 };

for (int i = 10; i > 0; i--)
{
    for (int j = 0; j < 9; j++)
    {
        if (array[j] > array[j + 1])
        {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }
}
于 2014-01-09T20:53:45.060 回答
0
    static bool BubbleSort(ref List<int> myList, int number)
    {
        if (number == 1)
            return true;
        for (int i = 0; i < number; i++)
        {
            if ((i + 1 < number) && (myList[i] > myList[i + 1]))
            {
                int temp = myList[i];
                myList[i] = myList[i + 1];
                myList[i + 1] = temp;
            }
            else
                continue;
        }
        return BubbleSort(ref myList, number - 1);
    }
于 2014-05-06T22:55:38.223 回答
0

只是另一个示例,但使用外部 WHILE 循环而不是 FOR:

public static void Bubble()
    {
        int[] data = { 5, 4, 3, 2, 1 };
        bool newLoopNeeded = false;
        int temp;
        int loop = 0;

        while (!newLoopNeeded)
        {
            newLoopNeeded = true;
            for (int i = 0; i < data.Length - 1; i++)
            {
                if (data[i + 1] < data[i])
                {
                    temp = data[i];
                    data[i] = data[i + 1];
                    data[i + 1] = temp;
                    newLoopNeeded = false;
                }
                loop++;
            }
        }
    }
于 2014-07-24T15:26:13.367 回答
0
public static int[] BubbleSort(int[] arr)
{
   int length = arr.Length();

   while (length > 0)
   {
      int newLength = 0;
      for (int i = 1; i < length; i++)
      {
         if (arr[i - 1] > arr[i])
         {
            Swap(ref arr[i - 1], ref arr[i]); 
            newLength = i;   
         }   
      }
      length = newLength;
   }
}

public static void Swap(ref int x, ref int y)
{
   int temp = y;
   y = x;
   x = temp;
}
于 2018-05-07T22:27:46.817 回答
0

带排序方向的冒泡排序 -

using System;

public class Program
{
    public static void Main(string[] args)
    {
        var input = new[] { 800, 11, 50, 771, 649, 770, 240, 9 };

        BubbleSort(input);

        Array.ForEach(input, Console.WriteLine);

        Console.ReadKey();
    }

    public enum Direction
    {
        Ascending = 0,
        Descending
    }

    public static void BubbleSort(int[] input, Direction direction = Direction.Ascending)
    {
        bool swapped;
        var length = input.Length;

        do
        {
            swapped = false;
            for (var index = 0; index < length - 1; index++)
            {
                var needSwap = direction == Direction.Ascending ? input[index] > input[index + 1] : input[index] < input[index + 1];

                if (needSwap)
                {
                    var temp = input[index];
                    input[index] = input[index + 1];
                    input[index + 1] = temp;
                    swapped = true;
                }
            }
        } while (swapped);
    }
}
于 2018-09-27T03:10:16.640 回答
0

这是我使用递归方法编写的:

    public static int[] BubbleSort(int[] input)
    {
        bool isSorted = true;
        for (int i = 0; i < input.Length; i++)
        {
            if (i != input.Length - 1 && input[i] > input[i + 1])
            {
                isSorted = false;
                int temp = input[i];
                input[i] = input[i + 1];
                input[i + 1] = temp;
            }
        }
        return isSorted ? input : BubbleSort(input);
    }
于 2018-12-07T12:20:45.010 回答
0

它以更优雅的方式做同样的事情。

var arrayValues = new[] { 99, 12, 11, 300, 400, 10, 9, 3, 6, 5, 7, 8};
for (var mainLoop = 0; mainLoop < arrayValues.Length; mainLoop++)
{
   for (var innerLoop = mainLoop + 1; innerLoop < arrayValues.Length; innerLoop++)
   {
       if (arrayValues[mainLoop] <= arrayValues[innerLoop])
       {
         continue;
       }

       var temp = arrayValues[mainLoop];
       arrayValues[mainLoop] = arrayValues[innerLoop];
       arrayValues[innerLoop] = temp;
  }
}
于 2019-02-27T01:09:25.680 回答
0

所以我把我的作为一个递归函数(不需要嵌套循环),如果这效率低下(与其他解决方案相比),也许有人可以评论。

    public static int[] BubbleSort(int[] arrayOfValues)
    {
        var swapOccurred = false;
        
        for (var i = 0; i < arrayOfValues.Length; i++)
        {
            if (i == arrayOfValues.Length - 1)
                continue;

            if (arrayOfValues[i] > arrayOfValues[i + 1])
            {
                //swap values
                var current = arrayOfValues[i];
                var next = arrayOfValues[i + 1];
                
                arrayOfValues[i] = next;
                arrayOfValues[i + 1] = current;

                swapOccurred = true;
            }
        }

        if (swapOccurred)
        {
            // keep going until no further swaps are required:
            BubbleSort(arrayOfValues);
        }

        return arrayOfValues;
    }
于 2021-04-22T01:32:03.430 回答
-1
int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };
for (int i = 0; i < arr.Length; i++)
{
    for (int j = i; j < arr.Length ; j++)
    {
        if (arr[j] < arr[i])
        {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}
Console.ReadLine();
于 2014-09-20T18:55:36.243 回答
-1
   using System; 
 using System.Collections.Generic; 
 using System.Linq;  
using System.Text; 
 using System.Threading.Tasks;

namespace Practice {
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the size");
            int n = Convert.ToInt32(Console.ReadLine());
            int[] mynum = new int[n];
            Console.WriteLine("Enter the Numbers");
            for (int p = 0; p < n;p++ )
            {
                mynum[p] = Convert.ToInt32(Console.ReadLine());

            }
            Console.WriteLine("The number are");
                foreach(int p in mynum)
                {
                    Console.WriteLine(p);
                }
                for (int i = 0; i < n;i++ )
                {
                    for(int j=i+1;j<n;j++)
                    {
                        if(mynum[i]>mynum[j])
                        {
                            int x = mynum[j];
                            mynum[j] = mynum[i];
                            mynum[i] = x;
                        }
                    }
                }
                Console.WriteLine("Sortrd data is-");
            foreach(int p in mynum)
            {
                Console.WriteLine(p);
            }
                    Console.ReadLine();
        }
    } }
于 2016-09-07T12:18:06.117 回答
-1
    public void BubbleSortNum()
    {
        int[] a = {10,5,30,25,40,20};
        int length = a.Length;
        int temp = 0; 
        for (int i = 0; i <length; i++)
        {               
            for(int j=i;j<length; j++)
            {
                if (a[i]>a[j])
                {
                    temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }     
            }
           Console.WriteLine(a[i]);
        }       
     }
于 2017-10-11T21:40:22.153 回答