Could you please help me on this. i tried but i am getting segmantation fault.

void qsort(char *v[], int left, int right){
int i, last;


void swap(char *v[],int i,int j);


if(left>=right)
return ;


swap(v,left,(left+right)/2);
last = left;


for(i=left+1;i<=right;i++)
if(strcmp(v,v)<0)
swap(v,++last,i);


swap(v,left,last);
qsort(v,left,last-1);
qsort(v,last+1,right);


}


void swap(char *v[],int i,int j){
'...
...
}
int main() {
char s[]={5,3,4,6,1,2,9};
char *temp;


temp = s;


qsort(&temp,0,6);


for(int i=0;i<6;i++)
printf("%c \t",s);


system("PAUSE");
return 0;
}

Recommended Answers

All 5 Replies

1- Most importantly, what u are doing here is not proper quick sort. Read about it.
2- I don't understand why you have sent the array as "*v[]". Why do you think u need that. Why not just "v[]".
3- Putting function prototype inside another function. Not a great way of writing good code.
4- Why use strcmp() for character values?

5- Use <code> tags </code>

yes, you are right that this is the most optimised quick sort and all i want is to implement "pointer to an array" logic. and also there is no use of strcmp in the first place and i could have used if statement to compare. but could you please check why have i done wrong

It is not chr *v[]

This seems to run, performance or also logic I do not know (with code tags and run through Code::Blocks Astyle formatter):

#include <stdio.h>
#include <stdlib.h>
void swap(char v[], int i, int j);

void myqsort(char v[], int left, int right)
{
    int i, last;
    if(left>=right)
        return ;

//printf("myqsort(v, %i, %i), %i...%i\n", left, right, v[left], v[right]);
    swap(v,left,(left+right)/2);
    last = left;

    for(i=left+1; i<=right; i++)
        if(v[i] < v[left])
            swap(v,++last,i);

    swap(v,left,last);

//printf("left part recurse\n");
    myqsort(v,left,last-1);
//printf("right part recurse\n");
    myqsort(v,last+1,right);
}

void swap(char v[], int i, int j)
{
    int temp;
    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

int main()
{
    char s[]= {5,3,4,6,1,2,9};
    int i;

    for(i=0; i<6; i++)
        printf("%i \t",s[i]);

//printf("Sorting\n");
    printf("\n");
    myqsort(&s,0,6);

    for(i=0; i<6; i++)
        printf("%i \t",s[i]);
    printf("\n");


    system("PAUSE");
    return 0;
}

thanks your code is working. Have to make a minor change like instead of mysort(&s,0,6), it has to be mysort(s,0,6).
And one more thing i dont think we are using pointer to an array logic here. it is just a passing the address of an array and do the sorting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.