Reputation: 1
I want to be able to take this quick sort and make it so that the odd numbers are listed in the original order and the even are listed in the original order, but with odd numbers first then even numbers.
Here is the initial quick sort program:
public static void bubbleSort(int[] list) { boolean needNextPass = true;
for (int b = 1; b < list.length && needNextPass; b++) {
needNextPass = false;
for (int i = 0; i < list.length - b; i++) {
if (list[i] > list [i + 1]) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
needNextPass = true;
}
}
}
}
public static void main(String[] args) {
int[] list = {10, 11, 12, 14, 9, 7, 8, 16, 6, 5, 4, 1, 3, 2, 14, 13, 16, 15, 17, 18};
bubbleSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
} I want to print out 11 9 7 5 1 3 13 15 17 10 12 14 8 16 6 4 2 14 16 18
instead of
1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 16 16 17 18
Upvotes: 0
Views: 918
Reputation: 1294
Your using the wrong tool for the job. You don't need a sort. You just need to to make one pass through the numbers and add them to 2 different lists.
Upvotes: 0
Reputation: 7486
The simplest thing to do would be to split the list into odd and even and then append the even list to the odd list. That'll be faster too - it's O(n) instead of O(n log n).
Upvotes: 0