数组

  1 package data.struct.algorithm;
  2 
  3 import java.util.Arrays;
  4 
  5 public class ArraysDemo {
  6 
  7 //    private static final int UNSORTARRAYSIZE = 10;
  8 
  9     /**
 10      * @param args
 11      */
 12     /*
 13      * 数据结构之数组的运用,无非是增删查操作,就有序数组和无序数组进行这三种操作
 14      */
 15     public static void main(String[] args) {
 16 
 17         // 无序数组,假定数组中元素互不相同
 18         int arr[] = new int[] { 10, 5, 7, 9, 42, 35, 46, 74, 3, 52, 252 };
 19         displayArr(arr);
 20         // 有序数组,假定数组中元素互不相同
 21         int arrSort[] = new int[] { 98, 65, 34, 25, 19, 16, 8, 4, 2, 1 };
 22         displaySortArr(arrSort);
 23         int key = 42;// 有序、无序数组查找是否存在42这个元素
 24         unSortSearchKey(arr, key);// 无序查找
 25         int position = sortSearchKey(arrSort, key);// 有序查找
 26         System.out.println(key + "在有序数组中索引为" + position + "个位置");
 27         //有序数组arrSort中插入一个指定大小的元素
 28         int key_insert=56;
 29         
 30         int arr_copySort[]=insertKey(arrSort,key_insert);//有序数组插入元素,用一个数组接收返回的数组
 31         displaySortArr(arr_copySort);
 32         insertKey_2(arr,key_insert);//无序数组插入元素
 33         int delSortIndexKey=5;
 34         int delUnSortIndexKey=4;
 35         delete_1(arrSort,delSortIndexKey);
 36         delete_2(arr,delUnSortIndexKey);
 37         
 38     }
 39     //无序数组删除元素
 40     public  static void delete_2(int[] arr, int delUnSortIndexKey) {
 41         
 42 }
 43     //有序数组删除元素
 44     public  static void delete_1(int arr[],int delSortIndexKey) {
 45         if(delSortIndexKey>=arr.length){
 46             return;
 47         }
 48         for(int i=delSortIndexKey;i<arr.length-1;i++){
 49             arr[i]=arr[i+1];
 50         }
 51         arr[arr.length-1]=0;
 52         displaySortArr(arr);
 53 }
 54     //无序数组中插入元素(数组扩容,插入到数组的最后即可)
 55     public static void insertKey_2(int[] arr, int key_insert) {
 56         Arrays.copyOf(arr, arr.length+1);
 57         arr[arr.length-1]=key_insert;
 58         displayArr(arr);
 59     }
 60 
 61     public  static void displaySortArr(int[] arrSort) {
 62         for(int x=0;x<arrSort.length;x++){
 63             System.out.print(arrSort[x]+" ");
 64         }
 65         System.out.println();
 66     }
 67     //打印输出数组中的元素
 68     public  static void displayArr(int[] arr) {
 69         for(int x=0;x<arr.length;x++){
 70             System.out.print(arr[x]+" ");
 71         }
 72         System.out.println();
 73     }
 74     //向有序数组中插入元素,并返回插入元素后的数组
 75     public static int[] insertKey(int[] arrSort, int key_insert) {
 76 //        int left=0;
 77 //        int right=arrSort.length-1;
 78 //        int half=(left+right)/2;
 79         int position=sortSearchKey(arrSort, key_insert);
 80         if(position>=0){
 81             arrSort=Arrays.copyOf(arrSort, arrSort.length+1);
 82             for(int x=arrSort.length-1;x>position;x--){
 83                 arrSort[x]=arrSort[x-1];
 84             }
 85             arrSort[position]=key_insert;
 86 //            displayArr(arrSort);
 87         }
 88         else{
 89             arrSort=Arrays.copyOf(arrSort, arrSort.length+1);
 90             for(int x=arrSort.length-1;x>=-(position+1);x--){
 91                 arrSort[x]=arrSort[x-1];
 92             }
 93             arrSort[-(position+1)]=key_insert;
 94 //            displaySortArr(arrSort);
 95         }
 96         return arrSort;
 97     }
 98 
 99     // 有序数组查找特定元素,可以采用线性查找或者是二分查找(BinarySearch),这里我们使用二分查找
100     //数组为升序排列或降序排列,使用二分查找代码代码有些不同,坑爹呀
101     public static int sortSearchKey(int[] arrSort, int key) {
102         
103         int max,min,mid;  
104           min = 0;  
105           max = arrSort.length-1;  
106           while(min<=max)  
107           {  
108            mid = (max+min)>>1;  
109            if(key>arrSort[mid])  
110               max = mid - 1; 
111            else if(key<arrSort[mid])  
112             min = mid + 1; 
113            else  
114             return mid;  
115           }  
116           return -min-1; 
117     }
118 
119     // 无序数组查找特定元素,线性查找
120     public static void unSortSearchKey(int arr[], int key) {
121         for (int i = 0; i < arr.length; i++) {
122             if(arr[i]==key){
123                 System.out.println(key+"在无序数组中索引为的"+i+"个位置");
124             }
125             else {
126                 continue;
127             }
128             if(i==arr.length){
129                 System.out.println(key+"不存在与无序数组中");
130             }
131         }
132 
133     }
134 
135 }

 

posted @ 2016-04-03 09:30  菜鸟奋斗史  阅读(207)  评论(0编辑  收藏  举报