Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

merge sort java

package Arrays;

import java.util.Arrays;

public class MergeSort {
    static void sort(int[] arr) {
        if (arr.length < 2) return;

        int mid = arr.length / 2;

        int[] left_half = new int[mid];
        int[] right_half = new int[arr.length - mid];

        // copying the elements of array into left_half
        for (int i = 0; i < mid; i++) {
            left_half[i] = arr[i];
        }
        
        // copying the elements of array into right_half
        for (int i = mid; i < arr.length; i++) {
            right_half[i - mid] = arr[i];
        }

        sort(left_half);
        sort(right_half);
        merge(arr, left_half, right_half);
    }
  
    static void merge(int[] arr, int[] left_half, int[] right_half) {
        int i = 0, j = 0, k = 0;

        while (i < left_half.length && j < right_half.length) {
            if (left_half[i] < right_half[j]) {
                arr[k++] = left_half[i++];
            }
            else {
                arr[k++] = right_half[j++];
            }
        }
        while (i < left_half.length) {
            arr[k++] = left_half[i++];
        }
        while (j < right_half.length) {
            arr[k++] = right_half[j++];
        }
    }
    public static void main(String[] args) {
        int[] arr = {5, 1, 7, 3, 8, 0, 1, 5, 7, 2, 8, 9, -7, 4, -9, -3, 4};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
Comment

mergesort java

public class MergeSort
{
public static void main(String[] args)
{
int i;
int values[] = new int[11];
for(i=0; i<values.length; i++) { values[i]=values.length-i; }
print(values);
sort(values,0,values.length-1);
print(values);
}
public static void sort(int[] numbers, int p,int r)
{
int q;
if(p<r)
{
q = (p+r)/2;
sort(numbers,p,q);
sort(numbers,q+1,r);
merge(numbers,p,q,r);
}
}
/**
* p, mid, and r are indices into the array such that p <= mid < r.
* The procedure assumes that the subarrays 'numbers[p..mid]' and 'numbers[mid+1..r]' are
* in sorted order. It merge them to form a single sorted subarray that replaces
* the current subarray 'numbers[p..r]'.
*/
private static void merge(int[] values, int p, int mid, int r)
{
int i,j,k;
int n1 = mid - p + 1;
int n2 = r - mid;
int[] left = new int[n1+1];
int[] right = new int[n2+1];
for(i=0; i<n1; i++)
{
left[i] = values[ p + i ];
}
for(j=0; j<n2; j++)
{
right[j] = values[mid + j + 1];
}
left[n1] = Integer.MAX_VALUE;
right[n2] = Integer.MAX_VALUE;
i=0; j=0;
for(k=p; k<=r; k++)
{
if(left[i]<=right[j])
{
values[k] = left[i];
i = i + 1;
}
else
{
values[k] = right[j];
j = j + 1;
}
}
}
public static void print(int[] numbers)
{
int i;
for(i=0; i<numbers.length; i++)
{
System.out.print(numbers[i]+ " ");
}
System.out.println();
}
}
Comment

PREVIOUS NEXT
Code Example
Java :: java integer object to char 
Java :: Write a method multiply() in a class Arithmetic 
Java :: java 14 switch 
Java :: parameterized constructor java 
Java :: insertion sort java 
Java :: random value between 10-20 
Java :: how to remove last latter in word in java 
Java :: how to start array index from 1 in java 
Java :: spring boot send api request 
Java :: java data structure 
Java :: Java Change ArrayList Elements 
Java :: different constructiors in java and what they do explained 
Java :: java create a string 
Java :: java to kotlin 
Java :: java @override annotation 
Java :: Java Iterating through LinkedList 
Java :: private method in interface java 
Java :: arraylist sort 
Java :: creating a properties object using a file 
Java :: spring mongodb 
Java :: output folder director 
Java :: how to choose an element in a array list Java 
Java :: assign a random number in a set without replacement javva 
Java :: generate random color java 
Java :: coin flip random java 
Java :: Which of the below is a correct identifier for a method name in Java * 2 points 0start #stop 0_start *start# 
Java :: longest subarray with equal 0 and 1 
Java :: Java TestNG Data Provider 
Java :: how to split string with dolor sign in java 
Java :: printing array in descending order 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =