高级排序-快速排序,最右边的值为枢纽
来源:互联网 发布:c 界面编程 编辑:程序博客网 时间:2024/05/16 11:12
package g_advancedSort.C_quickSort;
/**
* 快速排序,最右边的值为枢纽
* @author Administrator
*
*/
public class QuickSortApp {
public static void main(String[] args) {
int maxSize = 10;
ArrayIns arr = new ArrayIns(maxSize);
for (int i = 0; i < maxSize; i++) {
long n = (int) (Math.random() * 99);
arr.insert(n);
}
arr.display();
arr.quickSort();
arr.display();
}
}package g_advancedSort.C_quickSort;
public class ArrayIns {
private long[] theArray;
private int nElems;
public ArrayIns(int max) {
theArray = new long[max];
nElems = 0;
}
public void insert(long value) {
theArray[nElems++] = value;
}
public void display() {
System.out.println("A = ");
for (int i = 0; i < nElems; i++) {
System.out.print(theArray[i] + " ");
}
System.out.println();
}
public void quickSort() {
recQuickSort(0, nElems - 1);
}
/*
* 快速排序
*/
private void recQuickSort(int left, int right) {
if (right - left <= 0) {
return;
} else {
long pivot = theArray[right];
int partition = parttitionIt(left, right, pivot);
recQuickSort(left, partition - 1);
recQuickSort(partition + 1, right);
}
}
/*
* 划分
*/
private int parttitionIt(int left, int right, long pivot) {
int leftPtr = left - 1;
int rightPtr = right;
while (true) {
while (theArray[++leftPtr] < pivot)//leftPtr-1划分数组之外
;
while (rightPtr > 0 && theArray[--rightPtr] > pivot)//right划分子数组之外
;
if (leftPtr >= rightPtr)//2种情况,最大和最小值时停止
break;
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right);
return leftPtr;
}
/*
* 交换
*/
public void swap(int dex1, int dex2) {
long temp = theArray[dex1];
theArray[dex1] = theArray[dex2];
theArray[dex2] = temp;
}
}
/**
* 快速排序,最右边的值为枢纽
* @author Administrator
*
*/
public class QuickSortApp {
public static void main(String[] args) {
int maxSize = 10;
ArrayIns arr = new ArrayIns(maxSize);
for (int i = 0; i < maxSize; i++) {
long n = (int) (Math.random() * 99);
arr.insert(n);
}
arr.display();
arr.quickSort();
arr.display();
}
}package g_advancedSort.C_quickSort;
public class ArrayIns {
private long[] theArray;
private int nElems;
public ArrayIns(int max) {
theArray = new long[max];
nElems = 0;
}
public void insert(long value) {
theArray[nElems++] = value;
}
public void display() {
System.out.println("A = ");
for (int i = 0; i < nElems; i++) {
System.out.print(theArray[i] + " ");
}
System.out.println();
}
public void quickSort() {
recQuickSort(0, nElems - 1);
}
/*
* 快速排序
*/
private void recQuickSort(int left, int right) {
if (right - left <= 0) {
return;
} else {
long pivot = theArray[right];
int partition = parttitionIt(left, right, pivot);
recQuickSort(left, partition - 1);
recQuickSort(partition + 1, right);
}
}
/*
* 划分
*/
private int parttitionIt(int left, int right, long pivot) {
int leftPtr = left - 1;
int rightPtr = right;
while (true) {
while (theArray[++leftPtr] < pivot)//leftPtr-1划分数组之外
;
while (rightPtr > 0 && theArray[--rightPtr] > pivot)//right划分子数组之外
;
if (leftPtr >= rightPtr)//2种情况,最大和最小值时停止
break;
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right);
return leftPtr;
}
/*
* 交换
*/
public void swap(int dex1, int dex2) {
long temp = theArray[dex1];
theArray[dex1] = theArray[dex2];
theArray[dex2] = temp;
}
}
0 0
- 高级排序-快速排序,最右边的值为枢纽
- 快速排序的枢纽元选取策略
- 快速排序基于不同枢纽的实现方法
- 快速排序枢纽值(基元)选择方法(转载)
- 快速排序代码(选择最右值最为枢纽元)
- 快速排序普通法与中值枢纽元法以及三切法
- 最常用的排序--快速排序
- 排序算法---最常用的快速排序
- 最常用的排序:快速排序算法
- 高级排序算法--快速排序
- 高级排序之快速排序
- 最朴素的快速排序算法
- 最简单的快速排序代码
- JAVA-最简单的快速排序
- 快速排序的最简易实现
- 快速排序之最简单的阐述
- 高级排序-快速排序-利用三数据取中划分的快速排序算法
- 对于最常用的排序快速排序的思考
- 高级排序-划分
- LeetCode:Maximum Depth of Binary Tree
- 机器视觉技术原理解析及解决方案
- 【Python】学习笔记——-8.3、面向对象高级编程:3.多重继承
- getComputedStyle与currentStyle获取样式(style/class)
- 高级排序-快速排序,最右边的值为枢纽
- 关于:SpringMVC中文乱码问题
- 数论(快速幂) HDU-5690 All X
- 使用高大上的pm2代替forever部署nodejs项目
- 高级排序-快速排序-利用三数据取中划分的快速排序算法
- DP经典 ----- 整数划分
- 【Python】学习笔记——-8.4、面向对象高级编程:4.定制类
- 面向对象静态代码块
- 高级排序-快速排序-使用插入排序来处理小于10个数据项的子数组,使快速排序性能发挥到极致。