学习java数据结构和算法笔记(一)

package com.example.demo;


/**
 * 
 * <P>Description: 排序</P>
 * @ClassName: List
 * @author 冯浩  2018年4月19日 下午3:16:39
 * @see TODO
 */
public class List {
    
    private volatile int size;
    
    private Long[] array= {};
    
    public List(int max) {
        array=new Long[max];
        size=0;
    }
    
    public int find(Long key) {
        int up=size-1;
        int low=0;
        int model;
        while(true) {
            model=(up+low)/2;
            if(array[model]==key) {
                return model;
            }else if(low>up) {
                return size;
            }else {
               if(array[model]>key) {
                   up=model-1;
               }else {
                   low=model+1;
               }    
            }
            
        }
        
    }
    public void insert(Long value) {
        int i;
        for ( i = 0; i < size; i++) {
            if(array[i]>value) {
                break;
            }
        }
        for(int j=size;j>i;j--) {
            array[j]=array[j-1];
        }
        array[i]=value;
        size++;
    }
    public boolean delete(Long key) {
        int find = find(key);
        if(find==size) {
            return false;
        }
        for (int i = find; i < size; i++) {
            array[i]=array[i+1];
        }
        size--;
        return true;
        
    }
    
    
    /**
     * 
     * <p>Title: dubboSort</p>
     * <p>Description: 冒泡排序</p>
     * 2,1,3,4,7,8,1
     * 每次循环之后,本地循环最后一个值到数组的最后一个值是有序的
     * @return
     * @author 冯浩  2018年4月19日 下午3:29:01
     */
    public Long[] dubboSort() {
        for(int i=size-1;i<size;i--) {
            for(int j=0;j<i;j++) {
                if(array[j]>array[j+1]) {
                    long tem=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tem;
                }
            }
        }
        return array;
    }
    
    /**
     * 
     * <p>Title: choseSort</p>
     * <p>Description: 选择排序</p>
     * 选择排序--需要一个基数,和数组中的其他数据做比较,比较之后,前面的数据是有序数据
     * @return
     * @author 冯浩  2018年4月19日 下午5:02:57
     */
    public Long[] choseSort() {
        int min;
        for (int i = 0; i < size; i++) {
            min=i;
            for (int j = i+1; j <size; j++) {
                if(array[min]>array[j]) {
                    long tem = array[min];
                    array[min]=array[j];
                    array[j]=tem;
                }
            }
        }
        return array;
    }
    
    
    public Long[] insertSort() {
        
        for (int i = 1; i < size; i++) {
            long tem=array[i];
            while(i>0 && tem<array[i-1]) {
                array[i]=array[i-1];
                --i;
            }
            array[i]=tem;
        }
        return array;
    }
    public static void main(String[] args) {
        List list=new List(10);
        list.insert(1l);
        list.insert(2l);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        list.insert(3l);
        int find = list.find(2l);
//        System.out.println(find);
//        Long[] dubboSort = list.dubboSort();
//        Long[] dubboSort = list.choseSort();
        Long[] dubboSort = list.insertSort();
        for (Long long1 : dubboSort) {
            if(long1!=null) {
                System.out.println(long1);
            }
        }
        
    }

}

 

posted @ 2018-04-20 11:26  默默行走  阅读(171)  评论(0编辑  收藏  举报