C documentation V1 Help

Program: Bubble Sort

Problem Statement:

Write a C program that sorts an array of strings using the Bubble Sort algorithm. The program should take the following steps:

  1. Input:

    • Prompt the user to input the number of strings (n).

    • Allow the user to input each string.

  2. Bubble Sort:

    • Implement the Bubble Sort algorithm to sort the array of strings lexicographically.

    • Use the strcmp function to compare strings and the strcpy function to swap strings during the sorting process.

  3. Output:

    • Display the sorted strings in ascending order.

  4. Example:

    • If the user inputs:

      Input number of strings: 3 Input string 1: zero Input string 2: one Input string 3: two
    • The program should output:

      The strings appear after sorting: one two zero

Problem Explanation:

The program utilizes the Bubble Sort algorithm, a simple sorting technique, to arrange an array of strings. It compares adjacent strings using strcmp and swaps them if they are out of order. This process is repeated until the array is sorted. The resulting sorted strings are then displayed to the user.

Algorithm for Sorting Strings using Bubble Sort:

  1. Input the number of strings n.

  2. Create an array strings to store the strings.

  3. Input each string into the array.

  4. Implement the Bubble Sort algorithm using strcmp and strcpy functions to compare and swap strings.

  5. Display the sorted strings.

Pseudocode:

Function swapStrings(str1, str2): temp = str1 str1 = str2 str2 = temp Function bubbleSortStrings(strings[], n): for i from 0 to n-1: for j from 0 to n-i-1: if strcmp(strings[j], strings[j+1]) > 0: swapStrings(strings[j], strings[j+1]) Function main(): Input n Create an array strings[MAX_STRINGS][MAX_STRING_LENGTH] for i from 0 to n-1: Input strings[i] bubbleSortStrings(strings, n) Display "The strings appear after sorting:" for i from 0 to n-1: Display strings[i]

C Program:

#include <stdio.h> #include <string.h> #define MAX_STRINGS 100 #define MAX_STRING_LENGTH 50 void swapStrings(char *str1, char *str2) { char temp[MAX_STRING_LENGTH]; strcpy(temp, str1); strcpy(str1, str2); strcpy(str2, temp); } void bubbleSortStrings(char strings[][MAX_STRING_LENGTH], int n) { 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) { swapStrings(strings[j], strings[j + 1]); } } } } int main() { int n; printf("Input number of strings: "); scanf("%d", &n); if (n <= 0 || n > MAX_STRINGS) { printf("Invalid number of strings. Please provide a valid number.\n"); return 1; // Return with an error code } char strings[MAX_STRINGS][MAX_STRING_LENGTH]; for (int i = 0; i < n; i++) { printf("Input string %d: ", i + 1); scanf("%s", strings[i]); } bubbleSortStrings(strings, n); printf("\nThe strings appear after sorting:\n"); for (int i = 0; i < n; i++) { printf("%s\n", strings[i]); } return 0; }

Program Explanation:

This program takes the number of strings as input, reads the strings, and then uses the Bubble Sort algorithm to sort them in lexicographical order. The strcmp function is used to compare strings, and the strcpy function is used to swap strings during sorting.

If you have any questions or need further clarification, feel free to ask!

Last modified: 25 February 2024