Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static ArrayList<Integer> segregate_evens_and_odds(ArrayList<Integer> numbers) {
- // Write your code here.
- segregate_evens_and_odds_helper(numbers, 0, numbers.size()-1); // pass index only
- System.out.println(numbers);
- return numbers;
- }
- static void segregate_evens_and_odds_helper(ArrayList<Integer> numbers, int start, int end) {
- // Base Case
- if(start >= end) {
- return;
- }
- //Recursive Case
- int odd = start;
- for(int even = start; even <= end-1; even++) {
- if(isEven(numbers.get(even))) {
- Collections.swap(numbers, odd, even) ;
- odd ++;
- }
- }
- Collections.swap(numbers, odd, end);
- }
- static boolean isEven(int number) {
- if(number%2 == 0)
- return true;
- return false;
- }
Add Comment
Please, Sign In to add comment