📜  将给定的单词排序为字符串数组

📅  最后修改于: 2022-05-13 01:56:08.179000             🧑  作者: Mango

将给定的单词排序为字符串数组

给定一个字符串数组Arr[] 。任务是按字典顺序对它们进行排序。

例子:

选择排序方法:同样的问题也可以使用 选择排序。

下面是上述方法的实现:

C++
// C++ Program to sort
// array of strings
 
#include 
using namespace std;
 
// Function to compare 2 words
bool isAlphabeticallySmaller(string str1, string str2)
{
    transform(str1.begin(), str1.end(), str1.begin(),
              ::toupper);
    transform(str2.begin(), str2.end(), str2.begin(),
              ::toupper);
 
    if (str1 < str2) {
        return true;
    }
    return false;
}
 
void selectionSort(vector& arr)
{
    int n = arr.size();
 
    // One by one move boundary of
    // unsorted subarray
    for (int i = 0; i < n - 1; i++) {
 
        // Find the minimum element
        // in unsorted array
        int min_idx = i;
        for (int j = i + 1; j < n; j++)
            if (isAlphabeticallySmaller(arr[j],
                                        arr[min_idx]))
                min_idx = j;
 
        // Swap the found minimum
        // element with the first element
        string temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}
 
// Driver code
int main()
{
    vector Arr
        = { "sun", "earth", "mars", "mercury" };
    int N = Arr.size();
    selectionSort(Arr);
    for (int i = 0; i < N; i++) {
        cout << Arr[i] << "\n";
    }
 
    // This code is contributed by rakeshsahni
 
    return 0;
}


Java
// Java Program to sort
// array of strings
 
class GFG {
    static void selectionSort(String[] arr)
    {
        int n = arr.length;
 
        // One by one move boundary of
        // unsorted subarray
        for (int i = 0; i < n - 1; i++) {
 
            // Find the minimum element
            // in unsorted array
            int min_idx = i;
            for (int j = i + 1; j < n; j++)
                if (isAlphabeticallySmaller(
                        arr[j], arr[min_idx]))
                    min_idx = j;
 
            // Swap the found minimum
            // element with the first element
            String temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }
    // Function to compare 2 words
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        int N = Arr.length;
        selectionSort(Arr);
        for (int i = 0; i < N; i++) {
            System.out.println(Arr[i]);
        }
    }
}


C#
// C# Program to sort
// array of strings
using System;
 
public class GFG
{
  static void selectionSort(string[] arr)
  {
    int n = arr.Length;
 
    // One by one move boundary of
    // unsorted subarray
    for (int i = 0; i < n - 1; i++) {
 
      // Find the minimum element
      // in unsorted array
      int min_idx = i;
      for (int j = i + 1; j < n; j++)
        if (isAlphabeticallySmaller(
          arr[j], arr[min_idx]))
          min_idx = j;
 
      // Swap the found minimum
      // element with the first element
      String temp = arr[min_idx];
      arr[min_idx] = arr[i];
      arr[i] = temp;
    }
  }
  // Function to compare 2 words
  static bool isAlphabeticallySmaller(
    string str1, String str2)
  {
    str1 = str1.ToUpper();
    str2 = str2.ToUpper();
    if (str1.CompareTo(str2) < 0) {
      return true;
    }
    return false;
  }
 
  // Driver code
  static public void Main (){
 
    string[] Arr  = { "sun", "earth", "mars", "mercury" };
    int N = Arr.Length;
    selectionSort(Arr);
    for (int i = 0; i < N; i++) {
      Console.WriteLine(Arr[i]);
    }
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript


Java
// Java code for the above approach:
 
public class GFG {
 
    static void bubbleSort(String[] arr, int n)
    {
        int i, j;
        String temp;
        boolean swapped;
        for (i = 0; i < n - 1; i++) {
            swapped = false;
            for (j = 0; j < n - i - 1; j++) {
                if (isAlphabeticallySmaller(
                        arr[j + 1], arr[j])) {
                    // Swap arr[j] and arr[j+1]
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }
 
            // If no two elements were
            // swapped by inner loop,
            // then break
            if (swapped == false)
                break;
        }
    }
 
    // Function to compare 2 words
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        int N = Arr.length;
        bubbleSort(Arr, N);
        for (int i = 0; i < N; i++) {
            System.out.println(Arr[i]);
        }
    }
}


Java
// Java program to sort array of strings
 
class GFG {
 
    // Function to sort array
    // using insertion sort
    static void insertionSort(String[] arr)
    {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            String key = arr[i];
            int j = i - 1;
 
            // Move elements of arr[0..i-1],
            // that are greater than key,
            // to one position ahead
            // of their current position
            while (j >= 0
                   && isAlphabeticallySmaller(key, arr[j])) {
 
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
 
    // Function to compare 2 words
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        int N = Arr.length;
        insertionSort(Arr);
        for (int i = 0; i < N; i++) {
            System.out.println(Arr[i]);
        }
    }
}


Java
// Java program to sort
// arrays of strings using merge sort
 
class GFG {
 
    // Function to mergeSort 2 arrays
    static String[] mergeSort(String[] Arr,
                              int lo, int hi)
    {
        if (lo == hi) {
            String[] A = { Arr[lo] };
            return A;
        }
        int mid = lo + (hi - lo) / 2;
        String[] arr1 = mergeSort(Arr, lo, mid);
        String[] arr2 = mergeSort(Arr, mid + 1, hi);
 
        String[] arr3 = merge(arr1, arr2);
        return arr3;
    }
 
    static String[] merge(
        String[] Arr1, String[] Arr2)
    {
        int m = Arr1.length;
        int n = Arr2.length;
        String[] Arr3 = new String[m + n];
 
        int idx = 0;
 
        int i = 0;
        int j = 0;
 
        while (i < m && j < n) {
            if (isAlphabeticallySmaller(
                    Arr1[i], Arr2[j])) {
 
                Arr3[idx] = Arr1[i];
                i++;
                idx++;
            }
            else {
                Arr3[idx] = Arr2[j];
                j++;
                idx++;
            }
        }
        while (i < m) {
            Arr3[idx] = Arr1[i];
            i++;
            idx++;
        }
        while (j < n) {
            Arr3[idx] = Arr2[j];
            j++;
            idx++;
        }
        return Arr3;
    }
 
    // Return true if str1 appears before
    // str2 in alphabetical order
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        String[] a = mergeSort(Arr, 0, Arr.length - 1);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}



输出
earth
mars
mercury
sun

时间复杂度: O(K * N 2 )
辅助空间: O(K)

冒泡排序方法:按字典顺序对给定字符串进行排序的基本方法是使用冒泡排序。

下面是上述方法的实现:

Java

// Java code for the above approach:
 
public class GFG {
 
    static void bubbleSort(String[] arr, int n)
    {
        int i, j;
        String temp;
        boolean swapped;
        for (i = 0; i < n - 1; i++) {
            swapped = false;
            for (j = 0; j < n - i - 1; j++) {
                if (isAlphabeticallySmaller(
                        arr[j + 1], arr[j])) {
                    // Swap arr[j] and arr[j+1]
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }
 
            // If no two elements were
            // swapped by inner loop,
            // then break
            if (swapped == false)
                break;
        }
    }
 
    // Function to compare 2 words
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        int N = Arr.length;
        bubbleSort(Arr, N);
        for (int i = 0; i < N; i++) {
            System.out.println(Arr[i]);
        }
    }
}


输出
earth
mars
mercury
sun

时间复杂度: O(K * N 2 ) 其中 K 是每个单词的长度。
辅助空间: O(K)

插入排序方法:问题也可以解决 使用 插入排序。

下面是上述方法的实现:

Java

// Java program to sort array of strings
 
class GFG {
 
    // Function to sort array
    // using insertion sort
    static void insertionSort(String[] arr)
    {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            String key = arr[i];
            int j = i - 1;
 
            // Move elements of arr[0..i-1],
            // that are greater than key,
            // to one position ahead
            // of their current position
            while (j >= 0
                   && isAlphabeticallySmaller(key, arr[j])) {
 
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
 
    // Function to compare 2 words
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        int N = Arr.length;
        insertionSort(Arr);
        for (int i = 0; i < N; i++) {
            System.out.println(Arr[i]);
        }
    }
}


输出
earth
mars
mercury
sun

时间复杂度: O(K * N 2 )
辅助空间: O(K)

有效的方法:更有效地解决问题的想法是通过使用 对字符串进行合并排序。

请按照以下步骤实施此方法:

下面是上述方法的实现:

Java

// Java program to sort
// arrays of strings using merge sort
 
class GFG {
 
    // Function to mergeSort 2 arrays
    static String[] mergeSort(String[] Arr,
                              int lo, int hi)
    {
        if (lo == hi) {
            String[] A = { Arr[lo] };
            return A;
        }
        int mid = lo + (hi - lo) / 2;
        String[] arr1 = mergeSort(Arr, lo, mid);
        String[] arr2 = mergeSort(Arr, mid + 1, hi);
 
        String[] arr3 = merge(arr1, arr2);
        return arr3;
    }
 
    static String[] merge(
        String[] Arr1, String[] Arr2)
    {
        int m = Arr1.length;
        int n = Arr2.length;
        String[] Arr3 = new String[m + n];
 
        int idx = 0;
 
        int i = 0;
        int j = 0;
 
        while (i < m && j < n) {
            if (isAlphabeticallySmaller(
                    Arr1[i], Arr2[j])) {
 
                Arr3[idx] = Arr1[i];
                i++;
                idx++;
            }
            else {
                Arr3[idx] = Arr2[j];
                j++;
                idx++;
            }
        }
        while (i < m) {
            Arr3[idx] = Arr1[i];
            i++;
            idx++;
        }
        while (j < n) {
            Arr3[idx] = Arr2[j];
            j++;
            idx++;
        }
        return Arr3;
    }
 
    // Return true if str1 appears before
    // str2 in alphabetical order
    static boolean isAlphabeticallySmaller(
        String str1, String str2)
    {
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();
        if (str1.compareTo(str2) < 0) {
            return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String[] Arr
            = { "sun", "earth", "mars", "mercury" };
        String[] a = mergeSort(Arr, 0, Arr.length - 1);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}


输出
earth
mars
mercury
sun

时间复杂度: O(K * N * (log(N))
辅助空间: O(N)