2014-04-22 25 views
0
int i, j, temp, size = 4; 
int[] bar= {6, 4, 7, 1}; 
for(i=0; i< size - 1; i++){ 
    for(j=i; j< size; j++) { 
    if(bar[j] < bar[i]){ 
     temp = bar[j]; 
     bar[j] = bar[i]; 
     bar[i] = temp; 
    } 
    } 
} 

請幫我理解這一點。它是如何正確排序這些數字。我感到非常困惑。我如何理解這種排序?

如果你想知道,在我正在做的程序中,在這個循環的第一個輸出中,數字出現爲1,6,7和4,所以請幫助我解釋它是如何排序的!就像一步一步!謝謝!

+0

似乎冒泡排序http://en.wikipedia.org/wiki/Bubble_sort – Bakudan

+0

使用'watch'可以幫助您瞭解在執行操作時存儲了什麼值,只需使用帶有watch的C程序就可以幫助您理解此代碼容易。 –

回答

0

所以,這很容易。

您的初始數組看起來像{6,4,7,1}。

  1. I = 0。
  2. J = = 0
  3. 比較用0-元素0元件,所以與6.比較6他們是相同的,什麼也不做。
  4. j ++; j = 1.
  5. 比較1元素與0元素,所以比較4與6,4更小,交換它們,數組看起來像{4,6,7,1}。
  6. j ++; j = 2.
  7. 比較2元素與0元素,所以比較7與4. 7更大,什麼都不做。
  8. j ++; j = 3.
  9. 比較3元素與0元素,所以比較1與4,1比較小,交換它們,數組看起來像{1,6,7,4}。因此,正如您在第一次完成內部循環時所描述的那樣。
  10. i ++; I = 1
  11. J = = 1
  12. 等等...
0

這絕對冒泡排序:

啓動陣列: {6,4,7,1}

索引號: 6 =巴[0],4 =棒[1],7 =杆[2],1 =杆[3]

步驟1:

I = 0,J = 0,酒吧[I] = 6,巴[J] = 6

if(bar[j] < bar[i]) {...} // condition is not true so nothing happens 

你的數組: {6,4,7,1 }

增加j減1。

步驟2:

I = 0,J = 1,酒吧[I] = 6,巴[J] = 4

if(bar[j] < bar[i]) // condition is true, if loop starts 
{ 
    temp = bar[j]; // bar[j] value is 4, temp value is now 4 
    bar[j] = bar[i]; // bar[i] value is 6, bar[j] value is now 6 
    bar[i] = temp; // temp value is 4, bar[i] value is now 4 
} 

觀察:巴[I] = 4,bar [j] = 6 - 正如你所看到的值交換,這就是所有這個循環。

你的數組: {4,6,7,1}

增加Ĵ由1

步驟3:

I = 0,J = 2,bar [i] = 4,bar [j] = 7

if(bar[j] < bar[i]) {...} // condition is not true so nothing happens 

你的數組: {4,6,7,1}

j增加1。

步驟4:

I = 0,J = 3,巴[ I] = 4,巴[J] = 1

if(bar[j] < bar[i]) // condition is true, if loop starts 
{ 
    temp = bar[j]; // bar[j] value is 1, temp value is now 1 
    bar[j] = bar[i]; // bar[i] value is 4, bar[j] value is now 4 
    bar[i] = temp; // temp value is 1, bar[i] value is now 1 
} 

觀察:巴[I] = 1,酒吧[J] = 4

你的數組: {1,6,7,4}

增加Ĵ由1

由於Ĵ值現在是4和尺寸爲4(j <大小)條件是不正確的。對於循環結束,所以外循環可以繼續。

步驟5:

增加由1

I = 1,J = 1,酒吧[I] = 6,巴[J] = 6

if(bar[j] < bar[i]) {...} // condition is not true so nothing happens 

你的數組: {1,6,7,4}

增加j增加1。

步驟6:

I = 1,J = 2,巴[I] = 6,巴[J] = 7

if(bar[j] < bar[i]) {...} // condition is not true so nothing happens 

你的數組: {1, 6,7,4}

增加Ĵ由1

步驟7:

I = 1,J = 3,巴[I] = 6,巴[J] = 4

if(bar[j] < bar[i]) // condition is true, if loop starts 
{ 
    temp = bar[j]; // bar[j] value is 4, temp value is now 4 
    bar[j] = bar[i]; // bar[i] value is 6, bar[j] value is now 6 
    bar[i] = temp; // temp value is 4, bar[i] value is now 4 
} 

觀察:巴[I] = 4 ,酒吧[J] = 6

你的數組: {1,4,7,6}

增加Ĵ由1.

j的值再次超過大小,最後一次啓動外循環。

步驟8:

設爲i = 2,J = 2,巴[I] = 7,酒吧[J] = 7

if(bar[j] < bar[i]) {...} // condition is not true so nothing happens 

你的數組: {1,4 ,7,6}

增加Ĵ由1

步驟9:

設爲i = 2,J = 3,巴[I] = 7,酒吧[J] = 6

if(bar[j] < bar[i]) // condition is true, if loop starts 
{ 
    temp = bar[j]; // bar[j] value is 6, temp value is now 6 
    bar[j] = bar[i]; // bar[i] value is 7, bar[j] value is now 7 
    bar[i] = temp; // temp value is 6, bar[i] value is now 6 
} 

觀察:巴[I] = 6,巴[J] = 7

你的數組: {1,4,6,7}

在下一步驟j超過大小值,從而內for循環場所及i值超過大小-1,因此歐特r用於循環中斷。程序已結束,現在數組值從最小值到最大值進行排序。

獎金:

如果你注意到了,總是有一步時等於Ĵ裏面你內心的循環,這一步是不必要的,因爲你要比較的數量數組本身。將內循環條件從for(j=i; j< size; j++)更改爲for(j=i+1; j< size; j++),並且步數將大大減少。

我希望你能理解Bubble Sort現在。隨意問你是否有其他問題。

0

由於其他答案已經告訴你的算法一步一步分析,所以沒有用的一步一步去。

簡而言之,它發現每個循環中的最小值範圍[i,n](通過比較每個元素在位置i處的值並將其中的最小值放置在位置i處),並且過程繼續直到最後一個元素。

因此,對於你的情況即:-6,4,7,1

第一迭代後輸出將是1,6,7,4(1分鐘在範圍I [1,4])

在第二次迭代後輸出將是1,4,7,6(4是範圍i中的最小值[2,4])

第三次迭代後輸出將是1,4,6,7(6是最小值in範圍我[1,4])

欲瞭解更多信息,你可以看到這種形式的可溶性分類http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html