使用快速排序对数组进行排序。使用reccursion

发布于 2025-02-11 19:28:52 字数 1750 浏览 0 评论 0原文

#include<iostream>
using namespace std;

    
    void quickSort(int input[], int start, int end)
    {
            // your code goes here 
    }
    void quickSort(int input[], int size)
    {
            quickSort(input, 0, size - 1);
    }
        
*/
void swap(int* a,int* b){
    int temp=*a;
    *a=*b;
    *b=temp;
}
int count(int input[],int start,int end ){
    static int c=0;
    if(start==end)
        return c;
    if(input[start]>input[end])
        c++;
    return count(input,start,end-1);
}
int partionArray(int input[],int start,int end ){
    int c=count(input,start,end);
    int pi=c+start;
    swap(&input[start],&input[pi]);
    int i=start;
    int j=end;
    while(i<pi&&j>pi)
    {
        if(input[i]<input[pi])
        {
            i++;
        }
        else if(input[j]>=input[pi])
        {
            j--;
        }
        else
        {
            swap(&input[i],&input[j]);
            i++;
            j--;
        }
    }
    return pi;
}
void qs(int input[],int start, int end){
    if(start>=end)
        return;
    int pi=partionArray(input,start,end);
    qs(input,start,pi-1);
    qs(input,pi+1,end);
}
void quickSort(int input[], int size) {
    qs(input,0,size-1);

}

int main(){
    int n;
    cin >> n;
  
    int *input = new int[n];
    
    for(int i = 0; i < n; i++) {
        cin >> input[i];
    }
    
    quickSort(input, n);
    for(int i = 0; i < n; i++) {
        cout << input[i] << " ";
    }
    
    delete [] input;

}

使用快速排序对数组进行排序。使用收集是一个问题。 输入格式: 第1行:整数n ie数组大小 第2行:数组元素(由空间隔开) 输出格式: 阵列元素以越来越多的顺序(被空间隔开) 约束: 1&lt; = n&lt; = 10^3

我在此代码中做错了什么,可以解释任何人吗?此代码对每一件事都正确吗?

#include<iostream>
using namespace std;

    
    void quickSort(int input[], int start, int end)
    {
            // your code goes here 
    }
    void quickSort(int input[], int size)
    {
            quickSort(input, 0, size - 1);
    }
        
*/
void swap(int* a,int* b){
    int temp=*a;
    *a=*b;
    *b=temp;
}
int count(int input[],int start,int end ){
    static int c=0;
    if(start==end)
        return c;
    if(input[start]>input[end])
        c++;
    return count(input,start,end-1);
}
int partionArray(int input[],int start,int end ){
    int c=count(input,start,end);
    int pi=c+start;
    swap(&input[start],&input[pi]);
    int i=start;
    int j=end;
    while(i<pi&&j>pi)
    {
        if(input[i]<input[pi])
        {
            i++;
        }
        else if(input[j]>=input[pi])
        {
            j--;
        }
        else
        {
            swap(&input[i],&input[j]);
            i++;
            j--;
        }
    }
    return pi;
}
void qs(int input[],int start, int end){
    if(start>=end)
        return;
    int pi=partionArray(input,start,end);
    qs(input,start,pi-1);
    qs(input,pi+1,end);
}
void quickSort(int input[], int size) {
    qs(input,0,size-1);

}

int main(){
    int n;
    cin >> n;
  
    int *input = new int[n];
    
    for(int i = 0; i < n; i++) {
        cin >> input[i];
    }
    
    quickSort(input, n);
    for(int i = 0; i < n; i++) {
        cout << input[i] << " ";
    }
    
    delete [] input;

}

Sort an array A using Quick Sort. Using reccursion is the question.
Input format :
Line 1 : Integer n i.e. Array size
Line 2 : Array elements (separated by space)
Output format :
Array elements in increasing order (separated by space)
Constraints :
1 <= n <= 10^3

What did i do wrong in this code pls can any one explain?Is every thing right with this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文