We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Quicksort 2 - Sorting
- Discussions
Quicksort 2 - Sorting
Quicksort 2 - Sorting
Sort by
recency
|
200 Discussions
|
Please Login in order to post a comment
This solution is going crazy for me: I have tried like this
I have this code:
And output is like this:
if anybody knows please suggests me!!
`
Python
include
include
include
include
include
void quickSort(int *ar, int size)
{
if(size > 1){
int temp, pivot = ar[0], ub = size-1;
for(int i = size-1; i >= 0; --i){
if(ar[i] >= pivot){
temp = ar[i];
int j = i;
while(j < ub){//Variation of Insertion Sorting
ar[j] = ar[j+1];
++j;
}
ar[ub--] = temp;
}
}
++ub;
quickSort(ar, ub);
quickSort(ar+ub+1, size-ub-1);
//Output
for(int i = 0; i < size; ++i){
printf("%d ", ar[i]);
}
printf("\n");
}
}
int main(void)
{
int N;
scanf("%d", &N);
int ar[N];
for(int i = 0; i < N; i++){
scanf("%d", &ar[i]);
}
quickSort(ar, N);
return 0; }
//heqder flies
include
include
include
include
include
//quicksort function
void quickSort(int *a, int s) //s = size //p = pivot //temp is the temaprary variable { //conditional statement if(s > 1){
int temp, p = a[0], up = (s-1);
//loop condition for(int l = s-1; l >= 0; --l){
if(a[l] >= p){
temp = a[l];
int m = l;
while(m < up){
a[m] = a[m+1];
++m;
}
a[up--] = temp;
} }
++up;
quickSort(a, up);
quickSort(a+up+1, s-up-1);
//statement for printing output
for(int l = 0; l < s; ++l){
printf("%d ", a[l]);
}
printf("\n"); }
} //main block int main(void){ //n=no.of.elements
int n;
scanf("%d", &n);
int a[n]; //scanning array
for(int l = 0; l < n; l++) {
scanf("%d", &a[l]);
}
//function call quickSort(a, n);
//it reprasents '0' error return 0;
}
include
include
include
include
include
void quickSort(int *ar, int size)
{
if(size > 1){
int temp, pivot = ar[0], ub = size-1;
for(int i = size-1; i >= 0; --i){
if(ar[i] >= pivot){
temp = ar[i];
int j = i;
while(j < ub){//Variation of Insertion Sorting
ar[j] = ar[j+1];
++j;
}
ar[ub--] = temp;
}
}
++ub;
quickSort(ar, ub);
quickSort(ar+ub+1, size-ub-1);
//Output
for(int i = 0; i < size; ++i){
printf("%d ", ar[i]);
}
printf("\n");
}
}
int main(void)
{
int n;
scanf("%d", &n);
int ar[n];
for(int i = 0; i < n; i++){
scanf("%d", &ar[i]);
}
quickSort(ar, n);
return 0;
}