I am attempting to program an array sorting and searching program that will be handling 600 items within a string array.The data which is to be sorted looks as such:
2017 | 25 | January | 9994750 | 27.640 | 36.800 | DODECANESE ISLANDS, GREECE | 1485307173 | 01:19:33 | 4.000 |
I have been attempting to implement a merge sort to sort this data. However, I am unable to figure out how to convert a merge sort program designed for an int array into one designed for sorting a string array. Could anyone explain how I could do this?
Though this can be done via the use of existing built in functions I have been attempting this in preparation for university courseworks next years and so I am having to program the Merge sort from scratch and not use the built in functions.
Side note: I must note that I am aware that in its current form the program could not sort months naturally even with the merge sort but that is something I already have a work round for once I can get the merge sort operating as intended.
The current merge sort program:
static public void MainMerge(string[] numbers, int left, int mid, int right)
{
int[] temp = new int[25];
int i, eol, num, pos;
eol = (mid - 1);
pos = left;
num = (right - left + 1);
while ((left <= eol) && (mid <= right))
{
if (numbers[left].CompareTo(numbers[mid]))
temp[pos++] = numbers[left++];
else
temp[pos++] = numbers[mid++];
}
while (left <= eol)
temp[pos++] = numbers[left++];
while (mid <= right)
temp[pos++] = numbers[mid++];
for (i = 0; i < num; i++)
{
numbers[right] = temp[right];
right--;
}
}
static public void SortMerge(string[] numbers, int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
SortMerge(numbers, left, mid);
SortMerge(numbers, (mid + 1), right);
MainMerge(numbers, left, (mid + 1), right);
}
}
public void MergeSort<T>(this T[] elements) where T : IComparable { }Array.Sort(array);instead of implementing your own sorting?stringwithintwill work, but its a terrible solution.