小第已经写好了,把最后一个数作为POVIT的程序,已经测试!
public class QuickSort{
public static void main(String[] args){
int length=100; int []a=new int[length];
for(int i=0;i<length;i++)
a[i]=(int)(Math.random()*length);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+",");
System.out.println("=============="); QSort(a,0,length-1);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}static int Partition(int[] list, int low, int high) {
int pivotkey;
int temp = list[high];
pivotkey = list[high];
while (low<high) {
while (low<high && list[low]<=pivotkey) ++low;
list[high] = list[low];
while (low<high && list[high]>=pivotkey) --high;
list[low] = list[high];
}
list[high] = temp;
return high;
}
static void QSort(int[] list, int low, int high) { int pivotloc;
if (low < high) {
pivotloc = Partition(list, low, high);
QSort(list, low, pivotloc-1);
QSort(list, pivotloc+1, high);
}
} }
public class QuickSort{
public static void main(String[] args){
int length=100; int []a=new int[length];
for(int i=0;i<length;i++)
a[i]=(int)(Math.random()*length);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+",");
System.out.println("=============="); QSort(a,0,length-1);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}static int Partition(int[] list, int low, int high) {
int pivotkey;
int temp = list[high];
pivotkey = list[high];
while (low<high) {
while (low<high && list[low]<=pivotkey) ++low;
list[high] = list[low];
while (low<high && list[high]>=pivotkey) --high;
list[low] = list[high];
}
list[high] = temp;
return high;
}
static void QSort(int[] list, int low, int high) { int pivotloc;
if (low < high) {
pivotloc = Partition(list, low, high);
QSort(list, low, pivotloc-1);
QSort(list, pivotloc+1, high);
}
} }
public static void main(String[] args){
int[] a = new int[] {8,1,4,9,6,3,5,2,7,0};
QSort(a,0,9);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
static int Partition(int[] list, int low, int high) {
int pivotkey = list[high]; //最后一个数
while (low<high) {
while (low<high && list[low]<=pivotkey) ++low;
list[high] = list[low];
while (low<high && list[high]>=pivotkey) --high;
list[low] = list[high];
}
list[high] = pivotkey;
return high;
}
static void QSort(int[] list, int low, int high) {
int pivotloc;
if (low < high) {
pivotloc = Partition(list, low, high);
QSort(list, low, pivotloc-1);
QSort(list, pivotloc+1, high);
}
}
}
private static java.util.Random rand = new java.util.Random();
public static void main(String[] args){
int[] a = new int[] {8,1,4,9,6,3,5,2,7,0};
QSort(a,0,9);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
static int Partition(int[] list, int low, int high) {
int privotIndex = rand.nextInt(high); //随机数
while(privotIndex<low || privotIndex>high){
privotIndex = rand.nextInt(high);
}
int temp = list[privotIndex];
list[privotIndex] = list[high];
list[high] = temp;
int pivotkey = temp;
while (low<high) {
while (low<high && list[low]<=pivotkey) ++low;
list[high] = list[low];
while (low<high && list[high]>=pivotkey) --high;
list[low] = list[high];
}
list[high] = pivotkey;
return high;
}
static void QSort(int[] list, int low, int high) {
int pivotloc;
if (low < high) {
pivotloc = Partition(list, low, high);
QSort(list, low, pivotloc-1);
QSort(list, pivotloc+1, high);
}
}
}
public static void main(String[] args){
int[] a = new int[] {8,1,4,9,6,3,5,2,7,0};
QSort(a,0,9);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+",");
}
}
static int Partition(int[] list, int low, int high) {
int privotIndex = (low+high)/2; //中间数
int temp = list[privotIndex];
list[privotIndex] = list[high];
list[high] = temp;
int pivotkey = temp;
while (low<high) {
while (low<high && list[low]<=pivotkey) ++low;
list[high] = list[low];
while (low<high && list[high]>=pivotkey) --high;
list[low] = list[high];
}
list[high] = pivotkey;
return high;
}
static void QSort(int[] list, int low, int high) {
int pivotloc;
if (low < high) {
pivotloc = Partition(list, low, high);
QSort(list, low, pivotloc-1);
QSort(list, pivotloc+1, high);
}
}
}
int length=300000;
int []a=new int[length];
for(int i=0;i<length;i++)
a[i]=(length-i);对上面的数组A进行排列,出现OVERFLOW情况.
public class QuickSort4{
private static Integer[] list;
private static java.util.LinkedList stack;
public static void main(String[] args){
Integer[] list = new Integer[30000];
for(int i=0;i<list.length;i++)
list[i]=new Integer(list.length-i);
QSort(list);
for(int i=0;i<list.length;i++)
System.out.print(list[i]+",");
}
private static int getPartition(int low, int high) {
Integer tempInt = list[low];
while (low<high) {
while (low<high && list[high].intValue()>=tempInt.intValue()) --high;
list[low] = list[high];
while (low<high && list[low].intValue()<=tempInt.intValue()) ++low;
list[high] = list[low];
}
list[low] = tempInt;
return low;
}
public static void QSort(Integer[] list){
QuickSort1.list = list;
stack = new java.util.LinkedList();
quicksort(0,list.length-1);
}
private static void quicksort(int low,int high){
Position tempPos;
int pivotIndex;
while(low<high){
if((high-low)>2){
pivotIndex = getPartition(low,high);
if((high-pivotIndex)>(pivotIndex-low)){
stack.addLast(new Position(pivotIndex+1,high));
low = pivotIndex+1;
}else{
stack.addLast(new Position(low,pivotIndex-1));
high = pivotIndex-1;
}
}else if(low<high && (high-low)<3){
sort3(low,high);
low = high;
}else{
tempPos = (Position)stack.removeLast();
low = tempPos.low;
high = tempPos.high;
}
}
}
private static void sort3(int low,int high){
if((high-low) == 1){
if(list[low].intValue() > list[high].intValue())exchange(list[low],list[high]);
else{
if(list[low].intValue() > list[low+1].intValue())
exchange(list[low],list[low+1]);
if(list[low+1].intValue() > list[high].intValue())
exchange(list[low+1],list[high]);
if(list[low].intValue() > list[low+1].intValue())
exchange(list[low],list[low+1]);
}
}
}
private static void exchange(Integer a,Integer b){
Integer tempInt = list[a];
list[a] = list[b];
list[b] = tempInt;
}
private static class Position{
int low,high;
Position(int low,int high){
this.low = low;
this.high = high;
}
}
}