I'm writing a Drake Sort Algorithm in java that will sort a collection of elements. The algorithm is supposed to work like this:
An array for example: { -2, 4, 1, 4 }
- Get the max value of the array
- Get the min value of the array
- Create array with the size (max value - min value + 1) = 7 (4-(-2)+1) = {0,0,0,0,0,0,0}
- Go through the array and for each element in the collection count the corresponding index in the array{ 1, 0, 0, 1, 0, 0, 2 }
- Create a new array that is the same size as the collection that I wanted to sort (4)
- Go trough the array and fill the new collection with the sorted values { -2, 1, 4, 4 }
I'm not very good at writing algorithm and this is what I have so far (not completed I know)
public int[] sort(int[] unsorted) {
int maxValue = unsorted.length;
int minValue = unsorted[0];
int [] array = new int[maxValue - minValue + 1];
for (int i = 0; i < array.length; i++) {
int newArray = array.length;
}
return array;
Do you know how I can improve this and actually get it to work?