In this example we are going to sort integer values of an array using extra storage merge sort.
Extra Storage Merge Sort in Java
Introduction
In this example we are going to sort integer values of an array using extra
storage merge sort.
In extra storage merge sorting algorithm the unsorted values divide into two equal parts
iteratively and create an array for store data value in extra storage. Then merge the two parts
, sort it and store into an array .Then again merge the next
part , sort it and store into an array. Do it iteratively until
the
values are not in sorted order. In this sorting the number of elements must be
even.
Code description:
In extra storage is
similar to merge sort .But in extra storage merge sort the sorted values
are stored in an other array.
Working of extra storage merge
sort algorithm:
Say we have an array unsorted A[0],A[1],A[2]................
A[n-1] and A[n] as input. Then the following steps are followed by storage merge
sort algorithm to sort the values of an array.
Step1:Spliting the values of array
Divide the values into two equal 1/2
A[0],A[1],A[2].........A[n/2-1]
& A[n/2]....... .................A[n-1], A[n]
Again divide two equal 1/2
A[0] a[1]A[2]..............A[(n/2-1)/2-1] & A[(n/2-1)/2]............A[n/2-1],
A[n/2].............A[(2n-1)/2-1] & a[(2n-1)/2].............A[n-1],A[n]
..........................................................................................................................
..........................................................................................................................
........................................................................................................................
A[0] & A[1] & A[2]&
A[3],..............................................................A[n-1]&
A[n]
Step2:Mergesets of two values, sort
the values and store into a different array
A[0],A[1] &
A[2],A[3]&..................................................................&A[n-1],A[n]
If
A[1]<A[0],A[]<A[3]........................................................................A[n-1]>A[n]
then
A[1]A[0],A[2]A[3],...............................................................................A[n]A[n-1]
Step3:Merge sets of four values, sort
the values and store into a different array
A[2] A[1] A[0]
A[3],...................................................................................A[n-1]
..................................................................................................................
..................................................................................................................
.................................................................................................................
Step3:Merge n values, sort
the values and store into a different array
A[2]A[6]......................................................................................................A[n-5]
Where n must be even number.
Steps of Merge Sort:
Say unsorted an array values are:
12,9,4,99,120,1,3,10
The code of the program :
public class ExtraStorageMergeSort{
|
Output of the example:
C:\array\sorting>javac ExtraStorageMergeSort.java C:\array\sorting>java ExtraStorageMergeSort RoseIndia Extra Strorage Space Merge Sort Values Before the sort: 12 9 4 99 120 1 3 10 Values after the sort: 1 3 4 9 10 12 99 120 PAUSE C:\array\sorting>_ |