easy
0 views

Sort N Strings – Descending Order

Sort N given strings in descending lexicographical order and print them line by line.

Understand the Problem

Problem Statement

Problem Description

N strings are passed as input. The program must sort them in descending order.

Input Format

The first line contains the value of N. Next N lines contain the value of N string values.

Output Format

N lines containing the N string values sorted in descending order.

Constraints

  • 2 ≤ N ≤ 15
  • Length of each string is between 2 and 100 characters
  • Strings can contain letters (both cases), digits, and special characters
  • Input strings should be read exactly as provided (no trimming unless necessary)

Examples

Example 1
Input
6
Apple
banana
Boy
Zoo
Hat
heckle
Output
heckle
banana
Zoo
Hat
Boy
Apple
Explanation

The strings are sorted in descending lexicographical order. 'heckle' comes first because 'h' has a higher ASCII value than other starting characters in uppercase. 'Apple' comes last because 'A' has the lowest ASCII value among the starting characters.

Solution

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

int main() {
    int n;
    scanf("%d", &n);
    getchar(); // consume newline
    
    char strings[15][101]; // N <= 15, max length 100
    
    // Read strings
    for (int i = 0; i < n; i++) {
        fgets(strings[i], 101, stdin);
        // Remove newline if present
        int len = strlen(strings[i]);
        if (strings[i][len-1] == '\n') {
            strings[i][len-1] = '\0';
        }
    }
    
    // Sort in descending order using bubble sort
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (strcmp(strings[j], strings[j+1]) < 0) {
                // Swap strings
                char temp[101];
                strcpy(temp, strings[j]);
                strcpy(strings[j], strings[j+1]);
                strcpy(strings[j+1], temp);
            }
        }
    }
    
    // Print sorted strings
    for (int i = 0; i < n; i++) {
        printf("%s\n", strings[i]);
    }
    
    return 0;
}
Time:O(n² × m) where n is number of strings (max 15) and m is average string length (max 100). The bubble sort performs O(n²) comparisons, each comparison taking O(m) time.
Space:O(n × m) for storing the strings, where n is number of strings and m is maximum string length.
Approach:

Step 1: Read integer N using scanf and consume the newline character with getchar().

Step 2: Declare a 2D character array to store up to 15 strings, each with max 100 characters plus null terminator.

Step 3: Use fgets to read each string, which safely handles input including spaces. Remove the trailing newline character if present.

Step 4: Implement bubble sort to arrange strings in descending order. Use strcmp() to compare strings lexicographically. If strings[j] comes before strings[j+1] alphabetically (strcmp returns negative), swap them.

Step 5: Print each sorted string using printf with newline character.

Visual Explanation

Loading diagram...