Internal Sorting: Quicksort-1:Sorting by Exchanging
来源:互联网 发布:人工智能的利弊 作文 编辑:程序博客网 时间:2024/05/24 02:02
Quicksort-1:快速排序-1
Animation
Animated visualization of the quicksort algorithm. The horizontal lines are pivot values.
Full example of quicksort on a random set of numbers. The shaded element is the pivot. It is always chosen as the last element of the partition. However, always choosing the last element in the partition as the pivot in this way results in poor performance (
Complexity
Pseudo code
QUICKSORT(A, p, r) if p < r q = PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r)PARTITION(A, p, r) x = A[r] i = p - 1 for j=p to r-1 if A[j] <= x i = i + 1 exchange A[i] with A[j] exchange A[i+1] with A[r] return i+1
Java program
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 11/29/13 * Time: 10:01 PM * :)~ * Quicksort-1:Sorting by Exchanging:Internal Sorting */public class Main { public static int PARTITION(int[] K, int p, int r){ int x = K[r]; int i = p-1; int temp; for(int j=p; j<=r-1; j++){ if(K[j] <= x){ i++; temp = K[i]; K[i] = K[j]; K[j] = temp; } } temp = K[i+1]; K[i+1] = K[r]; K[r] = temp; return i+1; } public static void QUICKSORT(int[] K, int p, int r){ if(p < r){ int q = PARTITION(K, p, r); QUICKSORT(K, p, q-1); QUICKSORT(K, q+1, r); } } public static void main(String[] args) { int N = 16; int[] K = new int[17]; /*Prepare the data*/ K[1] = 503; K[2] = 87; K[3] = 512; K[4] = 61; K[5] = 908; K[6] = 170; K[7] = 897; K[8] = 275; K[9] = 653; K[10] = 426; K[11] = 154; K[12] = 509; K[13] = 612; K[14] = 677; K[15] = 765; K[16] = 703; /*Output unsorted Ks*/ System.out.println("Unsorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } System.out.println(); /*Kernel of the Algorithm!*/ QUICKSORT(K, 1, N); /*Output sorted Ks*/ System.out.println("Sorted Ks:"); for(int i=1; i<=N; i++){ System.out.println(i+":"+K[i]); } }}
Outputs
Unsorted Ks:1:5032:873:5124:615:9086:1707:8978:2759:65310:42611:15412:50913:61214:67715:76516:703Sorted Ks:1:612:873:1544:1705:2756:4267:5038:5099:51210:61211:65312:67713:70314:76515:89716:908
Reference
<< Introduction to Algorithms >> Third Edition, THOMAS H. CORMEN, CHARLES E. LEISERSON, RONALD L. RIVEST, CLIFFORD STEIN.
https://en.wikipedia.org/wiki/Quicksort
- Internal Sorting: Quicksort-1:Sorting by Exchanging
- Internal Sorting: Quicksort-2: Sorting by Exchanging
- Internal Sorting: Radix sort: Sorting by Exchanging
- Internal Sorting: Bubble sort: Sorting by Exchanging
- Internal Sorting: Radix exchange sort: Sorting by Exchanging
- Internal Sorting: Merge exchange sort: Sorting by Exchanging
- Internal Sorting: Cocktail-shaker sort: Sorting by Exchanging
- Bogosort: Sorting by Exchanging
- Bozosort: Sorting by Exchanging
- Comb sort: Sorting by Exchanging
- Gnome sort: Sorting by Exchanging
- Stooge sort: Sorting by Exchanging
- Internal Sorting: Heapsort-1: Sorting by Selection
- Odd-even sort: Sorting by Exchanging
- Optimized Gnome sort: Sorting by Exchanging
- Internal Sorting: Heapsort-2:Sorting by Selection
- Internal Sorting: List insertion: Sorting by Insertion
- Internal Sorting: Shellsort: Sorting by Insertion
- WCF 之 序列化(Serialization)
- PRG PAULIN v2011 1CD(专业压力容器和管道有限元)
- Folder Structure for Single Page Applications
- 十二周——多态性——项目一(3)动物怎么叫
- 经典FormsAuthenticationTicket 分析
- Internal Sorting: Quicksort-1:Sorting by Exchanging
- OpenCV学习笔记04--图片和视频的保存
- JSEE总结(一)——JNDI
- HDU 1690 Bus System (Floyed求最短路)
- ViewUtils模块的使用
- 使用 FileSystem JAVA API 对 HDFS 进行读、写、删除等操作
- 实现一个通讯录并将信息保存到文本中
- CSU 文本计算器
- 学习笔记一:TCP与UDP通信协议