Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[] MergeSort(int[] array){
- System.out.println("This is my length:" + array.length);
- // keys for merging and the temporary array
- int i = 0; int j;
- int [] tempArray = new int[array.length];
- int spiderman;
- // sets j
- if((array.length%2) == 0){
- j = array.length/2 ;
- } else{
- j = (array.length-1)/2;
- }
- // tells spiderman what to do
- if((array.length%2) == 0){
- spiderman = array.length/2 ;
- } else{
- spiderman = (array.length-1)/2;
- }
- // copies first and second half of the array into 2 arrays
- int[] first = new int[j];
- int[] second = new int[array.length-j];
- /*
- int[] first = Arrays.copyOfRange(array, 0, (j-1));
- int[] second = Arrays.copyOfRange(array, j, (array.length-1));
- */
- for(int k = 0; k < (j-1); k++){
- first[k] = array[k];
- }
- for (int l = j; l < (second.length-1);l++){
- second[l] = array[l];
- }
- //if lists are larger than 1, call mergesort on these lists.
- if(first.length > 1){
- MergeSort(first);
- }
- if(second.length > 1){
- MergeSort(second);
- }
- // used for counting
- int q = 0; int p = 0;
- // used to remember where i left off, when the rest of the remaining array needs to be added
- int tempo = 0; int dumbo = 0;
- // merges divded lists.
- System.out.println(first.length + " and " + second.length);
- while(q < first.length && p < second.length){
- if(first[q] <= second[p]){
- tempArray[i] = first[q];
- q++;
- dumbo = q;
- i++;
- } else{
- tempArray[i] = second[p];
- p++;
- tempo = p;
- i++;
- }
- }
- System.out.println(i);
- if(q == first.length && !(p==array.length)){
- for(int f = tempo; f < first.length ;f++){
- tempArray[i] = first[f];
- i++;
- }
- }
- if(p == second.length && !(q == spiderman)){
- for(int g = dumbo; g < second.length ;g++){
- tempArray[i] = second[g];
- i++;
- }
- }
- for(int k = 0; k<tempArray.length; k++){
- System.out.println("temp array at place k:" + tempArray[k]);
- }
- System.out.println(i + " Is i");
- // copy tempArray to array.
- array = Arrays.copyOfRange(tempArray, 0, (tempArray.length - 1));
- System.out.println(i + " Is i");
- return array;
- }
- public static void main(String[] args) {
- // used to create 2 large lists to do analysis of runtime.
- Random r = new Random();
- int p = r.nextInt(50000);
- int[] list = new int[p];
- int[] list2 = new int[p];
- for(int q=0; q < p; q++){
- list[q] = r.nextInt(10000);
- list2[q] = r.nextInt(10000);
- }
- /*
- //first array insertion
- int[] array = {21, 50, 30, 15, 20, 55, 92, 82, 44, 5, 10, 21};
- System.out.println("This is my current array: "+ Arrays.toString(array));
- System.out.println("Length of this array: " +array.length);
- InsertionSort(array);
- System.out.println("This is my new array: "+ Arrays.toString(array));
- */
- /*
- // Random array insertion
- System.out.println("This is my current (random)array: "+ Arrays.toString(list));
- System.out.println("Length of this random array: " +list.length);
- InsertionSort(list);
- System.out.println("This is my new (random)array: "+ Arrays.toString(list));
- */
- System.out.println("TIME FOR MERGE!!");
- //first array merge
- int[] array1 = {21, 50, 30, 15, 20, 55, 92, 82, 44, 5, 10, 21};
- System.out.println("This is my current array: "+ Arrays.toString(array1));
- System.out.println("Length of this array: " +array1.length);
- long n = System.currentTimeMillis();
- System.out.println("Time when Merge starts: "+n+" but lets call it 0.");
- MergeSort(array1);
- long m = System.currentTimeMillis();
- System.out.println("Time when Merge is done: "+m+" difference is: "+(m-n));
- System.out.println("This is my new array: "+ Arrays.toString(array1));
- /*
- // Random array merge
- System.out.println("This is my current (random)array: "+ Arrays.toString(list2));
- System.out.println("Length of this random array: " +list2.length);
- MergeSort(list2);
- System.out.println("This is my new (random)array: "+ Arrays.toString(list2));
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement