Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Main {
- public static volatile long start;
- public static volatile long end;
- public static void main(String[] args) {
- Random r = new Random();
- int[] testArray1 = new int[100000];
- int[] testArray2 = new int[100000];
- int[] testArray3 = new int[100000];
- int[] testArray4 = new int[100000];
- for (int i = 0; i < testArray1.length; i++) {
- testArray1[i] = r.nextInt(100000);
- testArray2[i] = testArray1[i];
- testArray3[i] = testArray1[i];
- testArray4[i] = testArray1[i];
- }
- Thread t1 = new Thread(() -> {
- InsertionSort(testArray1);
- if(end == 0){
- end = 999;
- } else {
- end = System.currentTimeMillis();
- System.out.println(end - start);
- }
- });
- Thread t2 = new Thread(() -> {
- BubbleSort(testArray2);
- if(end == 0){
- end = 999;
- } else {
- end = System.currentTimeMillis();
- System.out.println(end - start);
- }
- });
- start = System.currentTimeMillis();
- InsertionSort(testArray3);
- BubbleSort(testArray4);
- end = System.currentTimeMillis();
- System.out.println("Thread nΓ©lkΓΌl: ");
- System.out.println(end - start);
- System.out.println("\nThreaddel:");
- end = 0;
- start = System.currentTimeMillis();
- t1.start();
- t2.start();
- }
- public static void InsertionSort(int[] data) {
- for (int i = 0; i < data.length - 1; i++) {
- for (int j = i + 1; j > 0; j--) {
- if (data[j - 1] < data[j]) {
- int temp = data[j - 1];
- data[j - 1] = data[j];
- data[j] = temp;
- }
- }
- }
- }
- public static void BubbleSort(int[] data) {
- for (int i = 0; i < data.length; i++) {
- for (int j = 1; j < (data.length - i); j++) {
- if (data[j - 1] < data[j]) {
- //swap elements
- int temp = data[j - 1];
- data[j - 1] = data[j];
- data[j] = temp;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement