4.14
借用了一下快排的思想
public class Solution {
/**
* @param nums: an array of integers
* @return: nothing
*/
public static void partitionArray(int[] nums) {
if(nums.length <=1){
return;// write your code here;
}
int low = 0;
int height = nums.length-1;
int key = nums[low];
while(low < height){
while(low < height && nums[height]%2 == 0){
height --;
}
nums[low] = nums[height];
while(low < height && nums[low]%2 != 0){
low ++;
}
nums[height] = nums[low];
}
nums[low] = key;
}
}
本文介绍了一种基于快速排序思想实现的数组分区算法。该算法将数组中的元素按照奇偶性进行划分,所有偶数元素置于数组左侧,奇数元素置于右侧。通过此方法,实现了对整型数组的有效重新组织。
1177

被折叠的 条评论
为什么被折叠?



