When I try to sort strings by bubble sort algorithm, I do not see any strings at the output but only one string

56 Views Asked by At

Here is my code:

int main(int argc, char **argv) {
    int numberOfStrings, remaining;
    printf("Input number of strings:");
    scanf("%d", &numberOfStrings);
    // equalizing remaining check count for bubble sort algorithm
    remaining = numberOfStrings - 1; // strCount - 1 is adequate for bubble sort element count
    char strings[numberOfStrings][50], temp[50];
    int count = 0, cmp;
    
    printf("Input string %d:", numberOfStrings);
    
    for (int i = 0; i < numberOfStrings; i++)
        scanf("%s", strings);
    while (remaining != 0) {
        // bubble sort
        cmp = strncmp(strings[count], strings[count+1], sizeof(strings[count]-1));
        if (cmp > 0) {
            strncpy(temp, strings[count], sizeof(temp)-1);
            strncpy(strings[count], strings[count+1], sizeof(strings[count]-1));
            strncpy(strings[count+1], temp, sizeof(temp)-1);
        }

        count++;
        --remaining;
    }
    printf("\n--------------------------------------------\n\n");

    for(int i = 0; i < numberOfStrings; i++)
        printf("%s\n", strings[i]);
    return 0;
}

xx

console:

Input number of strings:3
Input string 3:ddddddddddd
vvvvvvvvvvv
bbbbbbbbbb

--------------------------------------------

bbbbbbbbbb

□□
2

There are 2 best solutions below

0
chqrlie On

You read all strings (you really mean words) at the same position: instead of scanf("%s", strings), you should write:

    for (int i = 0; i < numberOfStrings; i++)
        scanf("%49s", strings[i]);

Also note that your bubble sort is incorrect: when you swap 2 words, you should backtrack to compare with the previous one. Furthermore, there is no need for strncmp nor strncpy.

Here is a modified version:

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

int main(int argc, char *argv[]) {
    int numberOfStrings;
    printf("Input number of words: ");
    if (scanf("%d", &numberOfStrings) != 1)
        return 1;

    char strings[numberOfStrings][50], temp[50];
    
    printf("Input %d words: ", numberOfStrings);
    for (int i = 0; i < numberOfStrings; i++) {
        if (scanf("%49s", strings[i]) != 1)
            return 1;
    }
    for (int i = 1; i < numberOfStrings; i++) {
        int cmp = strcmp(strings[i - 1], strings[i]);
        if (cmp > 0) {
            // swap the words
            strcpy(temp, strings[i - 1]);
            strcpy(strings[i - 1], strings[i]);
            strcpy(strings[i], temp);
            if (i > 1)
                i -= 2;
        }
    }
    printf("\n--------------------------------------------\n\n");

    for (int i = 0; i < numberOfStrings; i++) {
        printf("%s\n", strings[i]);
    }
    return 0;
}
0
Chris On

While chqrlie has identified the source of your error, I feel like there is an opportunity to break this down into functions which would greatly improve your code.

void swap_strings(char **string_arr, size_t str_sz, size_t i, size_t j) {
    char temp[str_sz];

    strcpy(temp, string_arr[i]);
    strcpy(string_arr[i], string_arr[j]);
    strcpy(string_arr[j], temp);
}

// Nested loops approach to bubble sort, with tracking 
// of swaps to enable a shortcut when the array is sorted 
// (no swaps required).
void bubble_sort_strings(char **string_arr, size_t str_sz, size_t n) {
    for (size_t i = 1; i < n; i++) {
        size_t num_swaps = 0;
  
        for (size_t j = 0; j < n - i; j++) {
            if (strcmp(string_arr[j], string_arr[j+1]) > 0) {
                num_swaps++;
                swap_strings(string_arr, str_sz, j, j+1);
            }
        }  

        if (num_swaps == 0) return;
    }
}