Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

merge sort recursion java

/**
     * Takes in entire vector, but will merge the following sections together:
     * Left sublist from a[first]..a[mid], right sublist from a[mid+1]..a[last].
     * Precondition: each sublist is already in ascending order
     *
     * @param a
     *            reference to an array of integers to be sorted
     * @param first
     *            starting index of range of values to be sorted
     * @param mid
     *            midpoint index of range of values to be sorted
     * @param last
     *            last index of range of values to be sorted
     */
    private void merge(ArrayList<Comparable> a, int first, int mid, int last) {
        int x;
        int i;
        ArrayList<Comparable> left = new ArrayList<Comparable>();
        ArrayList<Comparable> right = new ArrayList<Comparable>();
        mergeSort(a,first,mid);
        for(i = 0; i < a.size() - mid; i++){
            left.add(i,a.get(i));
            a.remove(i);
        }
        mergeSort(a,mid,last);
        for (x = mid; x < a.size(); x++) {
            right.add(x,a.get(x));
            a.remove(x);
        }
        if ((left.get(i).compareTo(right.get(x))) > 0) {
            i++;
            a.add(i);
        } else if (i < x) {
            x++;
            a.add(x);
        }


        System.out.println();
        System.out.println("Merge");
        System.out.println();

    }

    /**
     * Recursive mergesort of an array of integers
     *
     * @param a
     *            reference to an array of integers to be sorted
     * @param first
     *            starting index of range of values to be sorted
     * @param last
     *            ending index of range of values to be sorted
     */
    public void mergeSort(ArrayList<Comparable> a, int first, int last) {

        int mid = (first + last)/2;
        if(first == last){

        }else if(last - first == 1){
            merge(a,first, mid ,last);              
        }else{
            last = mid;
        }


                }
Comment

PREVIOUS NEXT
Code Example
Java :: array to array list java 
Java :: java design patterns 
Java :: navigation graph setup android 
Java :: How to perform in-order traversal of a binary tree? 
Java :: use custom font java 
Java :: java draw image 
Java :: java first index of an arraylist 
Java :: run jar file with different jre 
Java :: what is return method 
Java :: how to divide a double in java 
Java :: why string is immutable in java 
Java :: check if object is empty java 8 
Java :: what is deserialization in java 
Java :: kafkalistener annotation pass topic from properties file 
Java :: output folder director 
Java :: android activity set action bar options 
Java :: add checkbox box rows in tabulator 
Java :: java import keyword 
Java :: map indexof java 
Java :: English print format number in java 
Java :: how to stop extending jpa repository to every class in java 
Java :: close current file android studio shortct 
Java :: What is Java? 
Java :: 2d matrix multiplication 
Java :: Implementation of HashMap Class in Java map 
Java :: java function that returns the index of the largest value in an array 
Java :: lelen suratlari ramkasidagi oltinlar 
Java :: charstreams cannot be resolved 
Java :: spring import properties file xml 
Java :: matrix program 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =