zoukankan      html  css  js  c++  java
  • 013 调整数组顺序使奇数位于偶数前面

    1.题目

      输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

    2.第一种方式思路

      这里最常见的就是弄一个新的数组

    3.程序

     1  /**
     2      * 新建一个新的数组
     3      * @param array
     4      */
     5     public static void reOrderArray(int [] array) {
     6         if(array.length==0||array.length==1) return;
     7         int oddCount=0,oddBegin=0;
     8         int[] newArray=new int[array.length];
     9         for(int i=0;i<array.length;i++){
    10             if((array[i]&1)==1) oddCount++;
    11         }
    12         for(int i=0;i<array.length;i++){
    13             if((array[i]&1)==1) newArray[oddBegin++]=array[i];
    14             else newArray[oddCount++]=array[i];
    15         }
    16         for(int i=0;i<array.length;i++){
    17             array[i]=newArray[i];
    18         }
    19         System.out.print(Arrays.toString(array));
    20     }

    4.程序二思路分析

      相对位置不变--->保持稳定性;奇数位于前面,偶数位于后面 --->存在判断,挪动元素位置;

      这些都和内部排序算法相似,考虑到具有稳定性的排序算法不多,例如插入排序,归并排序等;这里采用插入排序的思想实现。
     
    5.程序
     1 public static void reOrderArray2(int [] array) {
     2         //相对位置不变,稳定性
     3         //插入排序的思想
     4         int m = array.length;
     5         int k = 0;//记录已经摆好位置的奇数的个数
     6         for (int i = 0; i < m; i++) {
     7             if (array[i] % 2 == 1) {
     8                 int j = i;
     9                 while (j > k) {         //j >= k+1
    10                     int tmp = array[j];
    11                     array[j] = array[j-1];
    12                     array[j-1] = tmp;
    13                     j--;
    14                 }
    15                 k++;
    16             }
    17         }
    18         System.out.print(Arrays.toString(array));
    19     }

    6.全部程序

     1 package first;
     2 
     3 import java.util.Arrays;
     4 
     5 public class ReOrderArray {
     6     public static void main(String[] args){
     7         int[] arr={1,3,4,5,6,7};
     8         reOrderArray2(arr);
     9     }
    10 
    11     /**
    12      * 新建一个新的数组
    13      * @param array
    14      */
    15     public static void reOrderArray(int [] array) {
    16         if(array.length==0||array.length==1) return;
    17         int oddCount=0,oddBegin=0;
    18         int[] newArray=new int[array.length];
    19         for(int i=0;i<array.length;i++){
    20             if((array[i]&1)==1) oddCount++;
    21         }
    22         for(int i=0;i<array.length;i++){
    23             if((array[i]&1)==1) newArray[oddBegin++]=array[i];
    24             else newArray[oddCount++]=array[i];
    25         }
    26         for(int i=0;i<array.length;i++){
    27             array[i]=newArray[i];
    28         }
    29         System.out.print(Arrays.toString(array));
    30     }
    31 
    32 
    33     public static void reOrderArray2(int [] array) {
    34         //相对位置不变,稳定性
    35         //插入排序的思想
    36         int m = array.length;
    37         int k = 0;//记录已经摆好位置的奇数的个数
    38         for (int i = 0; i < m; i++) {
    39             if (array[i] % 2 == 1) {
    40                 int j = i;
    41                 while (j > k) {         //j >= k+1
    42                     int tmp = array[j];
    43                     array[j] = array[j-1];
    44                     array[j-1] = tmp;
    45                     j--;
    46                 }
    47                 k++;
    48             }
    49         }
    50         System.out.print(Arrays.toString(array));
    51     }
    52 }
     
  • 相关阅读:
    JAVA网络编程-IP组播
    Centos7安装Node
    Android Studio解决support-annotations版本冲突
    Wordpresss建站笔记:英文模板出现中文如何解决?
    Win10系统下插入耳机前面板无声后面板有声的处理(二)
    webstorm编辑器html浏览器快捷浏览按键图标消失的处理
    近期的感想
    Octet string 解析
    SSH隧道:端口转发功能详解
    uint, not []uint8
  • 原文地址:https://www.cnblogs.com/juncaoit/p/10424852.html
Copyright © 2011-2022 走看看