Program to sort an array of strings using Selection Sort

Given an array of strings, sort the array using Selection Sort.

Examples:

Input  : paper true soap floppy flower
Output : floppy, flower, paper, soap, true

Prerequisite : Selection Sort.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement selection sort for 
// array of strings. 
#include <bits/stdc++.h>
#include <string.h> 
using namespace std; 
#define MAX_LEN 100 
  
// Sorts an array of strings where length of every 
// string should be smaller than MAX_LEN 
void selectionSort(char arr[][MAX_LEN], int n) 
    int i, j, min_idx; 
  
    // One by one move boundary of unsorted subarray 
    char minStr[MAX_LEN]; 
    for (i = 0; i < n-1; i++) 
    
        // Find the minimum element in unsorted array 
        int min_idx = i; 
        strcpy(minStr, arr[i]); 
        for (j = i + 1; j < n; j++) 
        
            // If min is greater than arr[j] 
            if (strcmp(minStr, arr[j]) > 0) 
            
                // Make arr[j] as minStr and update min_idx 
                strcpy(minStr, arr[j]); 
                min_idx = j; 
            
        
  
        // Swap the found minimum element with the first element 
        if (min_idx != i) 
        
            char temp[MAX_LEN]; 
            strcpy(temp, arr[i]); //swap item[pos] and item[i] 
            strcpy(arr[i], arr[min_idx]); 
            strcpy(arr[min_idx], temp); 
        
    
  
// Driver code 
int main() 
    char arr[][MAX_LEN] = {"GeeksforGeeks", "Practice.GeeksforGeeks"
                                                    "GeeksQuiz"}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    int i; 
  
    cout<<"Given array is\n"
    for (i = 0; i < n; i++) 
        cout << i << ": " << arr[i] << endl; 
  
    selectionSort(arr, n); 
  
    cout << "\nSorted array is\n"
    for (i = 0; i < n; i++) 
        cout << i << ": " << arr[i] << endl; 
  
    return 0; 
  
// This is code is contributed by rathbhupendra

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to implement selection sort for
// array of strings.
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
   
// Sorts an array of strings where length of every
// string should be smaller than MAX_LEN
void selectionSort(char arr[][MAX_LEN], int n)
{
    int i, j, min_idx;
   
    // One by one move boundary of unsorted subarray
    char minStr[MAX_LEN];
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        int min_idx = i;
        strcpy(minStr, arr[i]);
        for (j = i+1; j < n; j++)
        {
            // If min is greater than arr[j]
            if (strcmp(minStr, arr[j]) > 0)
            {
                // Make arr[j] as minStr and update min_idx
                strcpy(minStr, arr[j]);
                min_idx = j;
            }
        }
   
        // Swap the found minimum element with the first element
        if (min_idx != i)
        {
            char temp[MAX_LEN];
            strcpy(temp, arr[i]); //swap item[pos] and item[i]
            strcpy(arr[i], arr[min_idx]);
            strcpy(arr[min_idx], temp);
        }
    }
}
   
// Driver code
int main()
{
    char arr[][MAX_LEN] = {"GeeksforGeeks", "Practice.GeeksforGeeks",
                                                    "GeeksQuiz"};
    int n = sizeof(arr)/sizeof(arr[0]);
    int i;
   
    printf("Given array is\n");
    for (i = 0; i < n; i++)
        printf("%d: %s \n", i, arr[i]);
   
    selectionSort(arr, n);
   
    printf("\nSorted array is\n");
    for (i = 0; i < n; i++)
        printf("%d: %s \n", i, arr[i]);
   
    return 0;
}

chevron_right



Output :

Given array is
0: GeeksforGeeks 
1: Practice.GeeksforGeeks 
2: GeeksQuiz 

Sorted array is
0: GeeksQuiz 
1: GeeksforGeeks 
2: Practice.GeeksforGeek

This article is contributed by Nikhil Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : rathbhupendra



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.