Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Sort vs Selection Sort

I have written these 2 sorting algorithms and it appears that selection sort is faster than merge sort, surely this can't be right? My test data is 10 random arrays of size 5000 to 50000 where the largest possible numbers in the array is 100.

Here is my selection sort implementation:

int i, j, iMin;
int n = c.length;

startTime = System.currentTimeMillis();
for (i = 0; i < n - 1; i++) {
    iMin = i;
    for (j = i + 1; j < n; j++)
        if (c[j] < c[iMin]) {
            iMin = j;
            if (sorting) {
                theDelay();
            }
        }

    if (iMin != i) {
        swap(c, iMin, i);
        if (sorting) {
            theDelay();
        }
    }
}
endTime = System.currentTimeMillis();
overallTime = endTime - startTime;
// System.out.println(overallTime);

The theDelay() method is simply to delay the thread that the sorting algorithm runs within so that a visual graph can draw to the JPanel to show the sort in action, in this test case it is being ignored so wont affect my sort time.

Here is my merge sort implementation:

public void mergeSort(int[] d) throws InterruptedException {
    startTime = System.currentTimeMillis();
    MergeSort(d, 0, d.length - 1);
    endTime = System.currentTimeMillis();
    overallTime = endTime - startTime;
    //System.out.println("Merge" +overallTime);
}

private void MergeSort(int[] array, int low, int high) throws InterruptedException {
    int[] temp = new int[array.length];
    if (low < high) {
        int middle = low + (high - low) / 2;
        MergeSort(array, low, middle);
        MergeSort(array, middle + 1, high);
        ReMerge(array, temp, low, middle, high);
    }
}

private void ReMerge(int[] array2, int[] temp, int low, int middle, int high) throws InterruptedException {
    for (int i = low; i <= high; i++) {
        temp[i] = array2[i];
    }
    int i = low;
    int j = middle + 1;
    int k = low;

    while (i <= middle && j <= high) {
        if (temp[i] <= temp[j]) {
            array2[k] = temp[i];
            i++;
            if (sorting) {
                theDelay();
            }
        } else {
            array2[k] = temp[j];
            j++;
            if (sorting) {
                theDelay();
            }
        }
        k++;
    }
    while (i <= middle) {
        array2[k] = temp[i];
        k++;
        i++;
        if (sorting) {
            theDelay();
        }
    }
}

is there something in my implementation affecting the time it should take for a merge sort to complete???

like image 989
MichaelStoddart Avatar asked Feb 04 '15 14:02

MichaelStoddart


People also ask

Why is merge sort better than selection sort?

Merge Sort is considered to be one of the fastest sorting algorithms, it is a bit more complex than Selection and Bubble Sort but its more efficient. The idea of Merge Sort is to divide the data-set into smaller data-sets, sort those smaller data-sets and then join them (merge them) together.

Why merge sort is faster?

Merge sort is more efficient and works faster than quick sort in case of larger array size or datasets. Quick sort is more efficient and works faster than merge sort in case of smaller array size or datasets. Sorting method : The quick sort is internal sorting method where the data is sorted in main memory.

What is the difference between selection sort and quick sort?

So comparing Selection sort with Quick sort for n=1000, selection sort requires O(n2) or about 1,000,000 operations where Quick sort requires only about 10,000! (log2 of 1000 is about 10) Or 100 times faster! For n=1,000,000 the savings are even better, Quick sort is 500,000 times faster than selection sort!

Which is better merge or insertion sort?

Insertion Sort is preferred for fewer elements. It becomes fast when data is already sorted or nearly sorted because it skips the sorted values. Efficiency: Considering average time complexity of both algorithm we can say that Merge Sort is efficient in terms of time and Insertion Sort is efficient in terms of space.


1 Answers

You have:

private void MergeSort(int[] array, int low, int high) throws InterruptedException
{
    int[] temp = new int[array.length];

Of course allocating an array of length 5000 to 50000 at each step of the recursion will make the algorithm much slower. Try to reuse the same temp array.

like image 90
IVlad Avatar answered Sep 30 '22 20:09

IVlad