【问题标题】:Alphabetically sorting array in sync with another array与另一个数组同步的按字母顺序排序数组
【发布时间】:2014-04-21 16:09:07
【问题描述】:

我正在开发一个简单的程序,目前,它将输入的名称和标记写入两个单独的数组,并同步数组计数器。例如,第一个数组中条目#3 中的名称对应于第二个数组中条目#3 中的标记。我的代码到现在是这样的:

import java.io.*;

public class project
{
    int ctr;
    int ctr1;
    int pos;
    int temp;
    int max;

    public static void main(String args[]) throws IOException
    {
        new project().input();
    }

    void input() throws IOException
    {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        System.out.println ("How many children?");
        int n = Integer.parseInt(obj.readLine());
        String a[] = new String[n];
        int b[] = new int[n];
        for(ctr=0;ctr<n;ctr++)
        {
            System.out.println("Enter name");
            a[ctr]=obj.readLine();
            System.out.println("Enter marks");
            b[ctr]=Integer.parseInt(obj.readLine());
        }
    }
}

我现在想按字母顺序排列名称列表,这样在按顺序打印数组时,名称按字母顺序从 A 到 Z 排列。

使用compareTo()sort()之类的任何函数,而是使用嵌套循环的冒泡或交换排序,对字符串数组进行排序的最佳方法是什么?

如何同步排序,以便在第一个数组中的名称被打乱时,相应的标记也会被相同地打乱,从而保持数据正确?

【问题讨论】:

  • 嗨,除了教育目的或非常特定的领域(如生物工程)之外,没有内置的 sort() 方法就没有对标准数据类型进行排序的最佳方法。如果是出于教育目的,请查看 Knuth en.wikipedia.org/wiki/The_Art_of_Computer_Programming 的书籍。如果你有一个特定的域,你应该更详细地解释。
  • 要使它们保持同步,只需在第二个阵列上执行与在第一个阵列上执行的操作完全相同的操作。最好创建一个 Student 类,将名称和标记组合到一个对象中,并将它们排序在一起,但看起来这是针对 class project 的。

标签: java arrays sorting


【解决方案1】:

首先,我建议不要这样做。我会创建一个包含名称和标记的类,将其存储在数组中,然后对结果数组进行排序。

然后,如果您真的想这样做,例如在冒泡排序中,当正常代码说要交换要排序的数组中的元素 i 和 j 时,也只需交换另一个数组中的元素 i 和 j。这样就可以了。

【讨论】:

    【解决方案2】:

    代码是 c++,但您可以将其翻译成 java 并根据您的需要进行更改。当我尝试为基于优先级的 CPU 调度的非抢先版本编写代码时,我遇到了类似的问题。试图同步两个的排序

    #include <bits/stdc++.h>
    using namespace std;
      
    // Function to sort character array b[]
    // according to the order defined by a[]
    void pairsort(int a[], char b[], int n)
    {
        pair<int, char> pairt[n];
      
        // Storing the respective array
        // elements in pairs.
        for (int i = 0; i < n; i++) 
        {
            pairt[i].first = a[i];
            pairt[i].second = b[i];
        }
      
        // Sorting the pair array.
        sort(pairt, pairt + n);
          
        // Modifying original arrays
        for (int i = 0; i < n; i++) 
        {
            a[i] = pairt[i].first;
            b[i] = pairt[i].second;
        }
    }
      
    // Driver function
    int main()
    {
        int a[] = {2, 1, 5, 4, 9, 3, 6, 7, 10, 8};
        char b[] = {'A', 'B', 'C', 'D', 'E', 'F', 
                             'G', 'H', 'I', 'J'};
                               
        int n = sizeof(a) / sizeof(a[0]);
          
        // Function calling
        pairsort(a, b, n);
      
        for (int i = 0; i < n; i++)
            cout << a[i] << " ";
        cout << endl;
      
        for (int i = 0; i < n; i++)
            cout << b[i] << " ";
              
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2013-03-18
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      相关资源
      最近更新 更多