您的位置:首页 > 理论基础 > 数据结构算法

【数据结构】快速排序算法(Java实现)

2017-07-28 00:00 162 查看


package com.paixu;

class shuzu1 {
int length;
int r[] = new int[100];

// 创建数组
public void create(int a[]) {
for (int i = 0; i <= length; i++) {
r[i] = a[i];

}

}

// 展示数组
public void display() {
System.out.println("这里是展示数组:");
for (int i = 1; i <= length; i++) {
System.out.print(r[i] + " ");

}
System.out.println();
System.out.println();
}

public void swap(int low,int high){
int temp;
temp = r[low];
r[low] = r[high];
r[high] = temp;
System.out.println("此时位置"+low+"和位置"+high+"交换");
display();
}

//快速排序
public void Qsort(int low,int high){
int pivot;

if (low<high){
pivot = Partition(low, high);
System.out.println("此时pivot为:"+pivot);
Qsort(low, pivot-1);
Qsort(pivot+1,high);
}
}

int Partition(int low,int high){
int pivotkey = r[low];
while (low<high){
while(low<high&&r[high]>=pivotkey){
high--;
}
swap(low,high);
while (low<high&&r[low]<=pivotkey){
low++;
}
swap(low,high);
}
System.out.println("-------------------");
return low;
}

}
public class kuaipai {

public static void main(String[] args) {
// TODO Auto-generated method stub
shuzu1 sz = new shuzu1();
sz.length = 10;

int a[] = { 0, 5, 8, 9, 7, 22, 20, 16, 200, 65, 98 };
sz.create(a);
sz.display();
sz.Qsort(1, 10);
sz.display();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构