2013-02-08 599 views
18
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}簡單的氣泡排序c#

這裏是獲取顯示:11, 50, 649, 9, 649, 770, 771, 800

我想,我可能會丟失在比較的東西。

+0

你是外循環,從開始到去年底,應該是年底開始!你內在的循環也應限制在寫入的值。 – Polity

+0

@Polity:我不相信這是正確的。如答案所示,外部循環是正確的。儘管你對內部循環是正確的。 –

+0

我希望這只是一個學習數組操作的練習? 我不能想到任何應用程序的氣泡排序將是'最佳'的排序策略。如果只是爲了演示/心理練習,那麼很好,但如果你使用這個是真實世界的應用程序,或許你應該看看其他的「排序」算法。 – Th3Minstr3l

回答

52

不,您的算法可以正常工作,但您的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(); 
+3

無論誰提出我的寫字都是錯位的,謝謝!這是什麼導致事情搞砸了。現在,它的工作 –

0

您的Console.Write("{0} ", arr[write]);爲時尚早。當排序仍在進行中時,您正在打印值。例如,您將9作爲索引3打印在數組中,但在循環的下一次迭代中,9已移至索引2,而240已移至索引3 ...但您的外循環已移動所以它第二次打印649並且240從不打印。

+0

這不是真的,他打印出最後的書面價值。這確實意味着在修復之後,結果將以下降的順序打印(儘管排序)。 – Polity

+0

@Polity - 「他正在打印出最後一筆書面價值。」 - 我想你誤解了「泡泡排序」。在算法完成排序之前,他明確地向控制檯輸出值。只要他只是想實現冒泡排序,上面的實際排序邏輯沒有任何問題。 - http://en.wikipedia.org/wiki/Bubble_sort – McAden

9

這一個對我的作品

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;   
} 
+0

有幾乎相同的解決方案: int [] unsorted = new int [] { 3,4,13,1,18,22,2,100,11 }; //冒泡排序 對(INT I = 0; I

+0

它不是[Bubble sort](https://en.wikipedia.org/wiki/Sorting_algorithm#Bubble_sort)。從wikipedia開始:「算法從數據集的開始處開始,它比較前兩個元素,如果第一個元素大於第二個元素,則交換它們。**繼續爲每對相鄰元素執行此操作數據集的末尾。**然後重新開始使用前兩個元素,重複直到最後一遍沒有交換。「 – MiniMax

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] + " "); 
+3

我同意你的反饋意見,但那不是從外部循環逃脫的「傳統」氣泡排序。 –

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(); 
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; 
     } 
    } 
} 
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); 
    } 
+0

也許寫一個簡短的解釋。 – Numbers

0

又一個例子,但有outter 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++; 
      } 
     } 
    } 
+1

這個while循環的例子比默認的BubbleSort和上面提供的隨機排序數據早期的逃逸BubbleSort算法都慢...... – ManIkWeet

-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(); 
+1

這是錯誤的,上面的代碼是選擇排序 - 而不是冒泡排序..在泡沫排序中你移動比較相鄰的元素..請更新它。對於(int i = 0; j arr [j + i ]) { int temp = arr [j + 1]; arr [j + 1] = arr [j]; arr [j] = temp; } } } –

5
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; 
    } 
+5

請不要只是發佈代碼。解釋你給我們展示的內容。 – Andrew

+10

清晰且自我記錄的代碼不需要評論。 – birdus

-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(); 
     } 
    } } 
+1

它錯了 - 你在這裏顯示選擇排序。您將第一個元素I = 0與j = I + 1的每個元素進行比較,這是選擇排序而不是冒泡排序。在每次傳遞的冒泡排序中,第一個元素j =與j + 1進行比較,如果不是,被交換,這將在我每次傳球時完成。請檢查您的for循環和matten的第一個答案 –

-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]); 
     }  
    }