Algorithm Basic(1)Algorithm in Java
1. Basic
Time O(1), O(n), O(n^2), O(n^3), O(2^n)
Storage
array is set, it is fast, but the length is set.
Collection
—List can have repeat objects
—ArrayList / LinkedList / Vector
—Set can not have repeat objects
—HashSet / TreeSet
Map
—HashMap
—HashTable
—TreeMap
List is interface, Implemented by ArrayList, Vector, LinkedList
ArrayList
good for list and search, bad for insert and delete.
length is not enough, then increase by 1.5 times and array copy happened,
int newCapacity = oldCapacity + (oldCapacity >> 1);
Arrays.copyOf(elementData, newCapacity)
Operator >> : System.out.println(10 >> 1); // 5
System.arraycopy
int[] ids1 = { 1, 2, 3, 4, 5 };
int[] ids2 = newint[6];
//copy ids1 begin from index 1, copy 3 elements to ids2 from index 0
System.arraycopy(ids1, 1, ids2, 0, 3);
System.out.println(Arrays.toString(ids2));
//[2, 3, 4, 0, 0, 0]
even we do add(int index, object), remove, we need to do array copy…..
LinkedList good for insert and delete
It is based on linked list.
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
Vector is slow than ArrayList
similar to ArrayList, and we have synchronized in the codes.
Bubblesort for Array
package com.sillycat.easyalgorithm.sort;
import java.util.List;
import java.util.Vector;
public class Bubblesort implements Sorter {
@Override
public void sort(List<Integer> list) {
if (list.isEmpty()) {
System.out.println("Vector can not be null!");
return;
}
Integer bak;
boolean flag = true;
while (flag) {
flag = false;
for (inti = 0; i < list.size() - 1; i++) {
System.out.println("counting ....");
bak = list.get(i);
if (bak > list.get(i + 1)) {
flag = true;
list.set(i, list.get(i + 1));
list.set(i + 1, bak);
}
}
}
}
public static void main(String[] args) {
Vector<Integer> v1 = new Vector<Integer>();
v1.add(1);
v1.add(9);
v1.add(3);
v1.add(11);
v1.add(6);
System.out.println(v1);
Bubblesort sort1 = new Bubblesort();
sort1.sort(v1);
System.out.println(v1);
}
}
At most, if we have an array N, we need N scan we have the array in right order. a = 2, f(n)=n^2, T(n) = O(n^2)
2. How to Check if Algorithm is good or bad
Time Complexity
Space Complexity
O(1) Complexity
Find the random number in an array, not the biggest, not the smallest, just pick first 3 numbers, sort it, pick up the middle number. O(3) + O(3) + O(1) = O(7)
O(log n) Complexity
10 to 3 functionality
package com.sillycat.easyalgorithm.basic;
public class BaseConversion {
public static int convert10to3(intsource){
String result = "";
while(source != 0){
result = source % 3 + result;
source = source / 3;
}
return result.isEmpty()? 0 : Integer.valueOf(result);
}
public static void main(String[] args) {
//convert 23(10) = 212(3)
System.out.println("23(10) = " + convert10to3(23) + "(3)");
//convert 0(10) = 0(3)
System.out.println("0(10) = " + convert10to3(0) + "(3)");
//convert 101(10) = 10202(3)
System.out.println("101(10) = " + convert10to3(101) + "(3)");
}
}
Adjust to convert to N
package com.sillycat.easyalgorithm.basic;
public class BaseConversion {
public static int convert10toRate(intsource, intrate) {
String result = "";
while (source != 0) {
result = source % rate + result;
source = source / rate;
}
return result.isEmpty() ? 0 : Integer.valueOf(result);
}
public static void main(String[] args) {
// convert 23(10) = 212(3)
System.out.println("23(10) = " + convert10toRate(23, 3) + "(3)");
// convert 0(10) = 0(3)
System.out.println("0(10) = " + convert10toRate(0, 3) + "(3)");
// convert 101(10) = 10202(3)
System.out.println("101(10) = " + convert10toRate(101, 3) + "(3)");
System.out.println("==============================");
// convert 2(10) = 10(2)
System.out.println("2(10) = " + convert10toRate(2, 2) + "(2)");
// convert 0(10) = 0(2)
System.out.println("0(10) = " + convert10toRate(0, 2) + "(2)");
// convert 101(10) = 1100101(2)
System.out.println("101(10) = " + convert10toRate(101, 2) + "(2)");
}
}
1 + |log3n| = O(log3n) = O(logn)
References:
http://daiziguizhong.qiniudn.com/article_20140305-18-32-10.html#rd
http://baike.baidu.com/view/7527.htm
http://javarevisited.blogspot.com/2013/03/top-15-data-structures-algorithm-interview-questions-answers-java-programming.html
http://www.cnblogs.com/wanlipeng/archive/2010/10/21/1857791.html
http://www.360doc.com/content/08/1027/15/61497_1833598.shtml
System.arraycopy(…)
http://blog.youkuaiyun.com/java2000_net/article/details/4059465

本文介绍了基本的时间复杂度概念如O(1), O(n), O(n^2),并讨论了不同数据结构的特点,例如数组、集合、列表和链表等。文章还详细解释了ArrayList与LinkedList的操作效率差异,并通过实例展示了冒泡排序算法与十进制转其他进制的实现。
1633

被折叠的 条评论
为什么被折叠?



