📌  相关文章
📜  对具有奇数和偶数索引字符的字符串数组进行排序,分别按降序和升序排序

📅  最后修改于: 2021-09-07 03:10:04             🧑  作者: Mango

给定的阵列ARR []N字符串,任务是第一个排序字符串的奇数和偶数索引在降低和增加分别顺序,对于阵列的每个字符串,然后,排序修改后的数组中的字符。

例子:

方法:给定的问题可以通过遍历字符串的给定阵列ARR []并为每个字符串首先排序字符串来解决,然后重新排列的每个字符串,使得在偶数索引字符是按升序排列,在奇数索引的字符是按降序排列.最后,按升序对新的字符串数组进行排序。请按照以下步骤解决问题:

  • 遍历给定的字符串数组arr[]并对每个字符串执行以下步骤:
    • 字母顺序对字符串arr[i]进行排序。
    • 初始化一个变量,比如temp“” ,用于存储结果字符串。
    • 遍历字符串ARR [I]的字符串ARR [I]的半长度和存储第i从一开始索引和字符串的字符串温度的末尾。
    • 如果字符串的大小为奇数,则将字符串arr[i]中间字符添加到变量temp的末尾。
    • 完成上述步骤后,将数组arr[i] 的i索引处的字符串更新为字符串temp
  • 完成上述步骤后,将新的字符串数组按升序排序并打印字符串数组arr[]

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to sort even indices in
// ascending order and odd indices in
// descending order and sort the array
// of strings in ascending order
void sortString(string S[], int N)
{
    // Traverse array of strings
    for (int i = 0; i < N; i++) {
 
        // Sort string in ascending order
        sort(S[i].begin(), S[i].end());
 
        // Length of string
        int n = S[i].size();
 
        string temp = "";
 
        // Traverse the string
        for (int j = 0; j < n / 2; j++) {
            temp += S[i][j];
            temp += S[i][n - j - 1];
        }
 
        // If length of string is odd
        if (n & 1)
            temp += S[i][n / 2];
 
        S[i] = temp;
    }
 
    // Sort array of strings
    sort(S, S + N);
 
    // Print array of strings
    for (int i = 0; i < N; i++) {
        cout << S[i] << " ";
    }
}
 
// Driver Code
int main()
{
    string arr[] = { "ball", "bat", "boy" };
    int N = sizeof(arr) / sizeof(arr[0]);
    sortString(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static String sortString(String inputString)
{
     
    // Convert input string to char array
    char tempArray[] = inputString.toCharArray();
       
    // Sort tempArray
    Arrays.sort(tempArray);
       
    // Return new sorted string
    return new String(tempArray);
}
 
// Function to sort even indices in
// ascending order and odd indices in
// descending order and sort the array
// of Strings in ascending order
static void sortString(String S[], int N)
{
     
    // Traverse array of Strings
    for(int i = 0; i < N; i++)
    {
         
        // Sort String in ascending order
        S[i] = sortString(S[i]);
 
        // Length of String
        int n = S[i].length();
 
        String temp = "";
 
        // Traverse the String
        for(int j = 0; j < n / 2; j++)
        {
            temp += S[i].charAt(j);
            temp += S[i].charAt(n - j - 1);
        }
 
        // If length of String is odd
        if (n %2== 1)
            temp += S[i].charAt(n / 2);
 
        S[i] = temp;
    }
 
    // Sort array of Strings
    Arrays.sort(S);
 
    // Print array of Strings
    for(int i = 0; i < N; i++)
    {
        System.out.print(S[i] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    String arr[] = { "ball", "bat", "boy" };
    int N = arr.length;
     
    sortString(arr, N);
}
}
 
// This code is contributed by Amit Katiyar


C#
// C# program for the above approach
using System;
 
public class GFG{
 
static String sortString(String inputString)
{
     
    // Convert input string to char array
    char []tempArray = inputString.ToCharArray();
       
    // Sort tempArray
    Array.Sort(tempArray);
       
    // Return new sorted string
    return new String(tempArray);
}
 
// Function to sort even indices in
// ascending order and odd indices in
// descending order and sort the array
// of Strings in ascending order
static void sortString(String []S, int N)
{
     
    // Traverse array of Strings
    for(int i = 0; i < N; i++)
    {
         
        // Sort String in ascending order
        S[i] = sortString(S[i]);
 
        // Length of String
        int n = S[i].Length;
 
        String temp = "";
 
        // Traverse the String
        for(int j = 0; j < n / 2; j++)
        {
            temp += S[i][j];
            temp += S[i][n - j - 1];
        }
 
        // If length of String is odd
        if (n %2== 1)
            temp += S[i][n / 2];
 
        S[i] = temp;
    }
 
    // Sort array of Strings
    Array.Sort(S);
 
    // Print array of Strings
    for(int i = 0; i < N; i++)
    {
        Console.Write(S[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    String []arr = { "ball", "bat", "boy" };
    int N = arr.Length;
     
    sortString(arr, N);
}
}
 
// This code is contributed by Amit Katiyar


输出:
albl atb byo

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live