24.10.25

10. Selection Sort (Strings), The One Where Friends Compete to Find the Smallest Word

 

hello everyone 

After learning how to sort numbers, Aarav got excited and said, “I bet sorting words is just as easy!” Rahul laughed and replied, “Sure, but let’s see if you can alphabetize your excuses first!”  That’s when we decided to try Selection Sort with strings and honestly, it was way more fun than it sounds!

What’s New Here?

This time, instead of numbers, we’re sorting words. The idea is the same: find the smallest (alphabetically first) word from the list and put it in its correct position. Then repeat for the next smallest, and so on until everything is perfectly sorted! It’s like arranging your playlist from Ariana to Zayn clean, logical, and satisfying!

How It Works

Rahul took charge of finding the smallest words her vocabulary skills are next-level . Aarav handled the swapping but accidentally swapped “zebra” with “apple” once and said, “Oops… still counts, right?”  And I (Daksh) kept printing the sorted list while laughing at their chaos. In the end, we all watched our words line up neatly like school kids in alphabetical order proof that even strings love a little organization! 

Code:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <string.h>
 
 int main() {
     char words[100][50];      // Array to store words
     int totalWords;           // Total number of words
     int index, compareIndex, minIndex; // Loop variables
     char temp[50];            // Temporary storage for swapping
     char continueChoice[10];  // To store user's choice to continue (yes/no)
 
     do {
         printf("Enter number of words (max 100): ");
         scanf("%d", &totalWords);
 
         printf("Enter %d word(s):\n", totalWords);
         for (index = 0; index < totalWords; index++) {
             scanf("%s", words[index]);
         }
 
         // Selection sort
         for (index = 0; index < totalWords - 1; index++) {
             minIndex = index;
             for (compareIndex = index + 1; compareIndex < totalWords; compareIndex++) {
                 if (strcmp(words[compareIndex], words[minIndex]) < 0) {
                     minIndex = compareIndex;
                 }
             }
             if (minIndex != index) {
                 strcpy(temp, words[index]);
                 strcpy(words[index], words[minIndex]);
                 strcpy(words[minIndex], temp);
             }
         }
 
         printf("\nWords in alphabetical order:\n");
         for (index = 0; index < totalWords; index++) {
             printf("%d. %s\n", index + 1, words[index]);
         }
 
         // Ask if user wants to continue
         printf("\nDo you want to sort another set of words? (yes/no): ");
         scanf_s("%s", continueChoice, (unsigned)sizeof(continueChoice));
 
     } while (strcmp(continueChoice, "yes") == 0 || strcmp(continueChoice, "YES") == 0);
 
     printf("\nOkay, exiting the program.\n");
     return 0;
 }

Output:

 
← Previous 🏠 Homepage Next Chapter →

1 comment:

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...