快速排序,有个小问题,能帮我看看怎么回事么?

利用快速排序,我写的代码以首元素作为划分基准,可以正常运行,但是使用末元素作为划分基准,为啥变成降序了?

第一种首元素:

#include <stdio.h>
void Swap(int *a,int *b)
{
    int c;
    c= *a;
    *a =*b;
    *b =c;    
}
int Partition(int a[],int p,int r)
{
    int low=p+1,high=r;//low=p,high=r-1;
    int x=a[p];//x=a[r]
    while (1)
    {while (a[low]<x&&low<r)//a[r]>a[high]&&low<r
        low++;//high--
     while (a[high]>x)//a[low]<x 
         high--;
     if (low >=high) break;
     Swap(&a[low],&a[high]);
    }
    a[p]=a[high];//a[r]=a[low]
    a[high]=x;//a[low]=x;
    return high;//low

}

void QuickSort(int a[],int p,int r)
{
    if(p<r)
    {
        int q=Partition(a,p,r);
        QuickSort(a,p,q-1);
        QuickSort(a,q+1,r);
    }
}
void main()
{
    int i=0;
    int p=0,r=19;
    int a[]={87,75,45,67,21,54,5,4,7,8,1,2,3,4,5,6,7,8,9,10};
    Partition ( a, 0, 19);
    QuickSort ( a, 0, 19);
    for(i=0;i<r+1;i++)
    {
        printf("%d,",a[i]);
    }
}

第二种为元素:

#include "stdio.h"
void Swap(int *a,int *b)
{
    int c;
    c= *a;
    *a =*b;
    *b =c;    
}
int Partition(int a[],int p,int r)
{
    int low=p,high=r-1;
    int x=a[r];//x=a[r-1]
    while (1)
    {
         while (a[low]>x && low<r) //a[r]>a[high]&&low<r
            low++;//high--
         while (a[high]<x)//a[low]<x 
             high--;
         if (low >=high) 
             break;
         Swap(&a[high],&a[low]);
    }
    a[r]=a[low];//a[low]=a[r]
    a[low]=x;//a[r]=x
    return high;

}

void QuickSort(int a[],int p,int r)
{
    if(p<r)
    {
        int q=Partition(a,p,r);
        QuickSort(a,p,q-1);
        QuickSort(a,q+1,r);
    }
}
void main()
{
    int i=0;
    int p=0,r=19;
    int a[]={87,75,45,67,21,54,5,4,7,8,1,2,3,4,5,6,7,8,9,10};
    Partition ( a, 0, 19);
    QuickSort ( a, 0, 19);
    for(i=0;i<r+1;i++)
    {
        printf("%d,",a[i]);
    }

 

这个是正常的,你对照一下

package T5;

public class QuickSort {
	int a[];
	public QuickSort() {
		a = new int[]{8,19,2,3,100,99,1000,888,-1,0};
	}
	public QuickSort(int a[]) {
		this.a = a;
	}
	//分区:i是左边界,j是右边界
	int partition(int a[],int i,int j,int size){
		//把小于m的放在m的左边,大于等于m的放在m的右边
		int m=a[i];
		while(i<j){
			//从右往左,查找小于m的元素
			while(i<j && a[j]>=m){
				j--;
			}
			if(i<j){
				a[i++]=a[j];
			}
			//从左往右,查找大于m的元素
			while(i<j && a[i]<m){
				i++;
			}
			if(i<j){
				a[j--]=a[i];
			}
		}
		a[i]=m;
//		print();
		return i;
	}
	//排序
	public void quickSort(int a[],int left,int right,int size){
		if(left<right){
			int pos = partition(a, left, right, size);
			//左边区块
			quickSort(a, left, pos-1, size); //调用很多次
			quickSort(a, pos+1,right, size);//调用很多次
		}
//		else{
//			System.out.println("排序结束");
//		}
	}
	public void print(){
		for (int e : a) {
			System.out.print(e+"\t");
		}
		System.out.println("");
	}
	public static void main(String[] args) {
		
		QuickSort sort = new QuickSort();
		System.out.println("排序前数据顺序:");
		sort.print();
		long start = System.currentTimeMillis();
		sort.quickSort(sort.a, 0, sort.a.length-1, sort.a.length);
		sort.print();
		long end = System.currentTimeMillis()-start;
		System.out.println("排序用时:"+end +"毫秒");
	}
}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632