快速排序(演化版by数组)
来源:互联网 发布:淘宝修改中差评步骤 编辑:程序博客网 时间:2024/05/22 03:46
基础原始快速排序(进行了优化,保持了稳定性)
import edu.princeton.cs.algs4.StdOut;import edu.princeton.cs.algs4.StdRandom;public class QuickSort { private int[] a; public QuickSort(int N) { a = new int[N]; for (int i = 0; i < N; ++i) a[i] = StdRandom.uniform(-15 * N,15 * N); } public QuickSort(int[] b) { a = new int[b.length]; for (int i = 0; i < a.length; ++i) a[i] = b[i]; } public int partition(int lo,int hi) { int i = lo,j = hi + 1; int v = a[lo]; while (true) { while (a[lo] >= a[++i]) if (i == hi) break; while (a[lo] <= a[--j]) if (j == lo) break; if (i >= j) break; int temp = a[j]; a[j] = a[i]; a[i] = temp; } int temp = a[j]; a[j] = a[lo]; a[lo] = temp; return j; } public void sort(int lo,int hi) { if (hi <= lo) return; int get = partition(lo,hi); sort(lo,get - 1); sort(get + 1,hi); } public void sort() { sort(0,a.length - 1); } public void display() { for (int i = 0; i < a.length; ++i) StdOut.print(a[i] + " "); StdOut.println(); }}
小数组时快速排序比插入排序慢,再次优化
import edu.princeton.cs.algs4.StdOut;import edu.princeton.cs.algs4.StdRandom;public class QuickAndInsert { private int[] a; public QuickAndInsert(int N) { a = new int[N]; for (int i = 0; i < N; ++i) a[i] = StdRandom.uniform(-15 * N,15 * N); } public QuickAndInsert(int[] b) { a = new int[b.length]; for (int i = 0; i < a.length; ++i) a[i] = b[i]; } public int partition(int lo,int hi) { int i = lo,j = hi + 1; while (true) { while (a[lo] >= a[++i]) if (i == hi) break; while (a[lo] <= a[--j]) if (j == lo) break; if (i >= j) break; int temp = a[i]; a[i] = a[j]; a[j] = temp; } int temp = a[lo]; a[lo] = a[j]; a[j] = temp; return j; } public void InsertSort(int lo,int hi) { for (int i = lo; i < hi; ++i) { int j = i + 1; int temp = a[j]; while (j > lo && temp < a[j - 1]) { a[j] = a[j - 1]; --j; } a[j] = temp; } } public void sort(int lo,int hi) { if (hi <= lo) return; if (hi - lo <= 5) { InsertSort(lo,hi); return; } int get = partition(lo,hi); sort(lo,get - 1); sort(get + 1,hi); } public void sort() { sort(0,a.length - 1); } public void display() { for (int i = 0; i < a.length; ++i) StdOut.print(a[i] + " "); StdOut.println(); }}
重复元素过多,会将重复元素数组也切分未免太过浪费,再次进行优化,此次优化的排序方法成为 三向切分的快速排序
import edu.princeton.cs.algs4.StdOut;import edu.princeton.cs.algs4.StdRandom;public class Quick3Way { private int[] a; public Quick3Way(int N) { a = new int[N]; for (int i = 0; i < N; ++i) a[i] = StdRandom.uniform(-15*N,15*N); } public Quick3Way(int[] b) { a = new int[b.length]; for (int i = 0; i < a.length; ++i) a[i] = b[i]; } public void sort(int lo,int hi) { if (lo >= hi) return; int le = lo,i = lo + 1,he = hi; int v = a[lo]; while (i <= he) { if (v > a[i]) { int temp = a[le]; a[le++] = a[i]; a[i++] = temp; } else if (v < a[i]) { int temp = a[i]; a[i] = a[he]; a[he--] = temp; } else ++i; } sort(lo,le - 1); sort(he + 1,hi); } public void sort() { sort(0,a.length - 1); } public void display() { for (int i = 0; i < a.length; ++i) StdOut.print(a[i] + " "); StdOut.println(); }}
阅读全文
1 0
- 快速排序(演化版by数组)
- 归并排序(演化版by数组)
- 数组排序-快速排序
- 二维数组快速排序
- 数组实现快速排序
- JAVA数组快速排序
- iOS数组快速排序
- 数组快速排序,selecter
- int数组快速排序
- 数组和快速排序
- 快速排序:数组
- 快速排序 数组 单链表
- php数组快速排序
- 数组快速排序
- 数组&链表 快速排序
- 数组快速排序
- 二维数组快速排序
- js数组快速排序
- String与StringBuilder与StringBuffer的对比
- angularjs $http 的post方法发送前准备返回404 解决方案
- Server responded "Algorithm negotiation failed"【SSH Secure链接服务器错误】解决办法
- 通过多态生成对象和直接创建对象的区别
- MyBatis实现增删改查
- 快速排序(演化版by数组)
- spring batch的使用和定时器Quart的使用
- 基于jstack的一次问题调查
- Super Jumping! Jumping! Jumping!
- UC/Gui和EmWin的区别:同与不同
- HDU 6073 Matching In Multiplication
- linux下与时间相关的结构体 struct timeb,struct timeval ,struct timezone,struct timespec,struct tm 等
- Docker 构建java工程镜像
- junit用法笔记