I can't sort the arrangements well.

Asked 2 years ago, Updated 2 years ago, 32 views

Java is trying to sort the arrays in ascending and descending order by defining the methods "arraysSort", "display", and "change".

I can output well before sorting, but I don't know how to output ascending and descending data after sorting.
I just turned it around with a for sentence, but the number was so messy that I couldn't print it out well.
I apologize for the inconvenience, but please let me know.

Source Code

package sample;

public class Kadai5_7 {

    public static void main(String[]args) {
        // Declare and initialize array data
        int[]arrays1 = {96,45,31,29,84,77};
        // View in array before sorting
        display(arrays1,false);
        // Sort in ascending order
        arraysSort (arrays1, true);
        // Display in sorted array
        display(arrays1,true);
        // Sort in descending order
        arraysSort (arrays1, false);
        // Display in sorted array
        display(arrays1,true);
    }

    public static void arraysSort (int[]) array, boolean orderType) {
        if(orderType==true){
            for(inti=0;i<array.length;i++){
                for(int j=i;j<array.length;j++){
                    if(array[i]<array[j]){
                        change(array,i,j);
                    }
                }
            }
        } else{
            for(inti=0;i<array.length;i++){
                for(int j=i;j<array.length;j++){
                    if(array[i]>array[j]){
                        change(array,i,j);
                    }
                }
            }
        }
    }

    public static void display(int[] array, boolean isSorted) {
        if(isSorted==true){
            US>System.out.println("**** After Sort ****");
        } else{
            System.out.println("*****Before sorting***");
            for(inti=0;i<array.length;i++){
                System.out.print(array[i]+",");
            }
        }

    }

    public static void change (int[]) array, inti, intj) {
        int[]tmp = new int [array.length];
        tmp[i] = array[i];
        tmp[j] = array[j];
        array[i] = tmp[i];
        array[j] = tmp[j];
    }
}

java

2022-09-30 19:53

1 Answers

"There is no change in ""outputting array elements"" before and after sorting, so you can use the same process as before sorting."


    public static void display(int[] array, boolean isSorted) {
        if(isSorted==true){
            US>System.out.println("**** After Sort ****");
        } else{
            System.out.println("*****Before sorting***");
        }

        // Array output is the same before and after rearrangement.
        for(inti=0;i<array.length;i++){
            System.out.print(array[i]+",");
        }
        System.out.println();
    }

It's different from what I asked, but I think the output is probably different from what I expected.(Specifically, "After sorting" is not sorted either.)

This is due to the fact that the change method has not been replaced, and that the arraysSort method reverses the ascending/descending order determination (so this is not an output processing issue).

(Note: Looking back at the title, I noticed that sorting itself was also mentioned.)

I think what you want to do with the change method is to replace the i and j elements of the array, but the current code is not able to do that.
I'd probably like to do something like this:

public static void change (int[]) array, inti, intj) {
        int tmp = array [i];
        array[i] = array[j];
        array[j] = tmp;
    }


2022-09-30 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.