随笔分类 -  alogrithm

快速排序
摘要:public class Main { public static void main(String[] args) { int [] a = { 9,8,7,6,15,4,3,2,1,5 } ; quickSort(a,0,a.length - 1); for (int i:a) System.out.println(i); } private static void quickSort(int... 阅读全文
posted @ 2010-07-08 00:05 sunliho 阅读(136) 评论(0) 推荐(0)
希尔排序
摘要:public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { int [] a = {9,8,7,6,5,4,3,2,1,0} ; shellSort(a); for (int i:a) System.out.println(i); } pri... 阅读全文
posted @ 2010-07-07 09:37 sunliho 阅读(167) 评论(0) 推荐(0)
组合问题
摘要:public class Main { public static void main(String[] args) { int [] a = {3,5,7,9,6} ; composite(a,0,3,new int[3]) ; } private static void composite(int [] src,int offset,int n,int [] des){ if (n == 0)... 阅读全文
posted @ 2010-07-07 00:18 sunliho 阅读(125) 评论(0) 推荐(0)
背包问题
摘要:public class Main { public static void main(String[] args) { int [] a = {2,4,5,7,8,6,10} ; pack(14,a,0,new int[a.length]); } private static void pack(int total,int [] src,int offset,int [] bag){ if (t... 阅读全文
posted @ 2010-07-06 23:32 sunliho 阅读(119) 评论(0) 推荐(0)
求x的y次幂
摘要:public class Main { public static void main(String[] args) { System.out.println(power(3,18)); } //求x的y次幂 private static long power(long x, long y){ if ( y == 1) return x ; else { if (y % 2 == 1) retur... 阅读全文
posted @ 2010-07-06 21:57 sunliho 阅读(264) 评论(0) 推荐(0)
归并排序
摘要:算法效率(O(N*LogN))优于冒泡,选择以及插入排序(O(N*N)),这里的实现效率不高,生成了大量临时数组,对于理解比较容易。public class Main { public static void main(String[] args) { int [] a = {9,8,7,6,5,4,3,2,1}; int [] b = mergeSort(a) ; for (int i:b) S... 阅读全文
posted @ 2010-07-06 05:40 sunliho 阅读(115) 评论(0) 推荐(0)
汉诺塔算法
摘要:public class Main { public static void main(String[] args) { hanoi(4,'A','B','C'); } //将n个盘子从a经过b搬运到c上 private static void hanoi(int n, char a, char b , char c){ if ( n == 1) System.out.println(a + " ... 阅读全文
posted @ 2010-07-06 04:39 sunliho 阅读(179) 评论(0) 推荐(0)
最大公约数
摘要:public class Main { public static void main(String[] args) { System.out.println(common(25,100)); } private static int common(int m , int n){ if (m % n == 0) return n ; else return common(n,m%n); }}最小公... 阅读全文
posted @ 2010-07-06 00:25 sunliho 阅读(127) 评论(0) 推荐(0)
字符全排列递归算法
摘要:public class Main { private static char [] str = {'1','2','3','4'} ; public static void main(String[] args) { arrange(str.length); } private static void arrange(int size){ if (size == 1) { show(); ret... 阅读全文
posted @ 2010-07-06 00:17 sunliho 阅读(183) 评论(0) 推荐(0)
insert sort
摘要:public class Main { public static void main(String[] args) { int [] arr = {9,8,7,6,5,4,3,2,1,0}; insertSort(arr); for(int i:arr) System.out.println(i); } private static void insertSort(int [] arr){ fo... 阅读全文
posted @ 2010-07-04 23:23 sunliho 阅读(179) 评论(0) 推荐(0)
Select sort
摘要:public class Main { public static void main(String[] args) { int [] arr = {9,8,7,6,5,4,3,2,1,0}; selectSort(arr); for(int i:arr) System.out.println(i); } private static void selectSort(int []arr){ for... 阅读全文
posted @ 2010-07-04 22:46 sunliho 阅读(200) 评论(0) 推荐(0)
Bubble sort
摘要:public class Main { public static void main(String[] args) { int [] arr = {9,8,7,6,5,4,3,2,1,0}; bubbleSort(arr); for(int i:arr) System.out.println(i); } private static void bubbleSort(int [] arr){ fo... 阅读全文
posted @ 2010-07-04 22:34 sunliho 阅读(124) 评论(0) 推荐(0)
Binary search
摘要:public class Main { public static void main(String[] args) { int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11}; int x = search(a, 12); System.out.println(x); } private static int search(int[] src, int val)... 阅读全文
posted @ 2010-07-04 09:37 sunliho 阅读(96) 评论(0) 推荐(0)
KMP算法
摘要:public class Main { public static void main(String[] args) { char s[] = "ababca12341234fgdf".toCharArray(); char t[] = "3412".toCharArray(); int x = search(s, t); System.out.println(x); } private stat... 阅读全文
posted @ 2010-07-04 04:44 sunliho 阅读(153) 评论(0) 推荐(0)