0

I am trying to sort an alphabet array using quicksort.

I've basically tried going from the main algorithm and converting it to work with char arrays.

I think I'm almost there, but I just can't seem to get it.

Any help is much appreciated.

:)

#include <stdio.h>
#include <conio.h>
#include <stdlib.h> 

int qscounter = 0;

int split(char a[], char low, char high)
{
    char part_element = a[low];

    for (;;) {
        while (low < high && part_element <= a[high])
            high--;
        if (low >= high) break;
        a[low++] = a[high]; 
        while (low < high && a[low] <= part_element)
            low++;
        if (low >= high) break;
        a[high--] = a[low];
    }
    a[high] = part_element;
    return high;
}

void quick_sort(char a[], char low, char high)
{
    char middle;

    if (low >= high) return;
    middle = split(a, low, high);
    qscounter++;
    quick_sort(a, low, middle - 1);
    quick_sort(a, middle + 1, high);

    printf("Quick Sort: %d\n", qscounter);
    for(int i=0;i<26;i++)
        printf("%c",a[i]);
    printf("\n\n");
}

void main()
{
    char unsorted_alphabet[26] = {'A','E','O','D','B','Q','G','V','Y','J','Z','S','M','N','C','P','F','R','L','T','U','H','W','X','I','K'};
    quick_sort(unsorted_alphabet,unsorted_alphabet[0],unsorted_alphabet[25]);
    fflush(stdin);
    getchar();
}
Lee
  • 37
  • 2
  • 9

2 Answers2

2

Your code has the following issues: You tried to use element value as array index, which was certainly wrong. You pass a[0] and a[25] as index into quick_sort function, however, low and high should be integer type, not char. You cannot use char value as index since the array values are out of order initially, while array indices are not.

Correct code should be the following:

int split(char a[], int low, int high) //should be integer type for low and high
{
  char part_element = a[low]; 
  //if low is a char, what is a[char]? It will not be the value you intended to want

  //do same thing in your code
}

void quick_sort(char a[], int low, int high)
{
  int middle; //not char

  //do same thing as in your code

}

In main(), function call should be:

 quick_sort(unsorted_alphabet,0,25); //should pass array indices

It actually works fine after these minor changes: I got:

Quick Sort: 20
ABCDEFGHIJKLMNOPQRSTUVWXYZ
taocp
  • 23,276
  • 10
  • 49
  • 62
  • Getting these errors: error C3872: '0xa0': this character is not allowed in an identifier error C2065: ' ' : undeclared identifier error C2143: syntax error : missing ';' before 'while' – Lee Mar 22 '13 at 02:14
  • @Lee Please check out the complete changed source code here:https://docs.google.com/document/d/13jFGf46pZJDTnf61jlXHQBHMVywjIofRAmQPlf-Yp-I/edit?usp=sharing Let me know if you cannot view or compile it. It has no problem for me. – taocp Mar 22 '13 at 02:18
  • Does 20 seem like an accurate count? Did I put the counter in the right place for number of comparisons? – Lee Mar 22 '13 at 02:29
  • 1
    I put the counter right under int middle; and I got 41. I believe that is more accurate and is correct now. Because it is supposed to show the number of comparisons. – Lee Mar 22 '13 at 02:32
0

"low" and "high" parameters are index of the array. You can try to call like this:

quick_sort(unsorted_alphabet,0,25);
Yang
  • 777
  • 1
  • 10
  • 19