- 浏览: 199234 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
NIghtmare28:
太好用了, 谢谢
Create Local Cloudera Parcels Repo to Save Your ASS -
oyxccyj:
你好,请问下你如上的问题解决了吗?我现在也遇到同样的问题,网上 ...
Homework - HBase Shell, Java Client and MapReduce Job -
20131007:
用java描述算法?
基础数据结构和算法二:Selection sort -
ender35:
第二种实现仅能用于数组排序
计数排序(Counting Sort) -
fy616508150:
我想知道有括号参加运算怎么办
算24算法实现
这个算法实现中,对小的子数组使用插入排序,借此来提高性能。
Knuth推荐对小数组的切割点为9,正是程序中使用的值。实践中还需要根据测试请看选择更好的切割点。
import java.util.Random; public class QuickSort2 { private long[] theArr; private int nElems; public QuickSort2(int max) { this.theArr = new long[max]; this.nElems = 0; } public void insert(long value) { this.theArr[nElems++] = value; } public void display() { System.out.print("A="); for (int i = 0; i < nElems; i++) { System.out.print(theArr[i] + " "); } System.out.println(); } public void quickSort() { this.recQuickSort(0, nElems - 1); } public void recQuickSort(int left, int right) { int size = right - left + 1; if (size <= 9) { // insert sort or sth. else. insertSort(left, right); } else { // quick sort long median = medianOf3(left, right); int partition = partitionIt(left, right, median); recQuickSort(left, partition - 1); recQuickSort(partition + 1, right); } } public void insertSort(int left, int right) { int in, out; for (out = left + 1; out <= right; out++) { long temp = theArr[out]; in = out; while (in > left && theArr[in - 1] > temp) { theArr[in] = theArr[in - 1]; --in; } theArr[in] = temp; } } /** * 因为使用了取头尾和中间点的中值作为枢纽点,并且在median3方法中 * 已经对三个值进行了正确的排序,左右指针都不会越界,所以这里去除了对leftPtr < right * 和rightPtr > left的检测,稍微提高了性能。 * */ public int partitionIt(int left, int right, long pivot) { int leftPtr = left; int rightPtr = right - 1; while (true) { while (theArr[++leftPtr] < pivot) { // } while (theArr[--rightPtr] > pivot) { // } if (leftPtr >= rightPtr) { break; } else { swap(leftPtr, rightPtr); } } swap(leftPtr, right - 1); return leftPtr; } /** * 取三中值法,分别取头尾和中间的点, * 分别取得正确位置以后,再将中值放到最后一个位置进行快速排序。 */ public long medianOf3(int left, int right) { int center = (left + right) / 2; if (theArr[left] > theArr[center]) { swap(left, center); } if (theArr[left] > theArr[right]) { swap(left, right); } if (theArr[center] > theArr[right]) { swap(center, right); } swap(center, right - 1); return theArr[right - 1]; } public void swap(int dex1, int dex2) { long temp = theArr[dex1]; theArr[dex1] = theArr[dex2]; theArr[dex2] = temp; } public static void main(String... args) { if (args.length < 1) { System.out.println("usage: java QuickSort2 number"); return; } QuickSort2 qs = new QuickSort2(100); int count = Integer.parseInt(args[0]); Random rand = new Random(); for (int i = 0; i < count; i++) { qs.insert(rand.nextInt(count * count)); } qs.display(); qs.quickSort(); qs.display(); } }
选取三中值是为了防止在极端情况下,比如每次选取的中值是所有数据中最大或者最小的情况,快速排序性能急剧下降。
发表评论
-
基础数据结构和算法十四:Directed Graphs
2013-12-15 22:40 1775In directed graphs, edges a ... -
基础数据结构和算法十三:Undirected Graphs (2)
2013-12-13 22:51 1025Design pattern for graph p ... -
基础数据结构和算法十三:Undirected Graphs
2013-12-13 20:15 1179A graph is a set of vertices ... -
基础数据结构和算法十二:Hash table
2013-12-02 22:06 996Search algorithms that use ... -
基础数据结构和算法十一:Red-black binary search tree
2013-12-01 12:12 1493The insertion algorithm fo ... -
基础数据结构和算法十:2-3 search tree
2013-11-30 11:07 1187Binary search tree works w ... -
基础数据结构和算法九:Binary Search Tree
2013-11-28 22:39 1628A binary search tree (BST) ... -
基础数据结构和算法八:Binary search
2013-11-28 21:21 1109Binary search needs an ordere ... -
基础数据结构和算法七:Priority queue & Heap sort
2013-11-27 19:47 2677Some important applications o ... -
基础算法七: Priority queue & Heap sort
2013-11-26 21:46 0Some important applications ... -
基础数据结构和算法六:Quick sort
2013-11-21 19:33 1202Quick sort is probably used m ... -
Inversions of an array
2013-11-20 22:28 0TODO -
基础数据结构和算法五:Merge sort
2013-11-20 21:44 1953One of mergesort’s most at ... -
基础数据结构和算法四:Shell sort
2013-11-20 19:11 1149Shellsort is a simple exte ... -
Comparing two sorting algorithms
2013-11-19 21:16 782Generally we compare algorith ... -
基础数据结构和算法三:Insertion Sort
2013-11-19 21:06 965As in selection sort, the ite ... -
基础数据结构和算法二:Selection sort
2013-11-19 20:57 1141One of the simplest sortin ... -
基础数据结构和算法一:UnionFind
2013-11-19 20:47 1247The problem that we consid ... -
Three-sum with linear time complexity
2013-09-14 10:08 0TODO package org.george.algor ... -
Blooming Filter in Hadoop
2013-07-27 22:49 0TODO
相关推荐
快速排序是实际运用中用的最多的算法,虽然它在最坏的情况下会达到n^2,但它的平均性能非常好,期望时间复杂度为nlgn,而且隐含的常数因子非常小,并且是原址排序。 快速排序原理:从一组数中任意选出一个数,将...
C语言实现多种链表快速排序
快速排序 非递归实现方式的完整源代码和测试结果。
知道快速排序算法的思想,但是一直都没有动手写,今天写了下,发现还不是那么容易
全面的排序算法实现,包括插入排序、合并排序、堆排序、快速排序。 堆排序:HeapSort 讲解详见http://blog.csdn.net/fly_yr/article/details/8550701 插入排序:InSertion_Sort 讲解详见...
快速排序 java实现
这是一个用C语言实现的快速排序的程序,它实现了对一个英文文本中的单词排序并将排序结果输出到另外一个文件中。
快速排序算法C语言实现,快排序算法QuickSort.cpp
快速排序 C语言实现 快速排序 C语言实现 快速排序 C语言实现
用MPI实现的快速排序,提高快速排序的效率
用模板类实现的一个简易的c++程序,实现了快速排序。
合并排序,插入排序,希尔排序,快速排序,冒泡排序,桶排序的C语言实现,原创。
选择排序、插入排序、冒泡排序以及快速排序和归并排序的C语言实现,绝对可用
1)实现快速排序算法。 2)要求输入待排序元素个数,利用随机函数生成指定数目的元素,元素值的 取值范围为[10, 1000000]。 3)运行快速排序程序对所生成元素进行排序,要求将元素的初始输入序列和 排序后的结果序列...
基于python实现的快速排序程序源码基于python实现的快速排序程序源码基于python实现的快速排序程序源码基于python实现的快速排序程序源码基于python实现的快速排序程序源码基于python实现的快速排序程序源码基于...
快速排序.py python代码实现快速排序.py python代码实现快速排序.py python代码实现快速排序.py python代码实现快速排序.py python代码实现快速排序.py python代码实现快速排序.py python代码实现快速排序.py python...
C语言数据结构实现快速排序代码,已经过调试可以直接使用。
可实现两个计算机间通信 并实现快速排序算法 2分钟内可排1000万个数 数是随机产生的
java 快速排序 折半查找的界面实现 (递归与分治法)
openmp实现快速排序 用NUM_THREADS设置线程数 建树时间θ(1), 树高θ(logn) 时间复杂度θ(logn)