Skip to content

Instantly share code, notes, and snippets.

@josinSbazin
Last active June 6, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josinSbazin/0b89f5162cbd3109977317a6338814bf to your computer and use it in GitHub Desktop.
Save josinSbazin/0b89f5162cbd3109977317a6338814bf to your computer and use it in GitHub Desktop.
Сортировка пузырьком
import java.util.*;
public class Program {
public static void main(String[] args) {
long start = System.nanoTime(); //померять время выполнения
int [] array = new int[150];
Random random = new Random();
int er=0;
//заполняем массив случайными числами
for (int i=0; i<array.length; i++) {
array[i] = random.nextInt(1000)-500;
//System.out.println (array[i]); //Посмотреть, что в массиве
}
System.out.println("sorting:");
int end = array.length-1;
//сортируем
while(true) {
er=0;
for(int j=0; j<(end); j++) {
if (array[j]>array[j+1]) {
int temp=array[j+1];
array[j+1]=array[j];
array[j]=temp;
//если есть элементы не попорядку, то прибавляем единицу к er
er++;
}
}
if (er==0) break; //если нет неотсортированных элементов, то прирываем сортировку
end--; //в конце оказалось максимальное значение, его не сортируем.
}
for (int v:array) System.out.println(v);
long end=System.nanoTime();
System.out.println((double)(end-start)/1000000000); // время сортировки в секундах
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment