Sorting Strings using Bubble Sort
Given an array of strings arr[]. Sort given strings using Bubble Sort and display the sorted array.
In Bubble Sort, the two successive strings arr[i] and arr[i+1] are exchanged whenever arr[i]> arr[i+1]. The larger values sink to the bottom and are hence called sinking sort. At the end of each pass, smaller values gradually “bubble” their way upward to the top and hence called bubble sort.
After all the passes, we get all the strings in sorted order.
Let us look at the code snippet
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
void sortStrings( char arr[][MAX], int n)
{
char temp[MAX];
for ( int i = 0; i < n - 1; i++) {
for ( int j = 0; j < n - 1 - i; j++) {
if ( strcmp (arr[j], arr[j + 1]) > 0) {
strcpy (temp, arr[j]);
strcpy (arr[j], arr[j + 1]);
strcpy (arr[j + 1], temp);
}
}
}
}
int main()
{
char arr[][MAX] = { "GeeksforGeeks" , "Quiz" , "Practice" ,
"Gblogs" , "Coding" };
int n = sizeof (arr) / sizeof (arr[0]);
sortStrings(arr, n);
printf ( "Strings in sorted order are : " );
for ( int i = 0; i < n; i++)
printf ( "\n String %d is %s" , i + 1, arr[i]);
return 0;
}
|
Java
class GFG {
static int MAX = 100 ;
public static void sortStrings(String[] arr, int n)
{
String temp;
for ( int j = 0 ; j < n - 1 ; j++) {
for ( int i = j + 1 ; i < n; i++) {
if (arr[j].compareTo(arr[i]) > 0 ) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
public static void main(String[] args)
{
String[] arr = { "GeeksforGeeks" , "Quiz" ,
"Practice" , "Gblogs" , "Coding" };
int n = arr.length;
sortStrings(arr, n);
System.out.println(
"Strings in sorted order are : " );
for ( int i = 0 ; i < n; i++)
System.out.println( "String " + (i + 1 ) + " is "
+ arr[i]);
}
}
|
C#
using System;
class GFG {
static int MAX = 100;
public static void sortStrings(String[] arr, int n)
{
String temp;
for ( int j = 0; j < n - 1; j++) {
for ( int i = j + 1; i < n; i++) {
if (arr[j].CompareTo(arr[i]) > 0) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
public static void Main(String[] args)
{
String[] arr = { "GeeksforGeeks" , "Quiz" ,
"Practice" , "Gblogs" , "Coding" };
int n = arr.Length;
sortStrings(arr, n);
Console.WriteLine( "Strings in sorted order are : " );
for ( int i = 0; i < n; i++)
Console.WriteLine( "String " + (i + 1) + " is "
+ arr[i]);
}
}
|
Python3
def compare(a, b):
return ((a < b) - (a > b))
def sort_string(arr, n):
temp = ""
for i in range (n - 1 ):
for j in range (i + 1 , n):
if compare(arr[j], arr[i]) > 0 :
temp = arr[j]
arr[j] = arr[i]
arr[i] = temp
print ( "String in sorted order are: " )
for i in range (n):
print (f 'Strings {i + 1} is {arr[i]}' )
arr = [ "GeeksforGeeks" , "Quiz" , "Practice" , "Gblogs" , "Coding" ]
n = len (arr)
sort_string(arr, n)
|
Javascript
function sortStrings(arr) {
let temp;
for (let j = 0; j < arr.length - 1; j++) {
for (let i = j + 1; i < arr.length; i++) {
if (arr[j].localeCompare(arr[i]) > 0) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
let arr = [ "GeeksforGeeks" , "Quiz" , "Practice" , "Gblogs" , "Coding" ];
sortStrings(arr);
console.log( "Strings in sorted order are : " );
for (let i = 0; i < arr.length; i++) {
console.log(`String ${i + 1} is ${arr[i]}`);
}
|
Output
Strings in sorted order are :
String 1 is Coding
String 2 is Gblogs
String 3 is GeeksforGeeks
String 4 is Practice
String 5 is Quiz
Time Complexity: O(n2)
Auxiliary Space: O(MAX) or O(100)
Last Updated :
14 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...