The sort (sort) method of the lists module on the next Erlang code parsing (a), so far, the list lists have been divided into N lists, and the elements of each list are ordered (from large to small)
Below we will focus on the Mergel and Rmergel modules, because our previous main analysis of the split_1_* corresponds to Rmergel, we first view from Rmergel, as follows
.......................................................
Split_1 (X, Y, [], R, Rs),
Rmergel ([[Y, X | R] | Rs], []).
| R] | Rs], []) ... ..... ... ..... ... ..... ..... ..... ..... ..... ..... ..... ... .. ..... ..... .........
Rmergel code more, look at the fact that the idea is very clear,
1 Rmergel ([[H3 | T3], [H2 | T2], T1 | L], ACC),2 Rmergel (L, [Rmerge3_1 (T1, [], H2, T2, H3, T3) | ACC]); 3 Rmergel ([[H2 | T2], T1], ACC),4 Mergel ([Rmerge2_1 (T1, H2, T2, []) | ACC], []); 5 Rmergel ([l], ACC)-6 Mergel ([Lists:reverse (L, []) | ACC], []); 7 Rmergel ([], ACC),8 Mergel (ACC, []).
When the number of the list >=3 more than 3 to compare the implementation of the merger Rmerge3_1, the 3 lists into 1 ordered list (to complete the small to large); the rest follows this logic.
Of course the list =2 take 2 to compare merge rmerge2_1 Implementation, the 2 lists into 1 ordered list (complete from small to large)
When the list is only 1, the list is ordered.
According to this understanding, the degree of complexity should be log3n*n not previously understood N,
But what we can't understand here is why we have to compare 3 of them,
I follow the logic of 2 once to write, the code is much simpler, but the operation of the time is almost the original author's 1.5-2 times, I do not understand
I'm enclosing the logic code for 2 lists at a time.
My_rmerge ([h1,h2| T], R)My_rmerge (T, [My_rmerge2 (H1, H2, [])|R]); My_rmerge ([H1], R)-My_merge ([Lists:reverse (H1)|R], []); My_rmerge ([], [R])-R;my_rmerge ([], R)-My_merge (R, []). My_rmerge2 ([H1| t1],[h2| T2], List) when H2 >= H1([H1| T1], T2, [h2|List]); My_rmemy_rmerge2rge2 ([H1| t1],[h2| T2], List)My_rmerge2 (T1, [H2| T2], [h1|List]); My_rmerge2 ([], L2, List)-lists:reverse (L2, list); My_rmerge2 (L1, [], list)-Lists:reverse (L1, List). My_merge ([H1,h2| T], R)My_merge (T, [My_merge2 (H1, H2, [])|R]); My_merge ([H1], R)-My_rmerge ([Lists:reverse (H1)|R], []); My_merge ([], [R])-Lists:reverse (R); My_merge ([], R)-My_rmerge (R, []). My_merge2 ([H1| t1],[h2| T2], List) when H2 < H1My_merge2 ([H1| T1], T2, [h2|List]); My_merge2 ([H1| t1],[h2| T2], List)My_merge2 (T1, [H2| T2], [h1|List]); My_merge2 ([], L2, List)-lists:reverse (L2, list); My_merge2 (L1, [], list)-Lists:reverse (L1, List).
View comparison Results
1 the>TIMER:TC (TT1, Mysort, [B2]).2{48842,3[1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A,4 at, -, -, -, -|...]}5 the>TIMER:TC (TT1, Mysort, [B2]).6{53618,7[1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A,8 at, -, -, -, -|...]}9 the>TIMER:TC (lists, sort, [B2]).Ten{31179, One[1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A, A at, -, -, -, -|...]} - 98>TIMER:TC (lists, sort, [B2]). -{29326, the[1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A, - at, -, -, -, -|...]}
B2 is a 1 to 100000 chaotic list, why the difference is so big,
There is no great God to explain, according to such logic, if you take 4 at a time is not more block, code of course more ~ ~ ~
Erlang Lists module sort (sort) method source code Analysis (ii)