Skip to main content
Tweeted twitter.com/StackCodeReview/status/1170034135001382915
copy-edited
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65

Find fail case Quicksort for this quicksort codeints

I have written this code forthe following quicksort without taking anyusing online help. Can someone suggest any case when it will fail ? I am not ableIt seems to find any fail case for this.be fine, but is there anything to improve?

#include<iostream>

using namespace std;
void quicksort(int*, int, int);
void print(int *p, int n)
{
    for(int i=0; i < n; ++i)
        cout << p[i] << ' ' ;
    cout << endl;
}
int main()
{
    int arr[] = {10,10,15,15,23,5,55,8,6,0,32,10,100};
    int len = sizeof(arr)/sizeof(int);
    
    cout << "Before sort:\n";
    print(arr,len);
    quicksort(arr,0,len-1);
    
    cout << "After sort:\n";
    print(arr,len);
}

void quicksort(int *arr, int start, int end)
{
    if(start >= end)
        return;
    int pivot = arr[end];
    
    int i=start,j=start;
    for(;j < end; ++j)
        if(arr[j] < pivot)
        {
            swap(arr[i],arr[j]);
            ++i;
        }
    swap(arr[i],arr[end]);
    
    quicksort(arr,0,i-1);
    quicksort(arr,i+1,end);
}

Find fail case for this quicksort code

I have written this code for quicksort without taking any online help. Can someone suggest any case when it will fail ? I am not able to find any fail case for this.

#include<iostream>

using namespace std;
void quicksort(int*, int, int);
void print(int *p, int n)
{
    for(int i=0; i < n; ++i)
        cout << p[i] << ' ' ;
    cout << endl;
}
int main()
{
    int arr[] = {10,10,15,15,23,5,55,8,6,0,32,10,100};
    int len = sizeof(arr)/sizeof(int);
    
    cout << "Before sort:\n";
    print(arr,len);
    quicksort(arr,0,len-1);
    
    cout << "After sort:\n";
    print(arr,len);
}

void quicksort(int *arr, int start, int end)
{
    if(start >= end)
        return;
    int pivot = arr[end];
    
    int i=start,j=start;
    for(;j < end; ++j)
        if(arr[j] < pivot)
        {
            swap(arr[i],arr[j]);
            ++i;
        }
    swap(arr[i],arr[end]);
    
    quicksort(arr,0,i-1);
    quicksort(arr,i+1,end);
}

Quicksort for ints

I have written the following quicksort without using online help. It seems to be fine, but is there anything to improve?

#include<iostream>

using namespace std;
void quicksort(int*, int, int);
void print(int *p, int n)
{
    for(int i=0; i < n; ++i)
        cout << p[i] << ' ' ;
    cout << endl;
}
int main()
{
    int arr[] = {10,10,15,15,23,5,55,8,6,0,32,10,100};
    int len = sizeof(arr)/sizeof(int);
    
    cout << "Before sort:\n";
    print(arr,len);
    quicksort(arr,0,len-1);
    
    cout << "After sort:\n";
    print(arr,len);
}

void quicksort(int *arr, int start, int end)
{
    if(start >= end)
        return;
    int pivot = arr[end];
    
    int i=start,j=start;
    for(;j < end; ++j)
        if(arr[j] < pivot)
        {
            swap(arr[i],arr[j]);
            ++i;
        }
    swap(arr[i],arr[end]);
    
    quicksort(arr,0,i-1);
    quicksort(arr,i+1,end);
}
Source Link

Find fail case for this quicksort code

I have written this code for quicksort without taking any online help. Can someone suggest any case when it will fail ? I am not able to find any fail case for this.

#include<iostream>

using namespace std;
void quicksort(int*, int, int);
void print(int *p, int n)
{
    for(int i=0; i < n; ++i)
        cout << p[i] << ' ' ;
    cout << endl;
}
int main()
{
    int arr[] = {10,10,15,15,23,5,55,8,6,0,32,10,100};
    int len = sizeof(arr)/sizeof(int);
    
    cout << "Before sort:\n";
    print(arr,len);
    quicksort(arr,0,len-1);
    
    cout << "After sort:\n";
    print(arr,len);
}

void quicksort(int *arr, int start, int end)
{
    if(start >= end)
        return;
    int pivot = arr[end];
    
    int i=start,j=start;
    for(;j < end; ++j)
        if(arr[j] < pivot)
        {
            swap(arr[i],arr[j]);
            ++i;
        }
    swap(arr[i],arr[end]);
    
    quicksort(arr,0,i-1);
    quicksort(arr,i+1,end);
}