- 博客(9)
- 资源 (3)
- 收藏
- 关注
转载 “基数排序”应用之数组中缺失的数字
首先看看题目要求: 给定一个无序的整数数组,怎么找到第一个大于0,并且不在此数组的整数。比如[1,2,0]返回3,[3,4,-1,1]返回2,[1, 5, 3, 4, 2]返回6,[100, 3, 2, 1, 6,8, 5]返回4。要求使用O(1)空间和O(n)时间。 我们不难发现使用“基数排序”正好可以用来解决这道题目。 以{1, 3, 6, -100, 2}为例来简介这种解法:
2014-11-07 23:59:38 668
原创 归并排序
package com.sort; public class MergeSort { public static void main(String[] args) { int[] a=new int[]{-6,5,4,3,-2,1}; mergeSort(a,0,a.length-1); for(int array:a){ System.out.println(array);
2014-11-07 23:34:52 1446
原创 希尔排序
package com.sort; public class ShellSort { static void shellsort3(int a[], int n) { int i, j, gap; for (gap = n / 2; gap > 0; gap /= 2) for (i = gap; i < n; i++) for (j = i - gap; j >=
2014-11-07 22:46:12 1458
原创 简单选择排序
package com.sort; public class SelectSort { public static void main(String[] args) { int[] a = new int[]{6,5,4,3,2,1}; selectSort(a); for(int array:a){ System.out.println(array); } }
2014-11-07 02:35:30 538
原创 交换排序之快速排序
package com.sort; public class QuickSort { public static void main(String[] args) { int[] a = new int[]{6,5,4,3,2,1}; quickSort(a,0,a.length-1); for(int array:a){ System.out.println(array);
2014-11-07 02:34:04 544
原创 交换排序之冒泡排序
package com.sort; //冒泡排序:O(n^) public class BubbleSort { public static void bubbleSort(int[] a) { for (int i = 1; i < a.length; i++) {// 表示次数 int j = a.length - 1; while (j >= i) { if (
2014-11-07 02:32:25 573
原创 计数排序
package com.sort; //计数排序 public class CountSort { //适用于排序元素在一个数值范围内 //一下以公司员工年龄为元素排序(Age:18-70) public static void main(String[] args) { int[] a = new int[]{26,25,24,23,23,26,25,24,23,26,25,24,
2014-11-07 02:31:59 1409
原创 堆排序
package com.sort; public class HeapSort { public static void main(String[] args) { HeapSort heapSortObj = new HeapSort(); int[] a = new int[] { 4, 3, 6, 5, 4, 3, 2, 1 }; MinHeap maxHeap = heap
2014-11-07 02:30:59 1508
原创 插入排序之直接插入排序
package com.sort; public class InsertSort { public static void insertSort(int[] a){ for(int i=1; i<a.length; i++){ int j = i; int key = a[j];//暂存到key中 while(j>0 && key<a[j-1]){//不断往后覆盖
2014-11-07 02:28:58 501
C语言成绩管理
2013-03-29
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人