关于数组的一些小操作:

1.数组中奇数放左边,偶数放右边:

public class Test{
    public static void main(String [] args){
        int [] yuxin = {1,2,3,4,5,6,7,8,9,10} ;
        sortArray(yuxin); 
        for(int i : yuxin){
            System.out.print(i+" ");
        }
    }
    public static void sortArray(int [] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] % 2 == 0) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

}

统计字符数组中字符出现的次数:

关键: 定义一个整数数组 ,索引存放 遍历出的字符与97的差值,值为次数,这样做事为了去重;

public class Test {
    public static void main(String[] args) {

        char [] ch ={'a','j','c','a','j','i','p','w','a','t'};

        int [] result= getCount(ch);  //定义获得字符出现次数的方法,并用整数数组接收;
        printCountArray(result);   //打印在控制台


    }
    public static int [] getCount(char [] ch){
        int count[] =new int[26];  //定义一个整数数组,长度为26,对应存放各个字母出现的次数;
        for(int i= 0; i<ch.length ;i++){
            int temp=ch[i];
            count[temp-97]++;// 这步最关键,索引存放的是遍历的字符与 97的差值,这样是为了去重,
        }
        return count;
    }
    public static  void printCountArray(int [] count){
        for(int i =0, ch=97; i<count.length ; i++,ch++){
            if(count[i] != 0){   //ch 数组中没出现的,就不用打印了,
                System.out.println((char)(ch)+ "------ "+count[i]);
            }
        }
    }

选择排序:

public class Test {
    public static void main(String[] args) {

        int [] arr={12,3,45,16,7,19,12};
        for(int i=0 ; i<arr.length ;i++){
            for(int j=i+1; j<arr.length ;j++){
                if(arr[i]>arr[j]){
                    int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        for(int i : arr){
            System.out.print(i+" ");
        }
    }



}

冒泡排序:

public class Test {
    public static void main(String[] args) {

        int [] arr={12,3,45,16,7,19,12};
        for(int i=0 ;i<arr.length-1;i++){   //外循环控制次数
            for(int j=0 ; j<arr.length-1-i; j++){ //内循环进行排序; arr.length-1-x ;   -1是为了避免角标越界,-i 是位了减少内循环次数,提高效率
                if(arr[j]>arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for(int i : arr){
            System.out.print(i+" ");
        }
    }



}

二分查找(折半查找):二分查找的效率还行,就是有个前提,必须对要查找的数组进行排序:

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {

        int [] arr={8,9,45,16,7,19,12};
        Arrays.sort(arr);  //用Arrays工具类进行排序
        for(int i :arr){       //遍历数组,打印在控制台
            System.out.println(i+" ");
        }
        int key=7;  确定要找的值
        int index=erFenChaZhao(arr,key);
        System.out.println("index:"+index);  //打印获得的索引
    }
    public  static  int erFenChaZhao(int [] arr, int key){
        int start=0,end=arr.length-1,mid=(start+end)/2;  //定义 首,末,中间的三个索引
        while(arr[mid]!=key){  //如果不是要找的值,继续进行循环
            if(arr[mid]<key){   //如果要找的值大于中间值,末尾的索引不变,将首索引变为 mid+1 
                start=mid+1;
            }else if(arr[mid]>key){
                end=mid-1;       //如果小于中间值,首索引不变,将末尾索引变为 mid-1 
            }
            if(start>end){
                return -1;       //当首索引都大于末尾索引了,肯定是没找到,返回 -1;
            }
            mid=(start+end)/2;   //再次折半;
        }
        return mid;
    }

}

猜你喜欢

转载自blog.csdn.net/yuwotongle1018/article/details/81159102