Program to sort an array of strings using Selection Sort
Last Updated :
09 Feb, 2023
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++
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define MAX_LEN 100
void selectionSort( char arr[][MAX_LEN], int n)
{
int i, j, min_idx;
char minStr[MAX_LEN];
for (i = 0; i < n-1; i++)
{
int min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j]<arr[min_idx])
{
min_idx = j;
}
}
if (min_idx != i)
{
swap(arr[i],arr[min_idx]);
}
}
}
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;
}
|
C
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
void selectionSort( char arr[][MAX_LEN], int n)
{
int i, j, min_idx;
char minStr[MAX_LEN];
for (i = 0; i < n-1; i++)
{
int min_idx = i;
strcpy (minStr, arr[i]);
for (j = i+1; j < n; j++)
{
if ( strcmp (minStr, arr[j]) > 0)
{
strcpy (minStr, arr[j]);
min_idx = j;
}
}
if (min_idx != i)
{
char temp[MAX_LEN];
strcpy (temp, arr[i]);
strcpy (arr[i], arr[min_idx]);
strcpy (arr[min_idx], temp);
}
}
}
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;
}
|
Java
import java.util.*;
import java.io.*;
class Main
{
static void selectionSort(String arr[], int n)
{
for ( int i = 0 ; i < n - 1 ; i++)
{
int min_index = i;
String minStr = arr[i];
for ( int j = i + 1 ; j < n; j++)
{
if (arr[j].compareTo(minStr) < 0 )
{
minStr = arr[j];
min_index = j;
}
}
if (min_index != i)
{
String temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
}
public static void main(String args[])
{
String arr[] = { "GeeksforGeeks" ,
"Practice.GeeksforGeeks" ,
"GeeksQuiz" };
int n = arr.length;
System.out.println( "Given array is" );
for ( int i = 0 ; i < n; i++)
{
System.out.println(i+ ": " +arr[i]);
}
System.out.println();
selectionSort(arr, n);
System.out.println( "Sorted array is" );
for ( int i = 0 ; i < n; i++)
{
System.out.println(i+ ": " +arr[i]);
}
}
}
|
Python3
def Selection(arr,n):
for i in range (n):
min_index = i
min_str = arr[i]
for j in range (i + 1 ,n):
if min_str>arr[j]:
min_str = arr[j]
min_index = j
if min_index ! = i:
temp = arr[i]
arr[i] = arr[min_index]
arr[min_index] = temp
return arr
arr = [ "GeeksforGeeks" , "Practice.GeeksforGeeks" , "GeeksQuiz" ]
print ( "Given array is" )
for i in range ( len (arr)):
print (i, ":" ,arr[i])
print ( "\nSorted array is" )
for i in range ( len (Selection(arr, len (arr)))):
print (i, ":" ,Selection(arr, len (arr))[i])
|
C#
using System;
class GFG{
static void selectionSort( string [] arr, int n)
{
for ( int i = 0; i < n - 1; i++)
{
int min_index = i;
string minStr = arr[i];
for ( int j = i + 1; j < n; j++)
{
if (arr[j].CompareTo(minStr) != 0)
{
minStr = arr[j];
min_index = j;
}
}
if (min_index != i)
{
string temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
}
static void Main()
{
string [] arr = { "GeeksforGeeks" ,
"Practice.GeeksforGeeks" ,
"GeeksQuiz" };
int n = arr.Length;
Console.WriteLine( "Given array is" );
for ( int i = 0; i < n; i++)
{
Console.WriteLine(i + ": " + arr[i]);
}
Console.WriteLine();
selectionSort(arr, n);
Console.WriteLine( "Sorted array is" );
for ( int i = 0; i < n; i++)
{
Console.WriteLine(i + ": " + arr[i]);
}
}
}
|
Javascript
<script>
function selectionSort(arr, n)
{
for (let i = 0; i < n - 1; i++)
{
let min_index = i;
let minStr = arr[i];
for (let j = i + 1; j < n; j++)
{
if ((arr[j]).localeCompare(minStr) === -1)
{
minStr = arr[j];
min_index = j;
}
}
if (min_index != i)
{
let temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
}
let arr = [ "GeeksforGeeks" ,
"Practice.GeeksforGeeks" ,
"GeeksQuiz" ];
let n = arr.length;
document.write( "Given array is" + "</br>" );
for (let i = 0; i < n; i++)
{
document.write(i + ": " + arr[i] + "</br>" );
}
document.write( "</br>" );
selectionSort(arr, n);
document.write( "Sorted array is" + "</br>" );
for (let i = 0; i < n; i++)
{
document.write(i + ": " + arr[i] + "</br>" );
}
</script>
|
Output
Given array is
0: GeeksforGeeks
1: Practice.GeeksforGeeks
2: GeeksQuiz
Sorted array is
0: GeeksQuiz
1: GeeksforGeeks
2: Practice.GeeksforGeeks
Time Complexity: O(n2), where n represents the size of the character array.
Auxiliary Space: O(100), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...