所以,我不知道什麼是合併排序是應該做的,我有點現在可以進行可視化。遞歸分割,直到只有一個元件被留在陣列中,由於一個元件的陣列已經排序,它減少了所需要的每一遞歸的工作量,並附加一個已排序陣列到另一陣列比至少很多計算密集按正常迭代排序。歸併排序算法不正確合併
我有兩個主要問題現在。一,關於分裂(這可能是一個簡單的,但它會拋出一切,如果不是固定的),當我通過低 - >中(+/- 1)和中 - >高分裂他們時,我遇到了問題基礎案例未正確測試並過早返回,導致未排序的數組。舉個例子,當我引用我的回答從另一個論壇,「如果我有喜9和低的0 5中間,然後我不得不要麼是正確的,從0分裂,5和6人至9日,或者右邊0到4,左邊5到9.我遇到的問題是,如果你再次分割它,比如從6到9,由於四捨五入,我有一個7的中間值,這意味着右邊只有6到7 ,因爲7 - 6等於1並且小於2,所以它在高 - > 2的情況下失敗,留下2個可能未分類的元素。「現在
,這種情況無論哪種方式,如添加到+/-中旬要麼會導致一種奇怪的低數量,並不能很好的工作。我如何解決這個問題?第二,當談到合併時,我如何正確(有效地)檢查B和C的數組邊界是否合適。我需要另一個條件語句來檢查POSB和POSC是否是在邊界,如果一個不是,我怎麼能適當地(整齊)上留下什麼其他的陣列到陣列的最後添加。
在此先感謝。這應該是在視覺基礎上,但現在僞代碼似乎是解決這些問題的最好方法,而不是強調正確的語法。
A[] = { 28, 39, 48, 27, 9, 88, 11, 4, 7} ' Global variable, disregard bad programming practices
Function Split(int low, int hi){ ' Adding brackets because not only am I used to java, it should help readability
if (hi - low) < 2 then
return array of elements from [low, hi]
end if
mid = (hi + low)/2
B[] = split(low, mid-1) ' Either I do mid-1 or mid+1, the results seem the same
C[] = split(mid, hi) ' Same problems as above
D[] = Merge(B[], C[])
return D[]
End Function
Function Merge(B[], C[]) ' I use two arrays because I figured it'd be the easiest to work with.
int PosB = 0, PosC = 0, PosMax = (b.length + c.length) -1 ' PosB and PosC keep track of the positions in the cells of B and C respectively.
'PosMax keeps track of the max cell that E[] (declared below) should have as well as the max number for the for loop
E[num] ' Declare and iniatialize an array that is supposed to hold the combined values of B and C, sorted.
for i = 0 to num
if PosC >= c.length or B[PosB] < C[PosC] ' Checks if C[] already has added everything it has to E[] and if so, proceeds to add the
' supposedly already sorted array B[]'s elements to E[]. Emphasis on "supposedly". A problem here is it does not check for if PosB
' is out of bounds, which is a huge problem with primitive arrays. Also checks if the current element in C is greater than the
' current element in B, and if so, add the element in B and increment.
E[i] = B[PosB]
PosB += 1 ' Does not check whether or not PosB is at the end, gotta figure a way to check
Else
E[i] = C[PosC]
PosC += 1
End If
Next
End Function
謝謝橡皮鴨爲急需編輯,我很感激。 – 2015-02-05 21:22:48