欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的深入分析

 更新時(shí)間:2013年06月04日 17:12:57   作者:  
本篇文章是對(duì)數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
這里介紹一種高效的能在O(n)時(shí)間復(fù)雜度內(nèi)完成的算法。
核心思想是:定義兩個(gè)指針,一個(gè)指針A從前往后掃描,一個(gè)指針B從后往前掃描。指針A掃描到偶數(shù)暫停,指針B掃描到奇數(shù)暫停,然后交換著兩個(gè)數(shù),交換之后繼續(xù)如上述掃描和交換,直到指針A和指針B重合停止。
這個(gè)算法的Java代碼如下:
復(fù)制代碼 代碼如下:

package Reorder;
public class Reorder {

 public static void main(String[] args) {
  int[] list = { 1, 2, 3, 4, 5, 7, 9, 11 };
  reorderOddEven(list);
 }
 public static void reorderOddEven(int[] list) {
  int length = list.length;
  for (int i = 0; i < length; i++) {
   System.out.print(list[i] + " ");
  }
  System.out.print("\n");
  int begin = 0;
  int end = length - 1;
  while (begin < end) {
   while (begin < end && (list[begin] & 0x1) != 0)
    begin++;
   while (begin < end && (list[end] & 0x1) == 0)
    end--;
   if (begin < end) {
    int temp = list[begin];
    list[begin] = list[end];
    list[end] = temp;
   }
  }
  for (int i = 0; i < length; i++) {
   System.out.print(list[i] + " ");
  }
 }
}

相關(guān)文章

  • OpenCV實(shí)現(xiàn)反閾值二值化

    OpenCV實(shí)現(xiàn)反閾值二值化

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)反閾值二值化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 最新評(píng)論