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 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. Complexity of the above algorithm will be O(N2).

Let us look at the code snippet:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation
  
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
  
void sortStrings(char arr[][MAX], int n)
{
    char temp[MAX];
  
    // Sorting strings using bubble sort
    for (int j=0; j<n-1; j++)
    {
        for (int i=j+1; i<n; i++)
        {
            if (strcmp(arr[j], arr[i]) > 0)
            {
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[i]);
                strcpy(arr[i], 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation
class GFG
{
  
    static int MAX = 100;
  
    public static void sortStrings(String[] arr, int n) 
    {
        String temp;
  
        // Sorting strings using bubble sort
        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;
                }
            }
        }
    }
  
    // Driver code
    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]);
    }
}
  
// This code is contributed by
// sanjeev2552
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation 
using System;
      
class GFG
{
    static int MAX = 100;
  
    public static void sortStrings(String[] arr, 
                                   int n) 
    {
        String temp;
  
        // Sorting strings using bubble sort
        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;
                }
            }
        }
    }
  
    // Driver code
    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]);
    }
}
  
// This code is contributed by Princi Singh
chevron_right

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

This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and 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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Improved By : sanjeev2552, princi singh

Article Tags :
Practice Tags :