Array of String Does not Sort

Viewed 42

I have a C program that takes an array of strings, sorts using selection sort, and also searches a string in the sorted list. The selection sort doesn't work.

The code to display the string is OK. I also tried to verify if the smallest string is identified and yes it does. But it is not moved in its correct position as the selection sort should work. In the main function after displaying the unsorted strings, the program terminates. Where am I not doing the right thing?

Here is the code for the entire program:

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

void doDisplay(char *council[15], int size) {
    int i;
    for (i = 0; i < 15; i++) {
        //printf("\n%s", council[i]);
        puts(council[i]);
    }
}

void select(char *council[15], int size) {
    int pass, i, smallest;
    char temp[];
    for (pass = 0; pass < size; pass++) {
        smallest = pass;
        for (i = pass + 1; i < size; i++) {
            if (strcmp(council[i], council[i - 1]) < 0) {
                smallest = i;
            }
        }
        printf("Value of smallest is: %d\n", smallest);
        if (smallest != pass) {
            strcpy(temp, council[pass]);
            strcpy(council[pass], council[smallest]);
            strcpy(council[smallest], temp);
        }
    }
}

int BinSearch(char *council[15], char *name, int min, int max) {
    int mid;
    min = 0;
    max = 15;
    while (min < max) {
        int mid = (min + max) / 2;
        if (strcmp(council[mid], name) == 0) {
            printf("%s", name);
            return 1;
        } else
        if (strcmp(council[mid], name) > 1) {
            max = mid - 1;
            BinSearch(council, name, min, max);
        } else {
            min = mid + 1;
            BinSearch(council, name, min, max);
        }
    }
}

int main() {
    int result = 0;
    char *key;
    char *council[15] = {
        "Bert", "Jeff", "Gary", "Neba", "Chang",
        "Olga", "Nora", "Mary", "Vera", "Rani",
        "Neil", "Jill", "Cain", "Lara", "Bart"
    };
    printf("Before Sorting\n");
    doDisplay(council, 15);
    select(council, 15);
    printf("\nAfter Sorting");
    doDisplay(council, 15);
    printf("Enter search key: ");
    gets(key);
    scanf("%s", &key);
    result = BinSearch(council, &key, 0, 15);
    return 0;
}
1 Answers

There are multiple problems in your code:

  • the strings in the array are string literals, modifying them has undefined behavior. You should swap the pointers, not the string contents
  • char temp[]; is an invalid definition. Arrays must be defined with an fixed length either specified between the brackets or determined from the initializer at compile time.
  • the test strcmp(council[mid], name) > 1 is incorrect, you should compare to 0.
  • gets() is obsolete, do not use this function.
  • scanf("%s", &key); is invalid: key is an uninitialized pointer, passing its address is a type mismatch. You should pass an array of sufficient length.

Here is a modified version:

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

void doDisplay(char *council[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        puts(council[i]);
    }
}

void select(char *council[], int size) {
    int pass;
    for (pass = 0; pass < size; pass++) {
        int smallest = pass;
        int i;
        for (i = pass + 1; i < size; i++) {
            if (strcmp(council[i], council[smallest]) < 0) {
                smallest = i;
            }
        }
        if (smallest != pass) {
            char *temp = council[pass];
            council[pass] = council[smallest];
            council[smallest] = temp;
        }
    }
}

int BinSearch(char *council[], const char *name, int size) {
    int min = 0, max = size;
    while (min < max) {
        int mid = min + (max - min) / 2;
        int cmp = strcmp(council[mid], name)
        if (cmp == 0) {
            return mid;
        } else
        if (cmp > 0) {
            max = mid;
        } else {
            min = mid + 1;
        }
    }
    return -1;
}

int main() {
    char *council[15] = {
        "Bert", "Jeff", "Gary", "Neba", "Chang",
        "Olga", "Nora", "Mary", "Vera", "Rani",
        "Neil", "Jill", "Cain", "Lara", "Bart"
    };
    int length = sizeof(council) / sizeof(council[0]);
    char key[20];
    int result;

    printf("Before Sorting\n");
    doDisplay(council, length);
    select(council, length);
    printf("After Sorting\n");
    doDisplay(council, length);
    printf("Enter search key: ");
    scanf("%19s", key);
    result = BinSearch(council, key, length);
    if (result < 0) {
        printf("%s not found\n", key);
    } else {
        printf("%s found at %d\n", key, res);
    }
    return 0;
}
Related