【Data Structures】 3. ArrayList and Binary Search
2016-11-01 03:46
309 查看
ArrayList class has many methods:
add(object), add(index, object), set(index, object), get(index), remove(index), size()
Initial length of new ArrayList is 10. You can also set the initial ArrayList length on your own.
List<Integer> numbers = new ArrayList<Integer>(0);
The ArrayList used to implement the doubling-up policy. (In Java 6, there has been a change to be ((oldCapacity*3)/2+1).
However, the removal in Java is not that efficient due to the nature of array. The ArrayList needs to shift many elements in the array EVERY TIME the remove(int index) or remove(Object o) method is called (except removing the last
element).
Also, those remove method calls do not reduce the length of the underlying array, which can be a concern interms of memory usage.
If you had any issue with unused memory, you need to manually call trimToSize() method to free up the memory.
Running time complexity analysis of add(E, e) method: adding a new element to the end of an array: Amortized Analysis
Adding a new element at the end of an array takes constant time: O(1). However, when the array is full and we still need to add a new element, we then need to create a new array whose length is twice bigger than that of the original
array AND copy all the elements of the old array into the new array.
List<Integer> numbers = new ArrayList<Integer>(4);
We can conclue that add(E, e) method has amortized constant time by looking at allocated dollars per operation.
Binary Search
Prerequisite for the binary search: The array should already be ordered.
Binary Search requires O(log n) time on a sorted array with n elements.
Advantages of Ordered Arrays
It may be useful in situations where searches are frequent but insertions and deletions are not.
add(object), add(index, object), set(index, object), get(index), remove(index), size()
Initial length of new ArrayList is 10. You can also set the initial ArrayList length on your own.
List<Integer> numbers = new ArrayList<Integer>(0);
The ArrayList used to implement the doubling-up policy. (In Java 6, there has been a change to be ((oldCapacity*3)/2+1).
public boolean add(E e) { ensureCapacity(size + 1); elementData[size++]; return true; } public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) { newCapacity = minCapacity; } elementData = Arrays.copyof(elementData, newCapacity); } }
However, the removal in Java is not that efficient due to the nature of array. The ArrayList needs to shift many elements in the array EVERY TIME the remove(int index) or remove(Object o) method is called (except removing the last
element).
Also, those remove method calls do not reduce the length of the underlying array, which can be a concern interms of memory usage.
If you had any issue with unused memory, you need to manually call trimToSize() method to free up the memory.
Running time complexity analysis of add(E, e) method: adding a new element to the end of an array: Amortized Analysis
Adding a new element at the end of an array takes constant time: O(1). However, when the array is full and we still need to add a new element, we then need to create a new array whose length is twice bigger than that of the original
array AND copy all the elements of the old array into the new array.
List<Integer> numbers = new ArrayList<Integer>(4);
Running time | # of elements | Array length | Allocated dollars | Cost | Saved dollars | Balance | |
number.add(1) | 1 | 1 | 4 | 3 | 1 | 2 | 2 |
number.add(2) | 1 | 2 | 4 | 3 | 1 | 2 | 4 |
number.add(3) | 1 | 3 | 4 | 3 | 1 | 2 | 6 |
number.add(4) | 1 | 4 | 4 | 3 | 1 | 2 | 8 |
number.add(5) | 5 | 5 | 8 | 3 | 5 | -2 | 6 |
number.add(6) | 1 | 6 | 8 | 3 | 1 | 2 | 8 |
number.add(7) | 1 | 7 | 8 | 3 | 1 | 2 | 10 |
number.add(8) | 1 | 8 | 8 | 3 | 1 | 2 | 12 |
number.add(9) | 9 | 9 | 16 | 3 | 9 | -6 | 6 |
Binary Search
Prerequisite for the binary search: The array should already be ordered.
public static int binarySearch(int[] data, int key) { int lowerBound = 0; int upperBound = data.length - 1; int mid; while(true) { if (lowerBound > upperBound) { return -1; } mid = lowerBound + (upperBound - lowerBound) / 2; if (data[mid] == key) { return mid; } if (data[mid] < key) { lowerBound = mid + 1; } else { upperBound = mid - 1; } } }
Binary Search requires O(log n) time on a sorted array with n elements.
Advantages of Ordered Arrays
It may be useful in situations where searches are frequent but insertions and deletions are not.
相关文章推荐
- Binary Search and Euclid Algorithm
- 【Data Structures】 2. Arrays and Linear Search
- Kmeans based indexing and Asymmetric Distance Computation for ANN search (Binary Local Feature):...
- No. 01 - Binary Search Tree and Double-linked List
- Binary search with addition and subtraction
- 中序遍历二叉排序树:BinarySearchTree:Create a tree and InorderTree
- Algorithms Review: Divide and Conquer(Binary Search & Merge Sort)
- merge sort and binary search recursive version
- 109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- Binary Search Tree--find Lowest Common Ancestor--C++ and Java
- [转贴]What is Google Binary Search and Should We Fear it?
- The bubble sort and Binary search
- Binary search tree system and method
- Convert Sorted Array to Binary Search Tree and Merge Two Sorted Lists
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
- PAT Advanced Level 1043. Is It a Binary Search Tree (25)(Java and C++)
- 减治法——二分查找算法(Decrease and Conquer by a Factor - Binary Search Algorithm)
- 转仁兄:Binary search and its variation
- Sqequential Search And Binary Search