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 に答える
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

あなたConsole.Write("{0} ", arr[write]);は早すぎます。並べ替えがまだ進行中のときに値を出力しています。たとえば9、配列内のインデックス 3 として出力しているにもかかわらず、ループの次の繰り返しで、9インデックス 2240に移動し、インデックス 3 に移動しました... それでも、外側のループが前方に移動したため、6492 回目に印刷され、240印刷されません。

于 2013-02-08T08:07:37.007 に答える
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 に答える
-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 に答える