Start New Discussion within our Software Development Community

Hey I need help with creating this program. It's supposed to take the values I've assigned in int[]A and put them into int[]C in order from least to greatest (for example, C[0] would be 1, C[1] would be 3, etc.) Yes, this is a homework problem, but I've been trying it for a while now. I don't expect the answer directly, just some guidance. This is what I have so far:

public class Sort{
    public static void main(String [] args){
        int[]A={3,7,8,1,4,9,10};
        int[]C=sort(A);
        for(int i=0;i<C.length;i++){
            System.out.print(C[i] +",");
        }
    }
    
    public static int[] sort(int[]A){
        int[]C=new int[A.length];
        int min=A[0];
        for(int g=0;g<C.length;g++){
            for(int i=1;i<C.length;i++){
                if(A[i]<min){
                   min=A[i];
                }
                C[g]=min;
                for(int z=0;z<C.length;z++){
                    if(A[z]==min){
                       A[z]=11;
                    }
        }
        return C;
    }
    }
return C;
}
}

I have "return C" twice because it kept giving me a "missing return statement" compile error. Thanks.

try this

public class Sort{
    public static void main(String [] args){
        int[]A={3,7,8,1,4,9,10};
        A=sort(A);
        for(int i=0;i<A.length;i++){
            System.out.print(A[i] +",");
        }
    }
    
    public static int[] sort(int[]A){
        for(int g=0;g<A.length;g++){
            int min=A[g];
            for(int h=g+1;h<A.length;h++){
                if(min>A[h]){
                    min=A[h];
                    int temp=A[g];
                    A[g]=A[h];
                    A[h]=temp;
                }
            }
                
            
        }
        return A;
    }
}

Basically you start from 0 and check if all the values after it are bigger, and if they arent you swap them.

This article has been dead for over six months. Start a new discussion instead.