qq_52037146 2021-05-27 17:03 采纳率: 0%
浏览 37

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

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

第一种首元素:

#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]);
    }

 

  • 写回答

2条回答 默认 最新

  • 关注

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

    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 +"毫秒");
    	}
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址