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];
}
}
"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;
}
545 Understanding How to Configure Google API Key
525 Uncaught (inpromise) Error on Electron: An object could not be cloned
545 PHP ssh2_scp_send fails to send files as intended
545 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
683 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.