Is there a way to sort the values of a DataStructure (Key/Value pairs) by first extracting the values (int) to an array, and performing a MergeSort?

huangapple go评论54阅读模式
英文:

Is there a way to sort the values of a DataStructure (Key/Value pairs) by first extracting the values (int) to an array, and performing a MergeSort?

问题

我想模拟一个面试安排程序,其中得分最高的姓名会被首先调用。优先级是确保我以某种方式应用合并排序算法到这个实现中。

我心目中的想法是将数据结构中的 int 类型的 复制到一个数组中,对数组进行排序,然后将其放回到数据结构中。

我的挑战是要将数据重新插入到数据结构中,让数字保持其相应的(最初的)名称作为

我尝试使用 switch 语句手动添加名称,其中数组中的元素是某个值:

int[] ints = new int[] {1, 2, 3, 4, 5};
switch (ints)
{
    case 5:
        Applicants.Add("某个名字", 5);
        break;
    default:
}

但我无法添加数字 5,因为我遇到了一个错误(无法隐式转换类型 int[] 到 int)。

除此之外,还有什么其他方法可以将这些元素复制到数据结构的值中,并让名称与其分数匹配?

英文:

I want to simulate an interview scheduler, where the names with the highest scores are called first. It is my priority to make sure I apply the mergeSort algorithm somehow to this implementation.
What I have in mind is to copy the values, of type int, in the Data Structure to an array, perform the sort on the array, and put it back into the data structure.

My challenge is having to insert the data back into the Data Structure with the numbers having their respective (initial) names being retained as the keys.

I tried using a switch statement to add the names manually with the values where the element in an array is a certain value:


int[] ints = new int[] {1,2,3,4,5};
switch (ints)
{
case 5:
Applicants.Add("Some name", 5);
break;
default:
}

But I can't add the number 5 because I get an error (cannot implicitely convert type int[] to int)

How else can I copy these elements to the values of a DataStructure, having the names matching their scores?

答案1

得分: 1

以下是翻译好的部分:

  • 要实现这个目标,有多种方法可供选择。如果你想从键值数据结构的值创建一个数组,你还可以尝试以下方法:

    • 不要创建一个整数数组,而是创建一个包含名称和分数的类
    • [Java 角度] -- 这个类应该实现 Comparable 接口,以便对类对象进行排序
    • 一旦对象数组被排序(使用内置方法或自定义的合并排序),排序后的数组将按照你所需的递减顺序包含名称和分数
    • 从这个有序的排序数组中,你可以将键和值都插入到数据结构中

----- 代码

static class Pair implements Comparable<Pair> {
    private String name;
    private int score;

    public Pair(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Pair o) {
        return o.score - this.score;
    }
}

public static void main(String[] args) {
    Pair[] array = new Pair[4];
    array[0] = new Pair("A", 3);
    array[1] = new Pair("B", 1);
    array[2] = a new Pair("C", 5);
    array[3] = a new Pair("D", 2);
    Arrays.sort(array);

    for (Pair p : array) {
        System.out.println(p.name + "--" + p.score);
    }
}

----- 输出

C--5
A--3
D--2
B--1

希望这对你有所帮助。

英文:

There are multiple methods to do this, but if you want to create an array from the values of your Key-Value Data Structure, you can also try the following:

  • Instead of creating an integer array, create a class that contains name and score
  • [Java perspective] -- This class should implement Comparable to sort the class objects in order
  • Once the object array is sorted [in-built method or your merge sort], the sorted array will have both Name and score in the decreasing order you want
  • From this ordered sorted array you can insert both key and values to the Data structure

-----Code

static class Pair implements Comparable&lt;Pair&gt; {
		private String name;
		private int score;

		public Pair(String name, int score) {
			this.name = name;
			this.score = score;
		}

		@Override
		public int compareTo(Pair o) {
			return o.score - this.score;
		}
	}

	public static void main(String[] args) {
		Pair[] array = new Pair[4];
		array[0] = new Pair(&quot;A&quot;, 3);
		array[1] = new Pair(&quot;B&quot;, 1);
		array[2] = new Pair(&quot;C&quot;, 5);
		array[3] = new Pair(&quot;D&quot;, 2);
		Arrays.sort(array);

		for (Pair p : array) {
			System.out.println(p.name + &quot;--&quot; + p.score);
		}
	}

-----Output-----

C--5
A--3
D--2
B--1

huangapple
  • 本文由 发表于 2023年2月14日 21:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定