I have one sorted ArrayList A and one unsorted ArrayList B, now I want to combine the elements of B into A so that A remains sorted.
Now I can think of only two ways to do this.
The first is to sort Arraylist B, and then have two index positions, one for Arraylist A and the other for Araylist B, then we will move the index one by one to insert the list item B into A.
Assume that Arraylist A be the size nand size B Arraylist m.
n
m
The complexity order will be O(m Log(m))(to sort ArrayList B) + O(n + m).
O(m Log(m))
O(n + m)
The second approach has only an index in ArrayListaylist B, and then use Binary searchto place an element from Arraylist B to A.
Binary search
The order of complexity will be O(Log(n) * m).
O(Log(n) * m)
Now someone can tell me which approach I should choose, also if you can think of any other approach better than these two, please indicate.
It depends on the relative size nand m.
n > m*log(m) O(m*Log(m) + max(n,m)) n ( max(n,m)=n n > m*log(m)). O(log(n) * m) .
n > m*log(m)
O(m*Log(m) + max(n,m))
max(n,m)=n
m*log(m)
O(log(n) * m)
, , n m . , m n, .
:
, , , . , , ? , . , . , , , .
, , , O(m log(n)), .
O(m log(n))
1- : m * log (n) = O (mlgn): m * log (m) + n + m = O (mlgm)
1- : m * log (n) = O (mlgn)
: m * log (m) + n + m = O (mlgm)
if n <= m { 1st approach } else { 2nd approach }
Source: https://habr.com/ru/post/1688728/More articles:How to display the number of rows affected in sql text file - sqlPPA: 'ppa: ondrej / php5-5.6' FORBIDDEN 403 - phpCan a Java class be an object and an object a class? - javaandroid studio 3.0 stuck in "building gradle project information" - androidjson.loads () does not support ordering - jsonКак добавить имя держателя карты в "Проверка полосы" с помощью "Элементов"? - stripe-paymentsError: Cannot read property selection. Using jest-expo with native response - javascriptКак быстро писать большие двоичные данные в Java? - javaGetting iPhone X safe zone using JavaScript - javascriptThe scope of var and variables is scopeAll Articles