Vous êtes sur la page 1sur 3

2009 International Conference on Advances in Computing, Control, and Telecommunication Technologies

Indexed Array Algorithm for Sorting

Varinder Kumar Bansal Department of Computer Science and Engineering, Motilal Nehru National Institute Of Technology, Allahabad, 211004.

varinder008@gmail.com

Rupesh Srivastava Department of Computer Science and Engineering, Motilal Nehru National Institute Of Technology, Allahabad, 211004. srirupesh@gmail.com

Pooja Department of Computer Science and Engineering, Shaheed Bhagat Singh College of Engineering and Technology

poojabansal90@gmail.com

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

class. We prove the correctness of the

algorithm, detailed time complexity analysis and applications of the algorithm.(Abstract)

(

O n

2

)

I.

INTRODUCTION

There are mainly two types of comparison based sorting

algorithms (i)

sorting algorithms run slower than O(nlog n)algorithms, but

algorithms

are non recursive in nature, they require much less space on the RAM. Another importance of the algorithms is that they can be used in conjunction with algorithms and the final algorithm can be applied to match a suitable situation, for example divide a big array into smaller arrays. Another

sorting algorithms is in sorting small

application of

arrays. Since O(n log n) sorting algorithms are recursive in nature their use is not recommended for sorting small arrays as they perform poorly.

sorting algorithms, selection

sort and insertion sort are the best performing algorithms in general data distributions. For some data distributions insertion performs better than selection and vice – versa. Other algorithms are suited for very limited and particular data distributions.

In this paper we device a sorting algorithm, in which we use 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: section–2 gives the basic algorithm and an example to illustrate the working of the algorithm. Section–3 proves the correctness of the algorithm using the method of loop invariant, section–4 gives a detailed time complexity analysis of the algorithm and the running time comparison graphs, section–5 gives application of IAS, section–6 concludes and gives an overview of the future work and finally section–7 gives important references.

O

( n

2

)

and (ii) O(nlog n). In general

(

O n

2

)

(

O n

2 )

still their importance can’t be ignored. Since

(

O n

2

)

It is known that among

(

O n

2

)

II. THE ALGORITHM

The Indexed – Array Sorting algorithm (IAS) is described below:

Input : An unsorted array A[ ] of size n. Output : A sorted array A[ ] of size n.

IAS ( A[ ], n )

1.

j 0, B[j] 0

2.

for i 1 to n

3.

do if(B[j] <= i)

4.

j 0

5.

B[j] i

6.

for

k i to n

7.

do if (A[k] <= A[B[j]])

8.

j j + 1

9.

B[j] k

10.

else

11.

for k B[j+1] to n

12.

do if(A[k] <= A[B[j]])

13.

j j + 1

14.

B[j] k

15.

exchange A[i] A[B[j]]

16.

do if(B[j] > 1)

17.

j j – 1

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 7–9: Insert k into the array B at j+1 if A[k]<=A[B[j]]. This function is for saving indices of array A so that we don’t have to start from ith element for next iteration. Let’s call it INSERT(k,B[],j+1).

Lines 12–14: Insert k into the array B at j+1 if A[k]<=A[B[j]]. Let’s call it INSERT(k,B[],j+1).

Lines 15: Exchange A[i] and A[B[j]] as j is the indices of minimum element found out by INSERT procedure. Let’s call it EXCHANGE(A[],i,B[j]).

Lines 16–17: Decrement j as we have used that indices for Exchange and number at that indices does not remain the same. Let’s call it EXCHANGE(A[],i,B[j]).

978-0-7695-3915-7/09 $26.00 © 2009 IEEE DOI 10.1109/ACT.2009.18

34

remain the same. Let’s call it EXCHANGE(A[],i,B[j]). 978-0-7695-3915-7/09 $26.00 © 2009 IEEE DOI 10.1109/ACT.2009.18 34
remain the same. Let’s call it EXCHANGE(A[],i,B[j]). 978-0-7695-3915-7/09 $26.00 © 2009 IEEE DOI 10.1109/ACT.2009.18 34
remain the same. Let’s call it EXCHANGE(A[],i,B[j]). 978-0-7695-3915-7/09 $26.00 © 2009 IEEE DOI 10.1109/ACT.2009.18 34

Now the shorter form of the algorithm can be stated as follows:

IAS ( A[ ], n )

1. j 0, B[j] 0

2. for i 1 to n

3. do if(B[j] <= i)

4. j 0

5. B[j] i

6.

7. INSERT(k, B[],j+1)

8. else

9. for k B[j+1] to n

10. INSERT(k, B[],j+1)

11. EXCHANGE(A[], i , B[j]).

12. do if(B[j] > 1)

13. j j – 1

for

k i to n

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 at indices 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

3

1 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 saving the 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 array number of memory writes required also decreases

compared to other existing

(

O n

2

)

algorithm.

III. CORRECTNESS OF THE ALGORITHM

Theorem – 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, resultant array 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.

In this part we take an array B and initialize its first element with 0. Hence loop invariant holds before the first loop iteration.

Initialization:

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 element

in subarray A[i…

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.

n]

placed to its required position i.e. at

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 the reasoning of the maintenance part, we can infer that at the end of this iteration the A[1………length] 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 ALGORITHM

We 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 f (n) .

35

IAS ( A[ ], n )

1. j 0, B[j] 0

2. for i1 to n

3.

4. j 0

5. B[j] i

6.

7. do if (A[k] <= A[B[j]])

8. j j + 1

9. B[j] k

10.

11. for k B[j+1] to n

12.

13.

14.

15. exchange A[i] A[B[j]]

16.

17. j j – 1

do if(B[j] <= i)

for

k i to n

else

do if(A[k] <= A[B[j]]) j j + 1

B[j] k .

do if(B[j] > 1)

cost

c

c

c

1

2

3

times

1

n

1

← k . do if (B[j] > 1) cost c c c 1 2 3 times

← k . do if (B[j] > 1) cost c c c 1 2 3 times

c

4

c

5

n

1

c

6

n

7 1

c

c

8

1

We can aggregate the constants into singular constants, without the loss of generality, thus making the derivation more comprehendable. Hence we compute f (n) by multiplying the costs with corresponding times. So we have,

f

(

for

n

)

= an

2 + bn + c

some

constants a,b and

c .

Hence

we

see

that

f (n) varies as quadratic function of n . Thus we have

f (n) = Ω(n)

and

f

(

n

)

(

= O n

2

)

Thus we prove that the algorithm IAS ( ) belongs to the

(

O n

2

)

complexity sorting algorithm class.

We now give the running time comparison chart, where our algorithm is compared with selection, bubble and insertion sort. Here in following graph we have taken time units on Y-axis and number of data elements on X-axis.

time units on Y-axis and number of data elements on X-axis. It is clear from the

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.

APPLICATIONS

The 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 O(nlog n) sorting algorithms efficiently. Our algorithm can also be used efficiently in all the places where 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 in

multiobjective 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 WORK

In 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

algorithms. 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 propose a method which introduces a minimal overhead.

(

O n

2

)

sorting

VII. IMPORTANT REFERENCES

[1] Alfred V. Aho, John E. Hopcroft, and Jeffrey D. Ullman. The Design 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 Computer Algorithms. Woodland Hills, Cal. : Computer Science Press, 1978.

[4]Thomas H. Cormen, Charles E. Lieserson, Ronald L. Rivest and Clifford Stein. Introduction to Algorithms. MIT Press.

[5] Richard Neapolitan and Kumarss Naimpour. Foundation of Algorithms. Narosa, 2005.

36