交换数组中的奇数和偶数
Swap odd and even numbers in array
我在这个网站上看到了这段代码。它使用一种方法对数组进行排序,偶数排在前面,奇数排在数组后面。我想知道你是否可以做同样的事情,只是让奇数先出现,然后偶数出现?我试过了但无济于事。我是 java 编码的新手,我想测试递归。
public class Recurse {
//i=left
//j=right
//first i tried to shift the whole thing
//have all even numbers pop to the front of array when even
public static int[] seperator(int[] arr1, int left, int right){
int temp;
if(left>=right) //base case, return array
return arr1;
else if(arr1[left]%2!=0 && arr1[right]%2==0){//if match, do the swap
temp=arr1[left];
arr1[left]=arr1[right];
arr1[right]=temp;
return seperator(arr1, left+1, right-1);
}
else{
if(arr1[right]%2!=0){//if right side is on odd #, then decrease index
return seperator(arr1, left, right-1);
}
if(arr1[left]%2==0){//if left side is on even #, then increase index
return seperator(arr1, left+1, right);
}
}
return arr1;
}
public static void main(String[] args){
//int index=0;
int[] arry={70,13,48,19,24,5,7,10};
int[] newarry=seperator(arry, 0, arry.length-1);
System.out.print("The new sorted array is: ");
for(int i=0; i<newarry.length;i++){
System.out.print(newarry[i]+" ");
}
}
}
输出是这样的:
The new sorted array is: 70 10 48 24 19 5 7 13
好吧,如果您希望奇数排在第一位,请将任何 %2!=0
替换为 %2==0
并将任何 %2==0
替换为 %2!=0
:
public static int[] seperator(int[] arr1, int left, int right){
int temp;
if(left>=right)
return arr1;
else if(arr1[left]%2==0 && arr1[right]%2!=0){
temp=arr1[left];
arr1[left]=arr1[right];
arr1[right]=temp;
return seperator(arr1, left+1, right-1);
}
else{
if(arr1[right]%2==0){//if right side is on even #, then decrease index
return seperator(arr1, left, right-1);
}
if(arr1[left]%2!=0){//if left side is on odd #, then increase index
return seperator(arr1, left+1, right);
}
}
return arr1;
}
试试这样的东西:
public static int[] seperator(int[] arr1, int left, int right) {
int temp;
if (left >= right) // base case, return array
return arr1;
else if (arr1[left] % 2 == 0 && arr1[right] % 2 != 0) {// if match, do the swap
temp = arr1[left];
arr1[left] = arr1[right];
arr1[right] = temp;
return seperator(arr1, left + 1, right - 1);
} else {
if (arr1[right] % 2 == 0) {// if right side is on odd #, then decrease index
return seperator(arr1, left, right - 1);
}
if (arr1[left] % 2 != 0) {// if left side is on even #, then increase index
return seperator(arr1, left + 1, right);
}
}
return arr1;
}
Output:
The new sorted array is: 7 13 5 19 24 48 70 10
public class Program {
public static void main(String[] args) {
int a[] = { 2, 3, 5, 6, 7, 8, 10, 46, 50 };
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0 ; j < a.length-1; j++) {
if (a[j] % 2 != 0) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
我在这个网站上看到了这段代码。它使用一种方法对数组进行排序,偶数排在前面,奇数排在数组后面。我想知道你是否可以做同样的事情,只是让奇数先出现,然后偶数出现?我试过了但无济于事。我是 java 编码的新手,我想测试递归。
public class Recurse {
//i=left
//j=right
//first i tried to shift the whole thing
//have all even numbers pop to the front of array when even
public static int[] seperator(int[] arr1, int left, int right){
int temp;
if(left>=right) //base case, return array
return arr1;
else if(arr1[left]%2!=0 && arr1[right]%2==0){//if match, do the swap
temp=arr1[left];
arr1[left]=arr1[right];
arr1[right]=temp;
return seperator(arr1, left+1, right-1);
}
else{
if(arr1[right]%2!=0){//if right side is on odd #, then decrease index
return seperator(arr1, left, right-1);
}
if(arr1[left]%2==0){//if left side is on even #, then increase index
return seperator(arr1, left+1, right);
}
}
return arr1;
}
public static void main(String[] args){
//int index=0;
int[] arry={70,13,48,19,24,5,7,10};
int[] newarry=seperator(arry, 0, arry.length-1);
System.out.print("The new sorted array is: ");
for(int i=0; i<newarry.length;i++){
System.out.print(newarry[i]+" ");
}
}
}
输出是这样的:
The new sorted array is: 70 10 48 24 19 5 7 13
好吧,如果您希望奇数排在第一位,请将任何 %2!=0
替换为 %2==0
并将任何 %2==0
替换为 %2!=0
:
public static int[] seperator(int[] arr1, int left, int right){
int temp;
if(left>=right)
return arr1;
else if(arr1[left]%2==0 && arr1[right]%2!=0){
temp=arr1[left];
arr1[left]=arr1[right];
arr1[right]=temp;
return seperator(arr1, left+1, right-1);
}
else{
if(arr1[right]%2==0){//if right side is on even #, then decrease index
return seperator(arr1, left, right-1);
}
if(arr1[left]%2!=0){//if left side is on odd #, then increase index
return seperator(arr1, left+1, right);
}
}
return arr1;
}
试试这样的东西:
public static int[] seperator(int[] arr1, int left, int right) {
int temp;
if (left >= right) // base case, return array
return arr1;
else if (arr1[left] % 2 == 0 && arr1[right] % 2 != 0) {// if match, do the swap
temp = arr1[left];
arr1[left] = arr1[right];
arr1[right] = temp;
return seperator(arr1, left + 1, right - 1);
} else {
if (arr1[right] % 2 == 0) {// if right side is on odd #, then decrease index
return seperator(arr1, left, right - 1);
}
if (arr1[left] % 2 != 0) {// if left side is on even #, then increase index
return seperator(arr1, left + 1, right);
}
}
return arr1;
}
Output:
The new sorted array is: 7 13 5 19 24 48 70 10
public class Program {
public static void main(String[] args) {
int a[] = { 2, 3, 5, 6, 7, 8, 10, 46, 50 };
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0 ; j < a.length-1; j++) {
if (a[j] % 2 != 0) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}