Tutorial Addendum on Allocation - Absorb Array
| |
Basic Idea
The basal abstraction of Absorb Array is to:
1. Bisect the data elements into two sections with according amount of elements.
2. Array the two sections separately.
3. Absorb the two sorted sections into a individual sorted collection.
Obviously, this is a recursive idea, area a problem is disconnected into smaller
problems. And the analysis will be again to create the abate problems
even smaller, until they are abate abundant so that the solutions are obvious.
Java Implementation
Here is my Java accomplishing of Absorb Array algorithm:
/**
* HyArrays.java
* This chic contains allocation methods agnate to java.util.Arrays.
* All allocation methods should accept a signiture of
* %Sort(Object a, int fromIndex, int toIndex)
* area "fromIndex" is inclusive, and "toIndex" is exclusive.
* Absorb (c) 1999 by Dr. Yang
*/
public chic HyArrays {
accessible changeless abandoned mergeSort(Object a, int fromIndex,
int toIndex) {
Object b = new Object;
for (int i=fromIndex; i<toIndex; i++) {
b = a;
}
mergeSortInternal(b, a, fromIndex, toIndex);
}
clandestine changeless abandoned mergeSortInternal(Object a, Object b,
int fromIndex, int toIndex) {
if (toIndex-fromIndex<=1) {
return;
} abroad if (toIndex-fromIndex==2) {
if (((Comparable)a).compareTo(a)>0) {
b = a;
b = a;
}
} abroad {
int iMiddle = (toIndex-fromIndex)/2 + fromIndex;
mergeSortInternal(b,a,fromIndex,iMiddle);
mergeSortInternal(b,a,iMiddle,toIndex);
int iLeft = fromIndex;
int iRight = iMiddle;
int i = fromIndex;
while (iLeft<iMiddle && iRight<toIndex) {
if (((Comparable)a).compareTo(a)>0) {
b = a;
iRight++;
} abroad {
b = a;
iLeft++;
}
i++;
}
while (iLeft<iMiddle) {
b = a;
iLeft++;
i++;
}
while (iRight<toIndex) {
b = a;
iRight++;
i++;
}
}
}
}
- Method mergeSort() is a adhesive of the absolute allocation method: mergeSortInternal().
- Array b is created to advice amalgamation the two sorted sections aback into a individual sorted
collection.
Performance
Now let s see how it performs. I approved this accomplishing with my SortTest.java
under JDK 1.3.1.
Here are the results:
Array size: 10000
Average allocation time: 98 milliseconds
Number of tests: 1000
Performance: 9.8 O(N) nonaseconds
Performance: 0.7375234893767539 O(N*Log2(N)) nonaseconds
Performance: 9.8E-4 O(N*N) nonaseconds
Array size: 20000
Average allocation time: 222 milliseconds
Number of tests: 1000
Performance: 11.1 O(N) nonaseconds
Performance: 0.7768913388743642 O(N*Log2(N)) nonaseconds
Performance: 5.55E-4 O(N*N) nonaseconds
Array size: 30000
Average allocation time: 336 milliseconds
Number of tests: 1000
Performance: 11.2 O(N) nonaseconds
Performance: 0.753058887534575 O(N*Log2(N)) nonaseconds
Performance: 3.733333333333333E-4 O(N*N) nonaseconds
The achievement of this accomplishing is at the adjustment of O(N*Log2(N)). But it
is about 5 times slower than java.util.Arrays.sort().
|
toindex, fromindex, sorting, object, imiddle, ileft, nonasecondsperformance, iright, merge, mergesortinternal, sections, n*log2, 1000performance, tests, millisecondsnumber, smaller, sorted, implementation, , merge sort, fromindex int, tests 1000performance, sorting time, int toindex, int fromindex, n*n nonasecondsarray size, java util arrays, sorting merge sort, |
Also see ...
ImprovementsOne way to advance the achievement is to divid the data elements into 3 sections.Sort them separately, then absorb them. Amalgamation 3 sorted sections is added efficient
Basic IdeaThe basal abstraction of Quicksort is to:1. Baddest an aspect as a axis element.2. Data elements are aggregate into t
PerformanceNow let s see how it performs. I approved this accomplishing with my SortTest.javaunder JDK 1.3.1.Here are the results: Array size: 10000