19 public static void oddAndEven (int [] nums) {
20 if (nums != null && nums.length != 0) {
21 for (int odd = 0, even = 0; even < nums.length; even ++) {
22 if (nums [even] % 2 != 0) {
23 int temp = nums [even];
24 nums [even] = nums [odd];
25 nums [odd] = temp;
26 odd ++;
27 }
28 }
29 }
30 }
双指针
public class frag{ public void frag(int[] nums) { int left = 0; int right = nums.length - 1; while (left < right) { while (left < right && (nums[left] & 1) == 1) left++; while (left < right && (nums[right] & 1) == 0) right--; if (left < right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; } } } }
#两个指针,一个从头开始,一个从尾开始,头指针遇到偶数则停下来,尾指针遇到奇数停下来,互相交换 arr=list(map(int,input().split())) low = 0 high = len(arr) - 1 while True: while arr[low] % 2 != 0 and low < high: low += 1 while arr[high] % 2 != 1 and low < high: high -= 1 if low == high: break arr[low], arr[high] = arr[high], arr[low] for i in arr: int(i) print (i,end=' ')
intSwapArray(int*a, intlen){int*first = a, *last = a + len - 1;while(first < last) {while((*first & 1) && first < last) ++first;while(!(*last & 1) && first < last) --last;int t = *first; *first++ = *last; *last-- = t;}return0;}
#include <iostream> using namespace std; bool sort_arr(int *a,int len) { if(a==NULL || len <1) return false; int tmp=a[0]; int left=0; int right=len-1; while(left<right) { while(left<right && a[right]&1 != 0) right--; if(left<right) a[left++]=a[right]; while(left<right && a[left]&1 == 1) left++; if(left<right) a[right--]=a[left]; } a[left]=tmp; return true; } void print_arr(const int *p,int len) { if(p==NULL || len <1) return; for(int i=0;i<len;i++) { cout<<p[i]<<" "; } cout<<endl; } int main() { int arr[]={1,2,3,4,5,6,7,8,9,10}; int len=sizeof(arr)/sizeof(arr[0]); print_arr(arr,len); sort_arr(arr,len); print_arr(arr,len); return 0; }
void changeArr(int *b, int size) { int i=0, j=size-1; while(i<j) { if(b[i]%2!=0) { i++; } if(b[j]%2==0) { j--; } if(i<j) { if(b[i]%2==0 && b[j]%2!=0) { b[i] = b[i] +b[j]; b[j] = b[i] - b[j]; b[i] = b[i] - b[j]; } } } }
class Solution(): def divide(self, nums): start = 0 end = len(nums) - 1 while start < end: while start < end and nums[start] % 2 == 1: start += 1 while end > start and nums[end] % 2 == 0: end -= 1 temp = nums[end] nums[end] = nums[start] nums[start] = temp return nums
public static void px(int[] nub){ int j=nub.length-1;//用于记录偶数的最左位 int temp = 0; f: for(int i = 0 ; i < nub.length ; i ++){ if((nub[i]&1)==0){//如果nub[i]是偶数 while((nub[j]&1)==0){//当要交换的数是偶数时候 if(i==j){ break f; } j--; } temp = nub[j]; nub[j]=nub[i]; nub[i]=temp; j--; } if(i==j){ break; } } for(int i = 0 ;i<nub.length ; i++){ System.out.println("nub["+i+"]="+nub[i]); } }
package Test; public class Test3 { /** * 算法:计数器为0,从第一个开始判断,若是奇数,就与计数器指定位置交换数据,计数器加1;若是偶数,则不作处理。 * @param v */ public void fun(int[] v) { for(int i = 0,j=0;i < v.length; i++) { if(v[i] % 2 != 0) { swap(v, i, j); j++; } } } public void swap(int[] v, int i, int j) { int tmp = v[i]; v[i] = v[j]; v[j] = tmp; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; Test3 test = new Test3(); test.fun(v); for(int i = 0; i < v.length; i++) { System.out.print(v[i]+" "); } } }