Uncategorized

quicksort


void quicksort(char* str, int strlen) {
    if (strlen < 2) return;

    int p_idx = strlen / 2;
    char p = str[p_idx];
    int i = 0;
    int j = strlen - 1;

    while (i < j) {
        while (str[i] <= p && i < j) {
            p_idx = i;
            i++;
        }

        while (str[j] > p && i < j) {
            j--;
        }

        if (str[i] > p && str[j] <= p) {
            char ch = str[i];
            str[i] = str[j];
            str[j] = ch;
        }

    }

    quicksort(str, p_idx); // [0 ... (p_idx-1)]
    quicksort(&str[p_idx+1], strlen - p_idx - 1); //[(p_idx+1) ... (strlen-1)]
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s