【算法】排序算法
来源:互联网 发布:小学女生内衣淘宝 编辑:程序博客网 时间:2024/05/14 04:46
现在在使用Java语言,也许用Java实现一些算法也是一件很有趣的事情哦。也算是温故而知新吧。
首先还是实现一些简单的排序算法吧。为了简单起见,只实现了升序排序。在我的机器上用QuickSort排序100万个随机整数花费1.6秒左右
- package cn.tenyears.demo;
- /**
- * implements some typical sort algorithms. includes selection sort, bubble
- * sort, insertion sort, quicksort.
- * @author taolue
- */
- public class Sort {
- private Sort() {
- }
- private static <T> void swap(T[] a, int first, int second) {
- T temp = a[first];
- a[first] = a[second];
- a[second] = temp;
- }
- @SuppressWarnings("unchecked")
- public static void selectionSort(Comparable[] a) {
- int size = a.length;
- for (int index = 0; index < size - 1; index++) {
- int small = index;
- for (int index2 = index + 1; index2 < size; index2++) {
- if (a[index2].compareTo(a[small]) < 0)
- small = index2;
- }
- swap(a, index, small);
- }
- }
- @SuppressWarnings("unchecked")
- public static void bubbleSort(Comparable[] a) {
- int size = a.length;
- for (int index = 0; index < size - 1; index++) {
- for (int index2 = index + 1; index2 < size; index2++) {
- if (a[index].compareTo(a[index2]) > 0) {
- swap(a, index, index2);
- }
- }
- }
- }
- @SuppressWarnings("unchecked")
- public static void insertionSort(Comparable[] a) {
- int size = a.length;
- int index2;
- Comparable temp;
- for (int index = 1; index < size; index++) {
- index2 = index;
- temp = a[index];
- while (index2 > 0 && temp.compareTo(a[index2 - 1]) < 0) {
- swap(a, index2, index2 - 1);
- index2--;
- }
- }
- }
- public static void quickSort(Comparable[] a) {
- quickSortInternal(a, 0, a.length - 1);
- }
- @SuppressWarnings("unchecked")
- private static void quickSortInternal(Comparable[] a, int low, int high) {
- Comparable pivot;
- int scanUp, scanDown;
- int mid;
- if (high - low <= 0)
- return;
- if (high - low <= 1) {
- if (a[high].compareTo(a[low]) < 0)
- swap(a, high, low);
- return;
- }
- mid = (low + high) / 2;
- pivot = a[mid];
- swap(a, mid, low);
- scanUp = low + 1;
- scanDown = high;
- do {
- while (scanUp <= scanDown && a[scanUp].compareTo(pivot) < 0)
- scanUp++;
- while (scanDown >= scanUp && pivot.compareTo(a[scanDown]) <= 0)
- scanDown--;
- if (scanUp < scanDown)
- swap(a, scanUp, scanDown);
- } while (scanUp < scanDown);
- a[low] = a[scanDown];
- a[scanDown] = pivot;
- if (low < scanDown - 1)
- quickSortInternal(a, low, scanDown - 1);
- if (scanDown + 1 < high)
- quickSortInternal(a, scanDown + 1, high);
- }
- public static <T> void printA(T[] a) {
- for (int i = 0; i < a.length; i++) {
- if (i != 0 && i % 20 == 0)
- System.out.println();
- System.out.print(a[i] + " ");
- }
- System.out.println();
- }
- public static void main(String[] args) {
- int size = 1000000;
- Integer[] a = new Integer[size];
- for (int i = 0; i < size; i++)
- a[i] = Integer.valueOf((int) Math.round(Math.random() * size));
- long start = System.currentTimeMillis();
- quickSort(a);
- long end = System.currentTimeMillis();
- System.out.println("Last: " + (end - start) + " ms");
- }
- }
- 【算法】计算机算法-排序算法
- 【算法】排序算法
- 算法:基本排序算法
- 排序算法 选择算法
- 算法笔记(排序算法)
- 排序算法--位图算法
- 排序算法-归并算法
- 算法之排序算法
- 算法--排序算法总结
- 算法:排序算法总结
- 《算法导论》排序算法
- 排序算法-插入算法
- 算法:排序算法总结
- 算法:选择排序算法。
- [算法拾遗]排序算法
- 排序算法--希尔算法
- 算法之排序算法
- 算法:快速排序算法
- 【算法】18位身份证号码校验码的算法(JAVA)
- Delphi6项目迁移到Delph7时最常见问题
- 2008年第五届“世界品牌价值实验室年度大奖”评选开始
- aspjpeg生成水印
- Java 开发工具
- 【算法】排序算法
- Delphi SendKeys.Pas
- JFreeChart中柱状图的详细设置
- 【算法】二叉树
- ln 硬链接与软链接的主要区别
- 【算法】堆的应用
- event和delegate的总结
- Linux SHELL if 命令参数说明
- dbhelper数据操作类