为什么我的排序算法不起作用?

Sar*_*eir 5 java

代码应该这样做:a)给定一个未排序的整数数组,你的任务是通过应用以下算法对数组进行排序(假设输入不包含重复项):从数组中的第一个元素开始执行以下步骤: - 计算较小元素的数量,以找到正确的位置i. - 如果元素处于正确位置,请移至后续元素. - 否则,将当前元素与位置i中找到的元素交换. - 重复前面的步骤,直到到达最后一个元素.

示例:5 7 3 6 9检查a [0],有一个元素小于它,因此它应与position1处的元素交换.7 5 3 6 9检查新元素a [0].它应该移动到位置3. 6 5 3 7 9检查新元素a [0].它应该移动到位置2. 3 5 6 7 9检查新元素a [0].它处于正确的位置,所以我们移动到后续元素a [1].

public class  Assignment1_T11_25_2729_Sara_Aly {
private int[] a;
private int max;
private int n;
int position=0;
public  Assignment1_T11_25_2729_Sara_Aly (int max){
    a= new int[max];
}
public void insert(int x){
    a[n]=x;
    n++;
        }
public void sort(){
    int out=0, smaller=0;
    while(out<n){
        for(int in=out+1;in<n;n++){
            if(a[in]<a[out])
                smaller++;
        }
        if (smaller==0){
            out++;
        }
        else {
            swap(a[out], a[smaller]);
        }
    }
    }
private void swap (int one, int two){
    int temp=a[one];
    a[one]=a[two];
    a[two]=temp;
}
    public void display(){
        for (int i=0;i<n;i++){
            System.out.print(a[i]+ " ");
        }
        System.out.println("");
    }
public static void main(String[]args){
    int maxsize=5;
    Assignment1_T11_25_2729_Sara_Aly trial;
    trial= new  Assignment1_T11_25_2729_Sara_Aly(maxsize);
    trial.insert(5);
    trial.insert(7);
    trial.insert(3);
    trial.insert(6);
    trial.insert(9);
    trial.display();
    trial.sort();
    trial.display();
}
}

    Tried a few algorithims to get it to work but for some reason it won't sort any suggestions??
Run Code Online (Sandbox Code Playgroud)

也尝试了这种排序方法,但没有运气.

public void sort(){
boolean finished = false;
int position =0;
    while (position<max){
        if (finished==true){
        position++;
        finished =false;
        }
        else {
            int smaller=0;
            for (int j = position+1; j<max; j++){
            int temp=a[position];
                if (a[j] <a[position]){
                    smaller++;
                }
            }
            if (smaller==0){
                finished= true;
            }
            else {
                int temp= a[smaller];
                a[smaller]=a[position];
                a[position]=temp;
                }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

Roh*_*ain 2

尽管您没有准确描述问题是什么,但我认为在您的方法循环first code内部for-loop给您带来了问题:-whilesort

    for(int in = out+1; in < n; n++) {
        if(a[in] < a[out])
            smaller++;
    }
Run Code Online (Sandbox Code Playgroud)

在这里,您是递增n++而不是in++。检查一下。将其更改为in++. 您可能会因此陷入无限循环。

另外,你的方法也有问题swap。您已swap使用实际数组元素调用了方法,但您将它们视为indices方法中的元素。

swap(a[out], a[smaller]);  // Called with element `a[out]`

private void swap (int one, int two) {

    int temp=a[one];    // This is equivalent to a[a[out]]
    a[one]=a[two];
    a[two]=temp;
}
Run Code Online (Sandbox Code Playgroud)

您可以将索引传递给您的方法:-

因此,调用您的交换方法,例如:-swap(out, smaller);

更新:-在方法while循环中sort,添加smaller = 0;作为第一个语句。重新初始化为0。