【DataStructure&AlgorithmInJava】Ch02-OrderArray w binarySearch
2014-05-03 03:10
507 查看
class OrderArray { private int nElems; private int[] arr; public OrderArray(int size){ arr=new int[size]; nElems=0; } //------***insert for ordered array---------------------------------- public void insert(int value){//----plz remember clearly!------ int i; for(i=0;i<nElems;i++) //find the slot if (value < arr[i]) //----note:<---- break; for(int k=nElems;i<k;k--) //move larger elements arr[k]=arr[k-1]; //---note: other loop methods are easy to make an Exception! arr[i]=value; // just remember it; nElems++;//---**-- } //-----delete------- public boolean delete(int value){//---note:return boolean-- int i=binarySearch(value); if (i==-1) return false; else{ for(int k=i;k<nElems;k++) arr[k]=arr[k+1]; //---index out of boundry, problem? nElems--; return true; } } //-----binarySearch---- public int binarySearch(int key){ int lowerB=0; int upperB=nElems-1; int mid; while(true) { mid=(lowerB+upperB)/2; if (arr[mid]==key) return mid; else if (lowerB>upperB) return -1; else{ if (key<arr[mid]) upperB=mid-1; else lowerB=mid+1; } } } //-----get length()---- public int getLength() { return nElems; } //-----print----- public void print(){ for(int i=0;i<nElems;i++) System.out.println("arr["+i+"]="+arr[i]); } } class OrderArrayDemo { public static void main(String[] args) { int maxSize=100; OrderArray arr=new OrderArray(maxSize); arr.insert(7); arr.insert(1); arr.insert(13); arr.insert(110); arr.insert(10); arr.print(); System.out.println("array length="+arr.getLength()); arr.delete(110); arr.print(); System.out.println("array length="+arr.getLength()); System.out.println("index of 10 is:"+arr.binarySearch(10)); arr.print(); } }
相关文章推荐
- 【DataStructure&AlgorithmInJava】Ch02-BinarySearchDemo
- 【DataStructure&AlgorithmInJava】Ch02-PersonArrayDemo
- 【DataStructure&AlgorithmInJava】Ch02-HighArrayDemo
- LeeCode Search in a Big Sorted Array java solution use binary search algorithm
- Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
- leetcode find-minimum-in-rotated-sorted-array java solution use binary search algorithm
- 【DataStructure&AlgorithmInJava】Ch05-LinkedListDemo1
- Data Structure Binary Tree: Inorder Tree Traversal without recursion and without stack!
- Data Structure Binary Tree: Inorder Tree Traversal without Recursion
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
- 【Leetcode】Convert Sorted Array to Binary Search Tree in JAVA
- Data Structure Binary Tree: Populate Inorder Successor for all nodes
- Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals
- Data Structure Binary Tree: Level order traversal in spiral form
- Search in Rotated Sorted Array 1 &&2 -- LeetCode (Java)
- FWNX- build a binary search tree using array - java version <<recursive>>
- 【Leetcode】:108. Convert Sorted Array to Binary Search Tree 问题 in JAVA
- Leetcode 33. Search in Rotated Sorted Array (Hard) (java)
- [LeetCode]Search in Rotated Sorted Array I && II