我的QuickSort和MergeSort无法正常运行

时间:2015-05-03 20:29:51

标签: java string sorting arraylist quicksort

我的MergeSort和QuickSort调用插入在段大于< = 15索引时排序。插入排序工作正常,合并工作正常,分区工作正常,但我的MergeSort和QuickSort不能正常工作......他们似乎根本没有排序...请帮助:

以下是我的插入排序函数,用于上下文:

int ndxCopy = 0, ndx, ndxSort, comp, result, ndxProper;
String sortMe = null;
for (ndx = (1 + firstIndex); ndx < numberToSort; ndx++) {
    sortMe = data.get(ndx);
    for (ndxSort = firstIndex; ndxSort < ndx; ndxSort++) {
        comp = sortMe.compareTo(data.get(ndxSort));
        if (comp < 0) {
            ndxProper = ndxSort;
            for (ndxCopy = ndx; ndxCopy > ndxProper; ndxCopy--)
                data.set(ndxCopy, data.get(ndxCopy - 1));

            data.set(ndxProper, sortMe);
        }
    }
}

这是我的Quicksort:

if (numberToSort <= 15)
    insertionSort(data, firstIndex, numberToSort);
else {
    int partitionNdx = partition(data, firstIndex, numberToSort);

    int sizeLeft = (partitionNdx - firstIndex);
    int sizeRight = (numberToSort - sizeLeft - 1);

    quickSort(data, firstIndex, sizeLeft);
    quickSort(data, (firstIndex + sizeLeft + 1), sizeRight);
}

这是我的分区: medianOfThree方法只是随机数在索引范围内生成3个数字,将3个值存储在这些索引中,并使用中位数作为支点......

int pivotNdx = medianOfThree(data, firstIndex, numberToPartition);
String temp = data.get(firstIndex);
String pivot = data.get(pivotNdx);

data.set(firstIndex, pivot);
data.set(pivotNdx, temp);

int TooBigNdx = (firstIndex + 1), TooSmallNdx = (firstIndex + numberToPartition - 1);

while (TooBigNdx < TooSmallNdx) {
    while ((TooBigNdx < TooSmallNdx) && ((data.get(TooBigNdx)).compareTo(pivot) <= 0))
        TooBigNdx++;

    while ((TooSmallNdx > firstIndex) && ((data.get(TooSmallNdx)).compareTo(pivot) > 0))
        TooSmallNdx--;

    if (TooBigNdx < TooSmallNdx) {
        temp = data.get(TooSmallNdx);
        String TooBig = data.get(TooBigNdx);

        data.set(TooSmallNdx, TooBig);
        data.set(TooBigNdx, temp);
    }

}

if (pivot.compareTo(data.get(TooSmallNdx)) >= 0) {
    temp = data.get(firstIndex);
    String TooSmall = data.get(TooSmallNdx);

    data.set(firstIndex, TooSmall);
    data.set(TooSmallNdx, temp);

    return TooSmallNdx;
} else
    return firstIndex;

这是我的MergeSort方法:

if (numberToSort <= 15)
    insertionSort(data, firstIndex, numberToSort);
else {
    int sizeLeft = (numberToSort / 2);
    int sizeRight = (numberToSort / 2) + (numberToSort % 2);

    mergeSort(data, firstIndex, sizeLeft);
    mergeSort(data, (firstIndex + sizeLeft), sizeRight);

    merge(data, firstIndex, sizeLeft, sizeRight);
}

这是我的合并方法:

ArrayList<String> temp = new ArrayList<String>();
int ndxTemp = 0, ndxLeft = firstIndex, ndxRight = (firstIndex + leftSegmentSize), comp, ndx;
String left, right;
int sizeLeft = leftSegmentSize, sizeRight = rightSegmentSize;

while ((sizeLeft > 0) || (sizeRight > 0)) {
    if ((sizeLeft > 0) && (sizeRight > 0)) {
        left = data.get(ndxLeft);
        right = data.get(ndxRight);

        comp = left.compareTo(right);
        if (comp <= 0) {
            temp.add(data.get(ndxLeft++));
            sizeLeft--;
        } else {
            temp.add(data.get(ndxRight++));
            sizeRight--;
        }
    } else {
        if (sizeLeft > 0) {
            temp.add(data.get(ndxLeft++));
            sizeLeft--;
        } else {
            temp.add(data.get(ndxRight++));
            sizeRight--;
        }
    }
}

ndx = firstIndex;
for (ndxTemp = 0; ndxTemp < temp.size(); ndxTemp++)
    data.set(ndx++, temp.get(ndxTemp));

请帮我弄清楚错误...

1 个答案:

答案 0 :(得分:0)

我问题的根源在于这行代码:

for (ndx = (1 + firstIndex); ndx < numberToSort; ndx++) {

特别是:

ndx < numberToSort

这应该是:

ndx < (numberToSort + firstIndex), as we are not necessarily starting at 0.