Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的排序算法在对一组对象进行排序时可能会遇到什么问题?_Java_Sorting_Quicksort - Fatal编程技术网

Java 我的排序算法在对一组对象进行排序时可能会遇到什么问题?

Java 我的排序算法在对一组对象进行排序时可能会遇到什么问题?,java,sorting,quicksort,Java,Sorting,Quicksort,我有一个对象数组。每个对象都将按作为对象成员的字符串进行排序。以下是保存的字符串 "Mead Johnson" "Heartlight Corp Inc" "Learning Horizons" "Block Drug Co" "Block Drug Co" "Warner Lambert Consumer Healthcare" "Novartis Consumer Health Inc" "Bionutrics Health Products Inc" "Warner Lambert Con

我有一个对象数组。每个对象都将按作为对象成员的字符串进行排序。以下是保存的字符串

"Mead Johnson"
"Heartlight Corp Inc"
"Learning Horizons"
"Block Drug Co"
"Block Drug Co"
"Warner Lambert Consumer Healthcare"
"Novartis Consumer Health Inc"
"Bionutrics Health Products Inc"
"Warner Lambert Consumer Healthcare"
"Barilla America"
"Bionutrics Health Products Inc"
这是我得到的输出顺序:

"Bionutrics Health Products Inc"
"Block Drug Co"
"Block Drug Co"
"Heartlight Corp Inc"
"Learning Horizons"
"Barilla America"
"Mead Johnson"
"Novartis Consumer Health Inc"
"Warner Labert Consumer Healthcare"
"Warner Lambert Consumer Healthcare"
"Biobutrics Health Products Inc"
如您所见,列表几乎已排序。下面是我使用的代码

private void sort(Manufacturer[] manuArray, int left, int right)
{
    int pivotValue;
    if(left < right)
    {
        pivotValue = pivot(manuArray, left,right);
        sort(manuArray, left, pivotValue-1);
        sort(manuArray, pivotValue+1,right);
    }
}

private  int pivot(Manufacturer[] manuArray, int left, int right)
{
    int p = left;
    String pivotName = manuArray[left].getName();
    for(int i = left + 1; i <= right; i++)
    {
        if(manuArray[i].getName().compareToIgnoreCase(pivotName) < 0)
        {
            p++;
            swap(manuArray, i, p);
        }
    }
    swap(manuArray, p, left);
    return p;
}

private void swap(Manufacturer[] manuArray, int i, int p)
{
    Manufacturer tempManu1 = manuArray[i];
    Manufacturer tempManu2 = manuArray[p];
    manuArray[i] = manuArray[p];
    manuArray[p] = tempManu1;
}
private void排序(制造商[]数组,左整数,右整数)
{
整型数据透视值;
if(左<右)
{
pivotValue=pivot(手动数组、左、右);
排序(数组,左,数据透视值-1);
排序(数组,数据透视值+1,右);
}
}
专用整数枢轴(制造商[]阵列,整数左,整数右)
{
int p=左;
字符串pivotName=manuArray[left].getName();

对于(int i=left+1;i尝试在循环中不求和1:

for(int i = left; i < right; i++)
{
...
for(int i=left;i
如果没有,请尝试将此代码改编为您的代码。我是通过增加距离而不是按字母顺序进行排序:

public static void ordenarChollos(List<CholloObjeto> chollostemp, int izq, int der) {
    CholloObjeto pivote=chollostemp.get(izq); 
    int i=izq; 
    int j=der; 
    CholloObjeto aux;

    while(i<j){           
        while(chollostemp.get(i).getDistance() <= pivote.getDistance() && i<j) i++; 
        while(chollostemp.get(j).getDistance() > pivote.getDistance()) j--;         
        if (i<j) {                      
            aux = chollostemp.get(i);                 
            chollostemp.set(i,chollostemp.get(j));
            chollostemp.set(j,aux);
        }
    }
    chollostemp.set(izq,chollostemp.get(j)); 
    chollostemp.set(j,pivote); 
    if(izq<j-1)
        ordenarChollos(chollostemp,izq,j-1); 
    if(j+1 <der)
        ordenarChollos(chollostemp,j+1,der); 
}
publicstaticvoidordenarchollos(列表chollosttemp、int-izq、int-der){
CholloObjeto pivote=CHOLLOSTTEMP.get(izq);
int i=izq;
int j=der;
乔洛比耶托奥克斯;

while(这是一个家庭作业问题?当quicksort是Java本机时,为什么要重新实现它?是的,我们必须创建自己的。很抱歉,我没有遵循您在文章中for循环部分的建议。我的意思是,在pivot函数中,从“左”开始for循环,而不是从“左+1”开始作为尝试的建议。在我的函数中,我使用while循环。我的chollostemp对象与manuArray对象相同。您使用的是getName,而我使用的是getDistance。字母顺序/米顺序的距离。如果我将while语句中的距离与>进行比较,甚至与for循环更改进行比较,我仍然收到相同的问题莱姆。
for(int i = left; i < right; i++)
{
...
public static void ordenarChollos(List<CholloObjeto> chollostemp, int izq, int der) {
    CholloObjeto pivote=chollostemp.get(izq); 
    int i=izq; 
    int j=der; 
    CholloObjeto aux;

    while(i<j){           
        while(chollostemp.get(i).getDistance() <= pivote.getDistance() && i<j) i++; 
        while(chollostemp.get(j).getDistance() > pivote.getDistance()) j--;         
        if (i<j) {                      
            aux = chollostemp.get(i);                 
            chollostemp.set(i,chollostemp.get(j));
            chollostemp.set(j,aux);
        }
    }
    chollostemp.set(izq,chollostemp.get(j)); 
    chollostemp.set(j,pivote); 
    if(izq<j-1)
        ordenarChollos(chollostemp,izq,j-1); 
    if(j+1 <der)
        ordenarChollos(chollostemp,j+1,der); 
}