This is what i have so far:
import java.util.*;
public class ProcessArray {
public static void main(String[] args) {
int[] arr = {3, 8, 1, 9, 2};
System.out.println(Arrays.toString(arr));
selectionSort(arr);
System.out.println(Arrays.toString(arr));
ProcessArray.oddArray(arr);
System.out.println(Arrays.toString(arr));
ProcessArray.evenArray(arr);
System.out.println(Arrays.toString(arr));
}
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = 0;
for (int k = 0; k < arr.length - i; k++) {
if (arr[k] > arr[index]) {
index = k;
}
}
//swap
int tmp = arr[index];
arr[index] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
public static int[] oddArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
arr[i] = arr[i];
}
}
return arr;
}
public static int[] evenArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
arr[i] = arr[i];
}
}
return arr;
}
}
For some reason when i go to run it, this is what im outputted with:
[3, 8, 1, 9, 2]
[1, 2, 3, 8, 9]
[1, 2, 3, 8, 9]
[1, 2, 3, 8, 9]
How do i make odd Array and even Array work? Because the logic make sense, at least to me it does, but the program still just prints the output it got from selectionSort. Im sorry if this basic computer science knowledge, we just recently started learning about sorting.
arr[i] = arr[i]won't change anything.public static int[] evenArray(int[] arr)to me means: return anewarray that only contains the even numbers of arr