Answers for "merge sort implementation java"

4

merge sort java

public static void mergeSort(int[] a, int n) {
    if (n < 2) {
        return;
    }
    int mid = n / 2;
    int[] l = new int[mid];
    int[] r = new int[n - mid];
 
    for (int i = 0; i < mid; i++) {
        l[i] = a[i];
    }
    for (int i = mid; i < n; i++) {
        r[i - mid] = a[i];
    }
    mergeSort(l, mid);
    mergeSort(r, n - mid);
 
    merge(a, l, r, mid, n - mid);
}

public static void merge(
  int[] a, int[] l, int[] r, int left, int right) {
 
    int i = 0, j = 0, k = 0;
    while (i < left && j < right) {
        if (l[i] <= r[j]) {
            a[k++] = l[i++];
        }
        else {
            a[k++] = r[j++];
        }
    }
    while (i < left) {
        a[k++] = l[i++];
    }
    while (j < right) {
        a[k++] = r[j++];
    }
}
Posted by: Guest on April-12-2021
0

how to write a merge sort array method ni java

public static void mergeSort(int[] a, int n) {
    if (n < 2) {
        return;
    }
    int mid = n / 2;
    int[] l = new int[mid];
    int[] r = new int[n - mid];
 
    for (int i = 0; i < mid; i++) {
        l[i] = a[i];
    }
    for (int i = mid; i < n; i++) {
        r[i - mid] = a[i];
    }
    mergeSort(l, mid);
    mergeSort(r, n - mid);
 
    merge(a, l, r, mid, n - mid);
}
Posted by: Guest on March-07-2020
0

how to write a merge sort array method ni java

public static void merge(
  int[] a, int[] l, int[] r, int left, int right) {
  
    int i = 0, j = 0, k = 0;
    while (i < left && j < right) {
        if (l[i] <= r[j]) {
            a[k++] = l[i++];
        }
        else {
            a[k++] = r[j++];
        }
    }
    while (i < left) {
        a[k++] = l[i++];
    }
    while (j < right) {
        a[k++] = r[j++];
    }
}
Posted by: Guest on March-07-2020
0

merge sort java

import java.util.Random;
import java.util.Scanner;

public class MergeSort {
    public static void main(String[] argh){
        Scanner scanner  = new Scanner(System.in);
        System.out.println("Enter the size of you victor: ");
        int size = scanner.nextInt();
        int[] unSortedArr = new int[size];
        unSortedArr = enterRandomValues(unSortedArr);
        int[] sortedARR = loop(unSortedArr);
        print(sortedARR);
    }
    public static int[] loop(int[] unSortedArr){
        for(int start = 0 ; start < unSortedArr.length ; ++start){
            int minIndex = findTheMiniIndex(unSortedArr,start);
            swap(unSortedArr,minIndex,start);
        }

        return unSortedArr ;
    }

    public static int findTheMiniIndex(int[] arr, int start){
        int index = start;
        for(int i = start+1 ; i < arr.length ; ++i){
            if(arr[index]>arr[i]){
                index = i ;
            }
        }

        return index;
    }

    public static void swap(int[] swapValOfArr,int minIndex,int start){
        int temp = swapValOfArr[minIndex];
        swapValOfArr[minIndex]= swapValOfArr[start]  ;
        swapValOfArr[start] = temp ;

    }

    public static void print(int[] sorteArr){
        for(int i = 0 ; i < sorteArr.length ; ++i) {
            System.out.println(sorteArr[i]);
        }
    }

    public static int[] enterRandomValues(int[] unSortedArr){
        Random random = new Random();
        for(int i = 0 ; i< unSortedArr.length; ++i){
            unSortedArr[i] =random.nextInt(1000);
        }
        return unSortedArr;
    }
}
Posted by: Guest on July-17-2021
0

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();
}
}
Posted by: Guest on April-15-2021

Code answers related to "merge sort implementation java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language