→ jokester:"合併"是哪種操作? intersection? union?114.160.120.147 01/22 17:21
→ jokester:這是c代碼嗎? 怎會返回一個棧上的指針@@114.160.120.147 01/22 17:22
是union
其實我程式底子沒很好....以上那些是虛擬碼,我的用意是回傳C陣列給主程式..
※ 編輯: flipsydee 來自: 60.244.44.20 (01/22 17:25)
對不起,我好像發現問題了,既然是兩個set的union操作,應該要排除共同
的元素才是。那我以上的寫法都寫錯了...
※ 編輯: flipsydee 來自: 60.244.44.20 (01/22 17:28)
※ 編輯: flipsydee 來自: 60.244.44.20 (01/22 17:28)
推 jokester:哈@@ 抱歉我看太快, 沒有看到是虛擬碼114.160.120.147 01/22 17:32
→ jokester:可以把AB分別排序 再匯合到C114.160.120.147 01/22 17:34
推 jokester:兩次排序和一次merge, 結果會有O(MlogM)114.160.120.147 01/22 17:36
推 suhorng:這個merge沒處理重複的狀況... 118.166.51.235 01/22 21:36
我又寫了一個...網路上搜尋set union algorithm找到的資料很少...
只好自己寫了一個,可是很難看
版本一:為了省時間用線性時間排序與二分搜尋法
https://github.com/vacuumv/coding1/blob/master/test.c
版本二:網路上有人說可以用hashing table的想法去寫,又沒給code
所以我也自作聰明的寫了一個
https://github.com/vacuumv/coding1/blob/master/test2.c
小弟的程式概念很薄弱,還望大家不吝賜教......有錯的很誇張的觀念
也請多多包涵...
我只是在想這種常見的問題應該會有固定解法阿(最好的演算法)...有人知道嗎?
※ 編輯: flipsydee 來自: 60.244.44.20 (01/22 22:11)
推 suhorng:因為比較多是不限定用什麼實作set吧 118.166.51.235 01/22 22:28
→ suhorng:例如用hash table代表set, 然後去討論各種 118.166.51.235 01/22 22:28
→ suhorng:操作的時間複雜度之類. 118.166.51.235 01/22 22:28
推 singlovesong:沒仔細看題目 不過set union不是課 140.109.16.164 01/23 09:26
→ singlovesong:本裡面有最佳算法嗎 140.109.16.164 01/23 09:26
→ singlovesong:path compression union by rank? 140.109.16.164 01/23 09:26
那個好像是disjoint set的union才適用喔..
現在題目給的兩個set 可能非disjoint..
其實這是台大資管所102年計算機概論的題目~貼上原文,大家可以思考一下!
Design an algorithm that, given two sets of numbers, computes the union of
the two sets.
The first set of m numbers is given as an array A in no particular order and
the second set of n numbers as another array B also in no particular order.
The result should be represented as a third array C where no particular
ordering is required.
It is known that m is much larger than n, approximately in the order of n^2.
Please describe your algorithm in suitable pseudo code and analyze its time
complexity.
The more efficient your algorithm is, the more points you will be credited
for this problem.(15%)
※ 編輯: flipsydee 來自: 60.244.44.20 (01/23 11:14)
推 yvb:test1.c 的 radixsort(), 註解的 O(1) 是?? 118.168.219.47 01/24 13:07
→ yvb:test2.c 若 M=100, N=10, A[0]=1000 會怎樣? 118.168.219.47 01/24 13:09
推 singlovesong:最慘就Mlog(M)吧, balanced tree硬做140.109.135.106 01/24 15:09
→ singlovesong:題目這樣出應該是要想Mlog(N)的解140.109.135.106 01/24 15:10