java有序数组和无序数组

描述:

线性表是其组成元素之间具有线性关系的一种线性结构。在线性表中的任意位置都可以进行插入操作和删除操作。可以采用顺序存储结构链式存储结构表示线性表。数组是实现顺序存储的基础。在程序设计语言中,数组是一种构造数据类型。

                                       

 时间复杂度表:

  增加 查找 删除 更改
有序数组 o(n) o(logn) o(n) o(n)
无序数组 o(1) o(n) o(n) o(n)

所以总结下来:无序数组增加元素很方便,直接在末尾添加就行(保证数组长度足够大);有序数组查找元素相对方便。

无序数组的基本操作相关代码:

package per.data.structure;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 无序数组 2018-9-4
 */
public class UnorderedArray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//声明一个20000长度的整形数组
		int[] a = new int[2000000];

		//数组a初始化10000个数
		a = randSort(1, 1000000);
		
		//向数组中中添加一个数
		insert(5055, a);;
		
		//在数组中查询一个数
		select(500,a);

		//在数组中删除一个数
		delete(888, a);
		
		//在数组中更改一个数
		update(1000, 6, a);
		
	}

	/**
	 * 利用set生成无重复元素无序随机数组
	 * 
	 * @param min
	 *            整数数组的最小值
	 * @param max
	 *            整数数组的最大值
	 * @return
	 */
	public static int[] randSort(int min, int max) {
		
		int[] arr = new int[2000000];
		
		for (int i = 0; i < max; i++) {
			arr[i] = min + i;
		}
		Random random = new Random();
		Set<Float> set = new HashSet<>();
		for (int i : arr) {
			set.add(i + random.nextFloat());
		}
		int j = 0;
		for (Float float1 : set) {
			arr[j] = (int) float1.floatValue();
			j++;
		}
		return arr;
	}
	
	/**
	 * 无序数组在末尾插入一个数
	 * @param t
	 * @param array
	 */
	public static void insert(int t,int[] array){
		
		Long t1 = System.currentTimeMillis();

		array[10000] = t;
		
		Long t2 = System.currentTimeMillis();
		
		System.out.println("成功在末尾插入一个数,用时:"+(t2-t1)+"ms");
	}
	
	/**
	 *无序数组查询
	 *
	 */
	public static void select(int t,int[] array){
		
		Long t1 = System.currentTimeMillis();
		
		int i = 0;
		
		for( ; i < array.length ; i++){
			if (array[i] == t) {
				Long t2 = System.currentTimeMillis();
				
				System.out.println("在数组中找到了这个数,用时:"+(t2-t1)+"ms");
				return;
			}
		}
		
		Long t2 = System.currentTimeMillis();
		System.out.println("没有找到该数,用时:"+(t2-t1)+"ms");
		
	}
	
	/**
	 * 无序数组删除
	 * 
	 */
	public static void delete(int t,int[] array){
		
		Long t1 = System.currentTimeMillis();
		
		int i = 0;
		
		for( ; i < array.length ; i++){
			if (array[i] == t) {
				for(int k = i; k < array.length ; k++){
					if(k == array.length-1){
						array[k] = 0;
					}else{
						array[k] = array[k+1];
					}
				}
				Long t2 = System.currentTimeMillis();
				
				System.out.println("成功删除该数,用时:"+(t2-t1)+"ms");
				return;
			}
		}
		
		
		Long t2 = System.currentTimeMillis();
		System.out.println("没有找到该数,用时:"+(t2-t1)+"ms");
		
	}
	
	
	/**
	 * 无序数组更改
	 * @param old
	 * @param new1
	 * @param array
	 */
	public static void update(int old,int new1,int[] array){
		Long t1 = System.currentTimeMillis();
		
		int i = 0;
		
		for( ; i < array.length ; i++){
			if (array[i] == old) {
				array[i] = new1;
				Long t2 = System.currentTimeMillis();
				
				System.out.println("成功更改该数,用时:"+(t2-t1)+"ms");
				return;
			}
		}
		
		Long t2 = System.currentTimeMillis();
		System.out.println("没有找到该数,用时:"+(t2-t1)+"ms");
		
	}

}

有序数组的基本操作代码:

/**
*
*增加
*
*/
public boolean insert(int value){

        if(nElems>=initSize){ 
            return false;
        }

        int index = 0; 
        for(int i=0;i<nElems;i++){
            if (value < orderArray[i]){
                index = i;  
                break;
            }
        }
       
        for(int j=nElems-1;j>=index;j--){
            orderArray[j+1] = orderArray[j];
        }
        orderArray[index] = value;
        nElems++;
        return true;
    }


/**
*
*删除
*
*/
public  boolean delete(int value){

        boolean flag = false;
        int index = 0;
        for(int i=0;i<nElems;i++){ 
            if(value == orderArray[i]){
                index = i;
                flag = true;
                break;
            }
        }

        if(!flag)  
            return false;

        for(int i=index;i<nElems-1;i++){  
            orderArray[i] = orderArray[i+1];
        }
        orderArray[nElems-1] = 0;  
        nElems--;
        return true;
    }





猜你喜欢

转载自blog.csdn.net/qq_38006520/article/details/82429411