hola,
I really need your help on this. I tried to do k-way merge sort in c# and finally I ended with this:
public ArrayList MergeSort ( ArrayList arrIntegers ) {
if (arrIntegers.Count == 1) {
return arrIntegers;
}
ArrayList arrSortedInt = new ArrayList();
int middle = (int)arrIntegers.Count/2;
ArrayList leftArray = arrIntegers.GetRange(0, middle);
ArrayList rightArray = arrIntegers.GetRange(middle, arrIntegers.Count - middle);
leftArray = MergeSort(leftArray);
rightArray = MergeSort(rightArray);
int leftptr = 0;
int rightptr=0;
for (int i = 0; i < leftArray.Count + rightArray.Count; i++) {
if (leftptr==leftArray.Count){
arrSortedInt.Add(rightArray[rightptr]);
rightptr++;
}else if (rightptr==rightArray.Count){
arrSortedInt.Add(leftArray[leftptr]);
leftptr++;
}else if ((int)leftArray[leftptr]<(int)rightArray[rightptr]){
//need the cast above since arraylist returns Type object
arrSortedInt.Add(leftArray[leftptr]);
leftptr++;
}else{
arrSortedInt.Add(rightArray[rightptr]);
rightptr++;
}
}
return arrSortedInt;
}
But this is only two way sorting, I would like to change it and make it n-way merge sorting.