package aa; class Array{ //定義一個有序數組 private long[] a; //定義數組長度 private int nElems; //構造函數初始化 public Array(int max){ a = new long[max]; nElems = 0; } //size函數 public int size(){ return nElems; } //定義添加函數 public void insert(long value){ //將value賦值給數組成員 a[nElems] = value; //而後將數組長度加一 nElems ++; long temp; //用冒泡法排序 for(int i = 0; i < nElems - 1; i ++) { for(int j = 0; j < nElems - 1 - i; j++) { if(a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } //定義查找方法 public int find(long searchKey){ //由於是有序數組,咱們能夠用二分法來查找時間爲 O(logN),若是線性查找則爲O(N) //下限 int lowerBound = 0; //上限 int upperBound = nElems -1; //中間值 int curIndex; while(true) { curIndex = (lowerBound + upperBound) / 2; if(a[curIndex] == searchKey) { return curIndex; } else if(lowerBound > upperBound) { return nElems; } else { if(a[curIndex] > searchKey) { upperBound = curIndex -1; } else { lowerBound = curIndex + 1; } } } } //定義刪除方法 public boolean delete(long value){ int index = find(value); if(index == size()) { return false; } else { for(int i = index; i < size(); i++) { a[i] = a[i + 1]; } nElems --; return false; } } //定義顯示方法 public void display(){ for(int j = 0; j < nElems; j++) { System.out.println(a[j] + " "); } System.out.println(""); } } public class Arr { public static void main(String[] args) { int maxSize = 100; Array arr = new Array(maxSize); arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(22); arr.insert(66); arr.insert(33); arr.display(); int searchKey = 54; if(arr.find(searchKey) != arr.size()) { System.out.println("found" + searchKey); } else { System.out.println("cant find" + searchKey); } arr.delete(22); arr.delete(55); arr.delete(99); arr.display(); } }
package aa; //定義一個無序數組 class HighArray{ private long[] a; private int nElems; public HighArray(int max) { a = new long[max]; nElems = 0; } public boolean find(long searchKey) { int j; for(j = 0; j < nElems; j++) { if(a[j] == searchKey) { break; } } if(j == nElems) { return false; } else { return true; } } public void insert(long value) { a[nElems] = value; nElems++; } public boolean delete(long value) { int j; for(j = 0; j < nElems; j++) { if(value == a[j]) { break; } } if(j == nElems) { return false; } else { for(int k = j; k < nElems; k++) { a[k] = a[k+1]; } nElems --; return true; } } public void display() { for(int j = 0; j < nElems; j++) { System.out.println(a[j] + ""); } System.out.println(""); } } public class highArrayApp { public static void main(String[] args) { int maxSize = 100; HighArray arr = new HighArray(maxSize); arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33); arr.display(); int searchKey = 35; if(arr.find(searchKey)) { System.out.println("Found" + searchKey); } else { System.out.println("cant find" + searchKey); } arr.delete(00); arr.delete(55); arr.delete(99); arr.display(); } }
大O表示法數組
O(1):優秀。例如無須數組插入。函數
O(logN):良好。例若有序的二分查找。spa
O(N):及格。例如無序數組的刪除,有序數組的刪除和插入,線性查找。code
O(N2):不及格。例如冒泡排序。blog
總結有序數組和無序數組排序
有序數組:插入+ 查找 +刪除 = O(N) +O(logN)+O(N);class
無序數組:插入 + 查找 + 刪除 = O(1) + O(N) + O(N);效率
因此在數據偏向查找操做的時候用有序數組快一些,在數據偏向插入的時候,無序數組好一些。刪除操做效率同樣。構造函數