摘要: 今天看算法分析是,看到一个这样的问题,就是在一堆数据中查找到第k个大的值。 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,当然,解决这个问题的方法很多,本人在网上搜索了一番,查找到以下的方式,决定很好,推荐给大家。 所谓“第(前)k大数问题”指的是在长度为n(n>=k)的乱序数组中S找出从大到小顺序的第(前)k个数的问题。 解法1: 我们可以对这个乱序数组按照从大到小先行排序,然后取出前k大,总的时间复杂度为O(n*logn + k)。 解法2: 利用选择排序或交互排序,K次选择后即可得到第k大的数。总的时间复杂度为O(n*k) 解法3: 利用快速排... 阅读全文
posted @ 2013-04-19 23:59 William_Xiao 阅读(716) 评论(0) 推荐(1) 编辑
摘要: import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class InputString { public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); S... 阅读全文
posted @ 2013-04-19 23:53 William_Xiao 阅读(261) 评论(0) 推荐(0) 编辑
摘要: import java.io.IOException;import Input.InputString; /** * 冒泡排序 * @author xiaomi * 2012.3.29 */public class BubbleSort { public static void main(String[] args) throws IOException{ String s = InputString.getString(); String[] str = s.split(" "); int[] a = new int[str.length]; ... 阅读全文
posted @ 2013-04-19 23:52 William_Xiao 阅读(146) 评论(0) 推荐(0) 编辑
摘要: import java.io.IOException;import Input.InputString; /** * 选择排序 * @author xiaomi * 2012.3.29 */public class SelectSort { public static void main(String[] args) throws IOException{ String s = InputString.getString(); String[] str = s.split(" "); int[] a = new int[str.length]; ... 阅读全文
posted @ 2013-04-19 23:51 William_Xiao 阅读(133) 评论(0) 推荐(0) 编辑
摘要: import java.io.IOException;import Input.InputString; /** * 插入排序 * @author xiaomi * 2012.3.29 */public class InsertSort { public static void main(String[] args) throws IOException{ String s = InputString.getString(); String[] str = s.split(" "); int[] a = new int[str.length]; ... 阅读全文
posted @ 2013-04-19 23:49 William_Xiao 阅读(135) 评论(0) 推荐(0) 编辑
摘要: import java.io.IOException;import Input.InputString; /** * 希尔排序 * @author xiaomi * 2012.3.29 */public class ShellSort { public static void main(String[] args) throws IOException{ String s = InputString.getString(); String[] str = s.split(" "); int[] a = new int[str.length]; ... 阅读全文
posted @ 2013-04-19 23:47 William_Xiao 阅读(159) 评论(0) 推荐(0) 编辑
摘要: import java.io.IOException;import Input.InputString; /** * 快速排序 * @author xiaomi * 2012.04.02 */public class MergeSort { public static void main(String[] args) throws IOException{ String s = InputString.getString(); String[] str = s.split(" "); int[] a = new int[str.length];... 阅读全文
posted @ 2013-04-19 14:06 William_Xiao 阅读(171) 评论(0) 推荐(0) 编辑
摘要: /** * 快速排序 * @author xiaomi * 2012.4.2 */public class QuickSort { public static void main(String[] args) throws IOException{ String s = InputString.getString(); String[] str = s.split(" "); int[] a = new int[str.length+1]; for(int i = 0;i = pivot){ high-... 阅读全文
posted @ 2013-04-19 13:21 William_Xiao 阅读(158) 评论(0) 推荐(0) 编辑
摘要: package Sort;import java.io.IOException;import Input.InputString;public class HeapSort { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { String str = InputString.getString(); String[] s = str.spl... 阅读全文
posted @ 2013-04-19 12:57 William_Xiao 阅读(172) 评论(0) 推荐(0) 编辑