Showing posts with label merge sort in java. Show all posts
Showing posts with label merge sort in java. Show all posts

Tuesday, October 8, 2019

Merge Sort in Java

About the code, it is called a merge sort program is written in Java programming language. Merge sort is another sorting algorithm the is widely used in arranging numbers and strings. It was developed by Dr. John Von Neumann in 1945.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.


My personal website is http://www.jakerpomperada.com.



Sample Program Output




Wednesday, September 13, 2017

Merge Sort in Java

Here is the implementation of Merge Sort using Java as my programming language. Our program will ask the user how many items to be sorted and then it will ask the user to give a series of numbers and then it will be sorted using selection sort algorithm. I hope you will find my work useful. 

I am currently accepting programming work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

/**
 * 
 */
package merge_sort;

import java.util.Scanner;

/**
 * @author Jake R. Pomperada
 * Bacolod City, Negros Occidental, Philippines
 * September 13, 2017   Wednesday
*/


public class Merge_Sorting {

/**
* @param args
*/
public static void merge_sort(int[] arr, int low, int high) 
    {
        int N = high - low;         
        if (N <= 1) 
            return; 
        int mid = low + N/2; 
        
        merge_sort(arr, low, mid); 
        merge_sort(arr, mid, high); 
        
        int[] temp = new int[N];
        int i = low, j = mid;
        for (int k = 0; k < N; k++) 
        {
            if (i == mid)  
                temp[k] = arr[j++];
            else if (j == high) 
                temp[k] = arr[i++];
            else if (arr[j]<arr[i]) 
                temp[k] = arr[j++];
            else 
                temp[k] = arr[i++];
        }    
        for (int k = 0; k < N; k++) 
            arr[low + k] = temp[k];         
    }
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
     
System.out.print("\n\n");
System.out.print("===== Merge Sort in Java ====");
System.out.print("\n\n");
System.out.print("===== Written By: Mr. Jake R. Pomperada =====");;
System.out.print("\n\n");
System.out.print("How many items? : ");
        int num = input.nextInt();
        int numbers[] = new int[num];

System.out.println();
        for (int i = 0; i < num; i++) {
            System.out.print ("Give value in item no. " + (i+1) + " : ");
            numbers[i] = input.nextInt();
        }

        System.out.print("\n\n");
System.out.print("Original Number Arrangements" );
System.out.print("\n\n");
        for (int temp : numbers){
        System.out.print(temp);
            System.out.print(", ");
        } 
        System.out.print("\n\n");
System.out.print("Sorted Number Arrangements" );
System.out.print("\n\n");
        
        
        merge_sort(numbers,0,num);
        for(int a:numbers){
            System.out.print(a);
            System.out.print(", ");
}
        input.close();

}

}