Question: NEED HELP IMPLEMENTING COUTING SORT //TO DO: transform into a generic class: key stays a int but value could be any class public class Element

NEED HELP IMPLEMENTING COUTING SORT

//TO DO: transform into a generic class: key stays a int but value could be any class

public class Element {

int key;

String value;

public Element(int key, String value) {

this.key = key;

this.value = value;

}

public int getKey() {

return key;

}

public String getValue() {

return value;

}

public boolean equals(Element e){

return (key==e.key) && (e.value.equals(this.value));

}

@Override

public boolean equals(Object obj) {

@SuppressWarnings("unchecked")

Element e = (Element) obj;

return this.equals(e);

}

}

//End of Element Class

import java.util.ArrayList;

class CountingSort {

//TO DO: implement counting sort

//don't forget to modify the signature of sort to use a generic element after you modify Element

public ArrayList sort(ArrayList A) {

if (A.size()==0) return A;

// Don't hard-code an upper bound on the key.

//get range of values in A[i].key: low=lower bound; upp=upper bound

//create an ArrayList with index in [0,upp-low]

//fill in range ArrayList R

//build the output array

ArrayList B = new ArrayList<>(A.size());

return B;

}

//Do not modify any code here; just add more unit tests in CountingSortTest.java

public ArrayList> fill(int[] keys, V[] values){

if (keys.length != values.length) return null; //dirty error detection; would be better to throw exception. Still, do not modify.

ArrayList> A = new ArrayList<>();

for (int i=0; i < keys.length; i++){

Element e = new Element<>(keys[i],values[i]);

A.add(e);

}

return A;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!