Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.module2hw;
- import java.lang.reflect.Array;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- import java.util.Random;
- public class Main {
- private static Random rand = new Random();
- private static int n1 = 100000, n2 = 120000;
- public static void main(String[] args) {
- //how the heck we are comparing c++ with java using different random data sets???
- Long[] arr1 = generateNumbers(n1);
- Long[] arr2 = generateNumbers(n2);
- Long startTime = System.nanoTime();
- Arrays.sort(arr1);
- Arrays.sort(arr2);
- Long[] arr3 = mergeSortedArrays(arr1,arr2);
- Long time = System.nanoTime() - startTime;
- DecimalFormat formatter = new DecimalFormat("#0.000000000");
- System.out.println("Elapsed time without arrays generation: " + formatter.format(time/1e9) + " sec.");
- }
- private static Long[] generateNumbers(int n) {
- Long[] array = new Long[n];
- for(int i = 0; i < array.length; i++) {
- array[i] = rand.nextLong();
- }
- return array;
- }
- private static Long[] mergeSortedArrays(Long[] arr1, Long[] arr2) {
- int i = 0, j = 0, k = 0;
- Long[] result = new Long[arr1.length + arr2.length];
- //part of merge sort algorithm
- while(i < arr1.length && j < arr2.length) {
- result[k++] = arr1[i] <= arr2[j] ? arr1[i++] : arr2[j++];
- }
- while(i < arr1.length) result[k++] = arr1[i++];
- while(j < arr2.length) result[k++] = arr2[j++];
- //-=-=-=-=-=-
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement