Answer the question
In order to leave comments, you need to log in
Why does the sorting algorithm work wrong if I call it via a method?
package WorkSpace;
import Edition.Array;
import java.util.*;
public class WorkSpace {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.print("кол-во эл-ов массива: ");
int scan_Number = scan.nextInt();
int[] box1 = new int[scan_Number];
Array.array_Add(box1);
Array.array_Sort(box1);
}
}
package Edition;
public class Array{
//СОРТИРОВКА массива
public static void array_Sort(int[] array){
boolean bool = false;
while (!bool){
for(int i=0;i<array.length-1;i++){
bool = true;
if(array[i]>array[i+1]){
bool=false;
int tmp = array[i];
array[i]=array[i+1];
array[i+1]=tmp;
}
}
}
//ВЫВОД массива
for(int i: array){
System.out.print(i+" ");
}
System.out.println();
}
//ЗАПИСЬ массива
public static void array_Add(int[] array){
for(int i = 0; i<array.length;i++){
array[i]=(int)(Math.random()*(9+1)+1);
}
for(int i: array){
System.out.print(i+" ");
}
System.out.println();
}
}
Answer the question
In order to leave comments, you need to log in
Because you are trying to implement a linear sorting algorithm in O(n), which is basically impossible.
The bubble needs a second loop inside the first one.
And sometimes it works because randomly wished it so and generated a sequence for you with the right number of inversions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question