Upload
sultan-khan
View
214
Download
0
Embed Size (px)
Citation preview
8/3/2019 05376871-forpaper
1/3
Indexed Array Algorithm for Sorting
Varinder Kumar Bansal Rupesh Srivastava PoojaDepartment of Computer Department of Computer Department of Computer
Science and Engineering, Science and Engineering, Science and Engineering,
Motilal Nehru National Institute Motilal Nehru National Institute Shaheed Bhagat Singh College
Of Technology, Allahabad, 211004. Of Technology, Allahabad, 211004. of Engineering and [email protected] [email protected] [email protected]
Abstract In this paper we present a sorting algorithm, which
uses the methodology of indexing in insertion sort efficiently to
give a much better performance than the existing sorting
algorithms of the )( 2nO class. We prove the correctness of the
algorithm, detailed time complexity analysis and applications of
the algorithm.(Abstract)
I. INTRODUCTIONThere are mainly two types of comparison based sorting
algorithms (i) ( )2nO and (ii) ( )nnO log . In general ( )2nO sorting algorithms run slower than ( )nnO log algorithms, butstill their importance cant be ignored. Since ( )2nO algorithmsare non recursive in nature, they require much less space onthe RAM. Another importance of the algorithms is that theycan be used in conjunction with algorithms and the finalalgorithm can be applied to match a suitable situation, forexample divide a big array into smaller arrays. Another
application of ( )2nO sorting algorithms is in sorting smallarrays. Since ( )nnO log sorting algorithms are recursive innature their use is not recommended for sorting small arrays asthey perform poorly.
It is known that among )( 2nO sorting algorithms, selection
sort and insertion sort are the best performing algorithms ingeneral data distributions. For some data distributionsinsertion performs better than selection and vice versa. Otheralgorithms are suited for very limited and particular datadistributions.
In this paper we device a sorting algorithm, in which weuse to store indices of numbers smaller than the pivot element.Since the number of iterations required is lesser the overhead
is reduced thus decreasing running time.
The paper is organized as follows: section2 gives thebasic algorithm and an example to illustrate the working of thealgorithm. Section3 proves the correctness of the algorithmusing the method of loop invariant, section4 gives a detailedtime complexity analysis of the algorithm and the runningtime comparison graphs, section5 gives application of IAS,section6 concludes and gives an overview of the future workand finally section7 gives important references.
II. THE ALGORITHMThe Indexed Array Sorting algorithm (IAS) is described
below:
Input : An unsorted array A[ ] of size n.Output : A sorted array A[ ] of size n.
The procedures from lines 7 9 and 12 14 simply
represent the indexing procedure, while those from line 15 is
simply exchange procedure. To simplify the code we have
define the following:
Lines 79: Insert k into the array B at j+1 ifA[k]
8/3/2019 05376871-forpaper
2/3
Now the shorter form of the algorithm can be stated as
follows:
The working of the above algorithm can be understood by the
following example. Consider the following input array:
7 8 5 3 9 15 6 1 10 2
Array A and B (used to save indices of array A, which
will be used in sorting) during n iterations of INSERT
function after line - 7 is as follows:
A[] B[]
7 8 5 3 9 15 6 1 10 2 0
7 8 5 3 9 15 6 1 10 2 0 2
7 8 5 3 9 15 6 1 10 2 0 2 3
7 8 5 3 9 15 6 1 10 2 0 2 3 7
Now we show the array status after lines 14, for each
iteration of the outermost for loop. After each iteration the
element at indices stored at end of array B and element atindices i are exchanged.
A[] B[]
1 8 5 3 9 15 6 7 10 2 0 2 3 9
1 2 5 3 9 15 6 7 10 8 0 2 3
1 2 3 5 9 15 6 7 10 8 31 2 3 5 9 15 6 7 10 8 4 6
1 2 3 5 6 15 9 7 10 8 5 6 7
1 2 3 5 6 7 9 15 10 8 5 6 9
1 2 3 5 6 7 8 15 10 9 7 8 9
1 2 3 5 6 7 8 9 10 15 8
Completely sorted data after all iterations :-
1 2 3 5 6 7 8 9 10 15
The time required to sort is lesser than any other existing
algorithm because:
1. The number of iterations has been reduced by savingthe indices of smaller elements than the pivot element ,
thus reducing the overhead and the running time.
2. As the percentage of sorted data increases in the arraynumber of memory writes required also decreases
compared to other existing ( )2nO algorithm.
III. CORRECTNESS OF THE ALGORITHMTheorem The necessary and sufficient condition for an
array a[max] to be sorted is for any indices p and q, p q
A[p] A[q] where p, q [1 , n].
We now prove that after completion of algorithm, resultantarray satisfies the above condition.
Proof:- We would use loop invariant method to prove
correctness of IAS ( ). We would use its shorter form for
reference.
Loop invariant: Before and after each iteration the sub
array A[(q+1).(p 1)] is sorted.
Initialization:In this part we take an array B and initialize its firstelement with 0. Hence loop invariant holds before the first
loop iteration.
Maintenance:Here starting with B[0]=0 now in lines 6-9 we store the
index of element in B[j]which is lesser than element at the
index of A stored in B[j-1]. Now after storing indices in array
B we exchange A[i] A[B[j]], this makes smallest elementin subarray A[i..n] placed to its required position i.e. at
A[i]. For the next invariant to find the smallest element in
further array we use B[j-1]. Hence the loop invariant holds.
Hence the loop invariant is maintained for each iteration of
the for loop.
Termination:It remains to note that when the outer loop terminates, i=
length(A)+1 so A[0.i-1] is A[0.length] and thus from thereasoning of the maintenance part, we can infer that at the
end of this iteration the A[1length] is sorted. Hence
loop invariant holds at termination of the algorithm also.
Hence, we prove that the algorithm IAS ( ) correctly sorts
the input array.
IV. COMPLEXITY OF THE ALGORITHMWe compute the time complexity of IAS ( ) using the cost
time analysis. Since the number of constants involved would
be large, we would skip some of the details of the analysis but
it would not affect the flow of the analysis.
The complexity is given by the time function )(nf .
IAS ( A[ ], n )
1. j 0, B[j] 0
2. for i 1 to n
3. do if(B[j] 1)13. j j 1
35
8/3/2019 05376871-forpaper
3/3
We can aggregate the constants into singular constants,
without the loss of generality, thus making the derivation
more comprehendable. Hence we compute )(nf by
multiplying the costs with corresponding times. So we have,
cbnannf ++= 2)(
for some constants ba, and c . Hence we see that
)(nf varies as quadratic function of n . Thus we have
)()( nnf = and )()( 2nOnf =
Thus we prove that the algorithm IAS ( ) belongs to the
( )2
nO complexity sorting algorithm class.
We now give the running time comparison chart, where
our algorithm is compared with selection, bubble andinsertion sort. Here in following graph we have taken time
units on Y-axis and number of data elements on X-axis.
It is clear from the above graph that IAS runs faster than the
existing sorting algorithms of the same complexity class.
Hence we can conclude that IAS ( ) is the best performing
amongst selection, bubble, insertion and IAS. One important
thing to note about our algorithm is that its running time
decreases rapidly as the percentage of sorted data increases in
input data.
V. APPLICATIONSThe present algorithm can be efficiently used to sort small
arrays, typically of size lying from 10 50 elements. The
algorithm can be used in conjunction with the ( )nnO log sorting algorithms efficiently.
Our algorithm can also be used efficiently in all the placeswhere percentage of sorted data in input data elements is
high. This algorithm can be used to find percentage of sorted
data in input data set by checking the extent of indexing in
array B.
In the single-objective optimization there is only one
global optimum, but in multiobjective optimization there is a
set of solutions, called the Pareto-optimal(PO) set, which are
considered to be equally important; all of them constitute
global optimum solutions. An important goal inmultiobjective optimisation is to find a good set of non-
dominated solutions that is both well-distributed and well-
converged. Since we are storing indices of smaller elements
with respect to pivot element, so this stored data can be used
in finding a measure of the amount of domination and in non-dominated sorting to find set of dominating elements and
dominated elements.
VI. CONCLUSION AND FUTURE WORKIn this paper we presented our algorithm which give a better
running time than the existing sorting algorithms of the same
complexity class.
The important thing about this algorithm is that its best
case running time is linear and for generally distributed data
sets it is the best candidate amongst the ( )2nO sortingalgorithms. As a matter of fact, we have yet not found the
worst case of the algorithm.
The future work includes the study of the performance of
the algorithm in conjunction with merge sort when applied in
external sorting applications. Also we are currently working
on reducing the number of swaps. The challenge is to proposea method which introduces a minimal overhead.
VII. IMPORTANT REFERENCES[1] Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. TheDesign and Analysis of Computer Algorithms. Addison Wesley,1974.
[2] Donald E. Knuth. Sorting and Searching, volume 3 of The Art of
Computer Programming. Addison Wesley, 1981.
[3] Horowitz E. and S. Sahni. Fundamentals of ComputerAlgorithms. Woodland Hills, Cal. : Computer Science Press, 1978.
[4]Thomas H. Cormen, Charles E. Lieserson, Ronald L. Rivest andClifford Stein. Introduction to Algorithms. MIT Press.
[5] Richard Neapolitan and Kumarss Naimpour. Foundation ofAlgorithms. Narosa, 2005.
IAS ( A[ ], n ) cost times
1. j 0, B[j] 0 1c 1
2. for i1 to n 2c n
3. do if(B[j]