今天上午开始看的快速排序,原理百度出来的:
<pre name="code" class="java">package testjson;
import org.junit.Test;
public class Quicksort {
private int i;
private int j;
int t = 0;
int[] tie = new int[5];
public int qp(int[] score, int i, int j) {
int temp;
int key;
int n = j;
int m = i;
key = score[i];
for (j = n; j > i; j--) {
if (score[j] < key) {
temp = score[j];
score[j] = score[i];
score[i] = temp;
for (i = m; i < j; i++) {
if (score[i] > key) {
temp = score[i];
score[i] = score[j];
score[j] = temp;
break;
}
}
}
}
System.out.println("排序完成后的结果:");
for (i = 0; i < score.length; i++) {
System.out.print(score[i] + ",");
}
return j;
}
@Test
public void test() {
int[] score = { 3, 5, 9, 2, 1, 8, 4, 6 };
i = 0;
j = score.length - 1;
qp(score, i, j);
}
}
package testjson;
import org.junit.Test;
public class Quicksort {
private int i;
private int j;
int t = 0;
int[] tie = new int[5];
public int qp(int[] score, int i, int j) {
int temp;
int key;
int n = j;
int m = i;
key = score[i];
for (j = n; j > i; j--) {
if (score[j] < key) {
temp = score[j];
score[j] = score[i];
score[i] = temp;
for (i = m; i < j; i++) {
if (score[i] > key) {
temp = score[i];
score[i] = score[j];
score[j] = temp;
break;
}
}
}
}
qp(score, j + 1, score.length - 1);
qp(score, i, j - 1);
System.out.println("排序完成后的结果:");
for (i = 0; i < score.length; i++) {
System.out.print(score[i] + ",");
}
return j;
}
@Test
public void test() {
int[] score = { 3, 5, 9, 2, 1, 8, 4, 6 };
i = 0;
j = score.length - 1;
qp(score, i, j);
}
}
它的一般的算法设计模式如下:
Divide-and-Conquer(P)
1. if |P|≤n0
2. then return(ADHOC(P))
3. 将P分解为较小的子问题 P1 ,P2 ,…,Pk
4. for i←1 to k
5. do yi ← Divide-and-Conquer(Pi) △ 递归解决Pi
6. T ← MERGE(y1,y2,…,yk) △ 合并子问题
7. return(T)
其中|P|表示问题P的规模;n0为一阈值,表示当问题P的规模不超过n0时,问题已容易直接解出,不必再继续分解。ADHOC(P)是该分治法中的基本子算法,用于直接解小规模的问题P。因此,当P的规模不超过n0时直接用算法ADHOC(P)求解。算法MERGE(y1,y2,…,yk)是该分治法中的合并子算法,用于将P的子问题P1 ,P2 ,…,Pk的相应的解y1,y2,…,yk合并为P的解。
所以我的问题出在,我并没有把问题明显的细分,我只用到递归,而且递归用的不对,关键的代码是:
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
package testjson;
import org.junit.Test;
//网上找的实例
public class Zquicksort {
int a[] = {3,5,9,2,1,8,4,6};
@Test
public void quickSort(){
quick(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+",");
}
public int getMiddle(int[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] >= tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] <= tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
//System.out.println(tmp+","+low);
System.out.println("排序完成后的结果:"+low);
for(int p=0;p<a.length;p++){
System.out.print(a[p]+",");
}
return low; //返回中轴的位置
}
public void _quickSort(int[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
public void quick(int[] a2) {
if (a2.length > 0) { //查看数组是否为空
_quickSort(a2, 0, a2.length - 1);
}
}
}
for(a;b;c) 执行顺序 先执行a 在判断b是否为真,若为真 执行循环体, 执行c 然后再次判断b是否为真,若为真 执行循环体 执行c 。。。 直到b为假,跳出循环
package testjson;
import org.junit.Test;
public class Quicksort {
int[] score = { 3, 5, 9, 2, 1, 8, 4, 6 };
public int qp(int[] score, int i, int j) {
int temp;
int key;
int n = j;
int m = i;
key = score[i];
for (j = n; j > i; j--) {
if (score[j] < key) {
temp = score[j];
score[j] = score[i];
score[i] = temp;
for (i = m; i < j; i++) {
if (score[i] > key) {
temp = score[i];
score[i] = score[j];
score[j] = temp;
break;
}
}
}
if(i==j){
break;
}
}
System.out.println("排序完成后的结果:"+j);
for (i = 0; i < score.length; i++) {
System.out.print(score[i] + ",");
}
return j;
}
public void quick(int[] list, int low, int high) {
if (low < high) {
int middle = qp(list, low, high); //将list数组进行一分为二
quick(list, low, middle - 1); //对低字表进行递归排序
quick(list, middle + 1, high); //对高字表进行递归排序
}
}
@Test
public void test() {
quick(score,0,score.length-1);
}
}