java单例模式等一些程序的写法....持续更新...

一、单例模式的写法:

public class MyFactory {
    /**
     * 饿汉式
     */
    private static MyFactory instance = new MyFactory();
    
    private MyFactory(){
        
    }
    
    public static MyFactory getInstance(){
        return instance;
    }
}
public class MyFactory {
    
    /**
     * 懒汉式(要注意处理线程安全问题)
     */
    private static MyFactory instance;
    
    public static MyFactory getInstance(){
        if(instance == null){
            synchronized(MyFactory.class){
                if(instance == null){
                    instance = new MyFactory();
                }
            }
        }
        
        return instance;
    }
}

 二、选择排序算法:

package com.cy.array;

public class selectionSort {
    /**
     * 外层循环每遍历一次,就把最小的数选出来。
     * 这是选择排序算法。
     * @param args
     */
    public static void main(String[] args) {
        int a[] = {2, 4, 6, 7, 3, 5, 1, 9, 8};
        
        for(int i=0; i<a.length; i++){
            for(int j=i+1; j<a.length; j++){
                if(a[j] < a[i]){
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
    }
    
}

上面的选择排序算法有待改进的空间,因为:

4 和 3 这次的交换没必要;

应该是:找到比4小的3,然后拿3后面的数和3进行比较,如果还找到比3小的2,那么再拿2后面的数和2比较..,不断找到最小的那个数,

然后直接拿最小的那个数和4交换就完了。

程序改写:

package com.cy.array;

public class selectionSort2 {
    public static void main(String[] args) {
        int a[] = {2, 4, 6, 7, 3, 5, 1, 9, 8};
        
        /**
         * 用k记录最小数的位置;
         * 假设第一次找到最小数的位置就是i;
         * 如果k位置后面的a[j]比a[k]还小,就更新k的位置;
         * 
         * 做完内层循环之后,如果找到最小位置k 不是 刚开始定义的位置i,发生一次交换
         */
        for(int i=0; i<a.length; i++){
            int k = i;
            for(int j=k+1; j<a.length; j++){
                if(a[j] < a[k]){
                    k = j;
                }
            }
            
            if(k != i){
                int temp = a[i];
                a[i] = a[k];
                a[k] = temp;
            }
            
        }
        
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
    }
    
}

 

三、冒牌排序算法:

package com.cy.array;

public class BubbleSort {
    public static void main(String[] args) {
        int a[] = {2, 4, 6, 7, 3, 5, 1, 9, 8};
        
        /**
         * 
         * 
         * 下标值只能到长度-1,所以i=a.length-1;
         */
        for(int i=a.length-1; i>=1; i--){
            for(int j=0; j<=i-1; j++){
                if(a[j] > a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
    }
}

 四、二分法查找:

package com.cy.array;

public class BinarySearch {
    public static void main(String[] args) {
        int a[] = {1, 3, 6, 8, 9, 10, 12, 18, 20, 34};
        int i = 12;
        
        //查找i=12,在a数组中的位置
        System.out.println(binarySearch(a, i));
    }
    
    /**
     * 二分法查找算法
     * 搜索一般是建立在排好序的基础之上;
     * 二分法查找, 思路:
     *     把数组排好序,将被查找数与数组中间位置的数进行比较;
     *  如果 = 中间位置的数,返回该中间位置;
     *  如果 > 中间位置的数,继续从右半边部分开始找..
     *  如果 < 中间位置的数,继续从左半边部分开始找..
     * @param a
     * @param num
     * @return
     */
    public static int binarySearch(int a[], int num){
        if(a.length == 0){
            return -1;
        }
        
        int start = 0;
        int end = a.length - 1;
        int m = (start + end) / 2 ;
        
        while(start <= end){
            if(num == a[m]){
                return m;
            }else if(num < a[m]){
                end = m - 1;
            }else if(num > a[m]){
                start = m + 1;
            }
            
            m = (start + end) / 2;
        }
        
        
        return -1;
    }
}

 

 

 

 

 

 

 

 

 

 

 

--------------

posted on 2017-06-21 00:16  有点懒惰的大青年  阅读(253)  评论(0编辑  收藏  举报