1

So my Professor gave us an activity where we input the amount of array and its value. Then Selection sort will be implemented and will display each pass. I manage to do the input part but only managed to display the unsorted and sorted output. I've been wracking my brains on how to show the input of each pass but I always come up short. I saw a code in C/C++ but I don't know how to read the language well enough to understand the code.

This is what the expected output:

Input a number: 5

a[0]: 5

a[1]: 4

a[2]: 2

a[3]: 8

a[4]: 10

UnSorted Array: 5  4  2  8  10 

1st Pass: 5 4 2 8 10

2nd Pass: 5 4 2 8 10

3rd Pass: 2 4 5 8 10

Sorted Array Values :2  4  5  8  10  

And here's my current output:

Input a number: 5

a[0]: 5

a[1]: 4

a[2]: 2

a[3]: 8

a[4]: 10

UnSorted Array: 5  4  2  8  10  

Sorted Array Values: 2  4  5  8  10  

This is my code:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    /*
     * input number of array
     * display array
     * 1st pass
     * 2nd pass
     * 3rd 
     * 4th
     * 5th...
     */
    Scanner ss = new Scanner(System.in);
    int a, j, temp;  
    int arr[] = new int[50];  
    
    //input number of array
    System.out.print("Input a number: ");
    int num=ss.nextInt();
    System.out.print("");
    
    //display array
    for (a = 0; a < num; a++) {
        System.out.print("a[" + a + "]: ");
        arr[a] = ss.nextInt();
    }
    
    //Unsorted Value
    System.out.print("UnSorted Array: ");
    for (a = 0; a < num; a++) {
        System.out.print(arr[a] + "  ");
    }
    
    System.out.println();
    for (a = 0; a < num; a++) {
        for(j = a + 1; j < num; j++) {   
            if(arr[a] > arr[j]) {  
                temp = arr[a];  
                arr[a] = arr[j];  
                arr[j] = temp;  
            }
        }
    }
    //Passes
    
    //Sorted Value
    System.out.print("Sorted Array Values :");  
    for (a = 0; a < num; a++) {  
        System.out.print(arr[a] + "  ");  
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.