Professional Documents
Culture Documents
66-70 66
ISSN 2230-7621©MIT Publications
ABSTRACT
A hybrid algorithm that combines two or more other algorithms that solve the same problem. Hybrid algorithm simply done
by combine desired characteristic of each algorithm, so that the all-inclusive algorithm is better than the single components.
Hybrid algorithm does not refer to simply combining multiple algorithms to solve a different problem but only to combining
algorithms that solve the same problem. A goal of this paper to solve hybrid approach by using Tim sort algorithm and
comparison with quick sort algorithm.
Keywords: Hybrid algorithm, Tim sort algorithm, quick sort algorithm, insertion sort algorithm.
I. Introduction (ii) Tim Sort walks the list, finding “runs” of elements already
Tim sort algorithm is a highly optimized hybrid sorting algorithm, in ascending order or in strictly descending order.
which derived from merge sort and insertion sort, which (iii) If the run is in strictly descending order, Tim Sort reverses
combines in the merging logic. Timsort algorithm is an effective it in place (the run must be strictly descending, otherwise
method expressed as a finite list of well-defined instructions this operation would not maintain Tim Sort’s stability).
for calculating a function [1]. Starting from an initial state and Then, if the run is less than a chosen “minrun” size.
initial input the instructions describe a computation that when
executed proceeds through a finite number of well-defined (iv) Tim Sort uses Insertion Sort to bulk it up to minrun
successive states, eventually producing output and terminating elements. The value of minrun is calculated such that,
at a final ending state. Tim sort is a hybrid sorting algorithm that where n is the size of an array.
designed to solve many kinds of real-world data. This algorithm (v) Tim Sort always tries to merge runs as soon as it is optimal
finds subsets of the data that are already organized, and to sort to do so, also attempts to keep the merges balanced. it
the data more reliable. This is done by merging or recognizes
records each run created on a stack.
subset, until conclusive criteria are satisfied.
B. Data Flow Diagram of Timsort Algorithm
II. How Tim sort algorithm works
Timsort operates by finding runs, subsets of at least two elements,
A. Timsort Algorithm in the data. Runs are either non-descending (each element is
Tim sort is an adaptive, stable, in-place sorting algorithm [1]. equal to or greater than its predecessor) or strictly descending
The steps are following: (each element is lower than its predecessor) Fig. 1. Tim sort has
(i) It takes existing structure in a list, allowing it to perform been Python’s standard sorting algorithm. After obtaining such
n-1 comparisons on a list which is already in order or in a run in the given array, Timsort processes it, and then searches
strictly-descending (reverse) order. for the next run.
MIT International Journal of Computer Science and Information Technology, Vol. 5, No. 2, August 2015, pp. 66-70 67
ISSN 2230-7621©MIT Publications
B. Minrun
The minimum run size (minrun) depends on the size of the array.
minrun is chosen from the range 32 to 64 inclusive, such that
the size of the array, divided by min run, is equal to, or slightly
smaller than, a power of two. This algorithm works for all arrays,
including those smaller than 64.
D. Merge Memory
Timsort does not merge non-consecutive runs, because doing this
would cause the element common to all three runs to become
out of order with respect to the middle run [2]. Thus, merging
is always done on consecutive runs Fig. 3. If X, Y, Z represents
the lengths of the three uppermost runs in the stack and two
rules are satisfied:
1. X > Y + Z
2. Y > Z
If the first of the two rules is not satisfied by the current run
status, that is, if X < Y + Z, then, Y is merged with the smaller
of X and Z. The merging continues until both rules are satisfied.
A. Run
A natural run is a sub-array that is already ordered. Natural runs
in real-world data may be of varied lengths [4]. Timsort chooses
a sorting technique depending on the length of run Fig. 2. For Fig. 3: Rules for merging the memory
example, if the run length is smaller than a certain value, insertion
sort is used. Thus Timsort is an adaptive sort.
E. Merging Prodecure
Merging adjacent runs is done with the help of temporary
memory. The temporary memory is of the size of the lesser of
the two runs. The algorithm copies the smaller of the two runs
into this temporary memory and then uses the original memory
[3]. A simple merge algorithm runs left to right or right to left
depending on which run is smaller, on the temporary memory
and original memory of the larger run. The final sorted run is
stored in the original memory of the two initial runs. Timsort
Fig. 2: Calculating Runs searches for appropriate positions.
MIT International Journal of Computer Science and Information Technology, Vol. 5, No. 2, August 2015, pp. 66-70 68
ISSN 2230-7621©MIT Publications
V. Experimental Results
For example, (Fig. 4 & Fig. 5) if two runs A and B are to be merged,
with A as the smaller run. In this case a binary search examines A to
find the first position larger than the first element of B (a’). When
a’ is found, the algorithm can ignore elements before that position
while inserting B. Similarly, the algorithm also looks for the smallest
element in B (b’) greater than the largest element in A (a”). The
elements after b’ can also be ignored for the merging.