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 }
#include <iostream>
void devide(int arr[], int len){
int l, r;
for(l = 0, r = 0; r <= len && l <= len; l++){
while(r <= len && (arr[r]%2 == 0)) r++; //寻找后方第一个奇数
if(r <= len) std::swap(arr[l], arr[r]); //与此处偶数交换,确保已遍历过的左方全部为奇
}
} 双指针
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]);
}
}
#include <stdio.h> #include <stdlib.h> #define bool int #define false 0 #define true 1 void Reorder(int *pData, unsigned int length, bool (*func)(int)); bool isEven(int n); void ReorderOddEven_1(int *pData, unsigned int length) { if(pData == NULL || length == 0) return; int *pBegin = pData; int *pEnd = pData + length - 1; while(pBegin < pEnd) { // 向后移动pBegin,直到它指向偶数 while(pBegin < pEnd && (*pBegin & 0x1) != 0) pBegin ++; // 向前移动pEnd,直到它指向奇数 while(pBegin < pEnd && (*pEnd & 0x1) == 0) pEnd --; if(pBegin < pEnd) { int temp = *pBegin; *pBegin = *pEnd; *pEnd = temp; } } } void Reorder(int *pData, unsigned int length, bool (*func)(int)) { if(pData == NULL || length == 0) return; int *pBegin = pData; int *pEnd = pData + length - 1; while(pBegin < pEnd) { //向后移动pBegin while(pBegin < pEnd &&!func(*pBegin)) pBegin ++; // 向前移动pEnd while(pBegin < pEnd &&func(*pEnd)) pEnd --; if(pBegin < pEnd) { int temp = *pBegin; *pBegin = *pEnd; *pEnd = temp; } } } bool isEven(int n) { return (n & 1) == 0; }