C Program to Sort Elements in Lexicographical Order (Dictionary Order)

Updated on September 30, 2024
Sort Elements in Lexicographical Order (Dictionary Order) header image

Introduction

Sorting elements in lexicographical order, often referred to as dictionary order, is a common requirement in many programming scenarios, particularly when dealing with large data sets of words or strings. This method is used to arrange words in the same way that they are ordered in a dictionary, which is essentially alphabetical order with consideration for string length and character comparison.

In this article, you will learn how to implement a C program to sort strings in lexicographical order. You will be guided through multiple examples that demonstrate the techniques for sorting both arrays of characters and arrays of strings.

Sorting Single Words

Sort Characters in a Word

  1. Declare and initialize a string.

  2. Implement a sorting algorithm such as bubble sort to reorder the characters.

    c
    #include <stdio.h>
    #include <string.h>
    
    void sortString(char s[]) {
        int length = strlen(s);
        for (int i = 0; i < length-1; i++) {
            for (int j = i + 1; j < length; j++) {
                if (s[i] > s[j]) {
                    char temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                }
            }
        }
    }
    
    int main() {
        char str[] = "programming";
        sortString(str);
        printf("Sorted string: %s\n", str);
        return 0;
    }
    

    In the above code, sortString function uses a simple bubble sort algorithm to reorder the characters within a string. Each character is compared with others in a nested loop, and they are swapped if they are not in order.

Sorting an Array of Strings

  1. Define an array of strings.

  2. Use a sorting technique suitable for strings.

    c
    #include <stdio.h>
    #include <string.h>
    
    void sortStrings(char arr[][100], int n) {
        char temp[100];
    
        for (int i = 0; i < n-1; i++) {
            for (int j = i+1; j < n; j++) {
                if (strcmp(arr[i], arr[j]) > 0) {
                    strcpy(temp, arr[i]);
                    strcpy(arr[i], arr[j]);
                    strcpy(arr[j], temp);
                }
            }
        }
    }
    
    int main() {
        char words[5][100] = {"Banana", "Apple", "Orange", "Mango", "Strawberry"};
        int n = sizeof(words) / sizeof(words[0]);
        sortStrings(words, n);
        printf("Sorted words:\n");
        for(int i = 0; i < n; i++) {
            printf("%s\n", words[i]);
        }
        return 0;
    }
    

    This snippet sorts an array of strings. The strcmp() function is used to compare two strings lexicographically. If the first string is greater than the second, they are swapped using strcpy().

Conclusion

Sorting strings in lexicographical order in C is an invaluable skill that improves data handling and organization in applications. By mastering string sorting, developers enhance the functionality and performance of their programs. With the demonstrated techniques for sorting both characters and arrays of strings, tackle various data sorting challenges in your projects to maintain well-ordered data.