Java 我的快速排序和合并排序功能不正常
当段为ndxProper时,我的MergeSort和QuickSort调用插入排序;ndxCopy--) data.set(ndxCopy,data.get(ndxCopy-1)); 数据集(ndxProper,sortMe); } } } 这是我的快速排序:Java 我的快速排序和合并排序功能不正常,java,string,sorting,arraylist,quicksort,Java,String,Sorting,Arraylist,Quicksort,当段为ndxProper时,我的MergeSort和QuickSort调用插入排序;ndxCopy--) data.set(ndxCopy,data.get(ndxCopy-1)); 数据集(ndxProper,sortMe); } } } 这是我的快速排序: int ndxCopy = 0, ndx, ndxSort, comp, result, ndxProper; String sortMe = null; for (ndx = (1 + firstIndex); ndx < nu
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);
}
}
}
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);
}
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));
if(numberToSort 0))
Toosmalndx--;
if(Toobingndx=0){
temp=data.get(firstIndex);
字符串toosmal=data.get(toosmalndx);
data.set(firstIndex,太小);
数据集(TOOSMOLLNDX、temp);
返回smallndx;
}否则
返回第一个索引;
这是我的合并排序方法:
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);
}
}
}
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);
}
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));
if(numberToSort 0)| |(sizeRight>0)){
如果((sizeLeft>0)和&(sizeRight>0)){
左=data.get(ndxLeft);
右=data.get(ndxRight);
comp=左。与(右)相比;
如果(组件0){
临时添加(数据获取(ndxLeft++);
西泽夫特;
}否则{
temp.add(data.get(ndxRight++));
大小适中--;
}
}
}
ndx=第一指数;
对于(ndxTemp=0;ndxTemp
请帮我找出问题所在…我的问题根源在于以下代码行:
for (ndx = (1 + firstIndex); ndx < numberToSort; ndx++) {
for(ndx=(1+firstIndex);ndx
特别是:
ndx < numberToSort
ndx
这应该是:
ndx < (numberToSort + firstIndex), as we are not necessarily starting at 0.
ndx<(numberToSort+firstIndex),因为我们不一定从0开始。