Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static ArrayList<Integer> merge_one_into_another(ArrayList<Integer> arr1, ArrayList<Integer> arr2) {
- // moving all numbers in arr2 to the second half
- for(int i = 0; i < arr1.size(); i++)
- {
- Collections.swap(arr2, i, arr1.size() + i);
- }
- int i = 0;
- int j = arr1.size();
- int idx = 0;
- // sorting from left side
- while(i < arr1.size() && j < arr2.size()) {
- if(arr1.get(i) <= arr2.get(j)) {
- arr2.set(idx, arr1.get(i));
- arr1.set(i, 0);
- i++;
- idx++;
- } else {
- arr2.set(idx, arr2.get(j));
- arr2.set(j, 0);
- j++;
- idx++;
- }
- }
- // put remaining elements in arr1 to arr2
- while(i < arr1.size())
- {
- arr2.set(idx, arr1.get(i));
- arr1.set(i, 0);
- i++;
- idx++;
- }
- return arr2;
- }
Add Comment
Please, Sign In to add comment