# Pair Wise Sorting: A New Way of Sorting

Document Sample

```					                                                               (IJCSIS) International Journal of Computer Science and Information Security,
Vol. 8, No. 9, December 2010

Pair Wise Sorting: A New Way of Sorting
Md. Jahangir Alam 1, a, Muhammad Monsur Uddin2, b, Mohammad Shabbir Hasan3, c, Abdullah Al Mahmood4, c
E-mail: 1palash_14@yahoo.com, 2dolon_it@yahoo.com,3shabbir_cse03@yahoo.com, 4sajibcseofkuet@gmail.com
a. Dhaka International University, Dhaka, Bangladesh
c. Panacea Research Lab, Dhaka, Bangladesh

Abstract— This paper presents a technique for sorting numerical
data in an efficient way. The numbers of comparisons i.e. the                    II.    PROPOSED TECHNIQUE- PAIR WISE SORTING
running time of this technique is dependent on distribution or               The proposed sorting technique works in two different
diversity of the value of data items as like as other efficient
strategies depending on whether the number of elements to be
algorithms. When the total number of data is even, this method
groups that data into a collection of pairs and therefore
sorted is odd or even. When the total number of data is even,
establishes the sorting constraints on each of the pairs. The             this method groups that data into a collection of pairs and
control is traversed through the list of elements by changing the         therefore establishes the sorting constraints on each of the
position of each pair which is the major principle of this                pairs. The control is traversed through the list of elements by
technique. On the other hand, when the total number of elements           changing the position of each pair which is the major principle
is odd, this method sorts all elements except the last one in the         of this technique. On the other hand, when the total number of
same was as mentioned earlier and the last element is sorted              elements is odd, this method sorts all elements except the last
using the general Insertion Sort. This algorithm is therefore a           one in the same was as mentioned earlier and the last element
hybrid sorting method that sorts elementary numeric data in a
is sorted using the general Insertion Sort. This section
faster and efficient manner.
describes the proposed sorting technique in details.
Keywords- Sorting, Pair Wise Sorting, Sorting Techniques.              A. Working Procedure:
Let an array contains n elements where n is even. To sort
I.    INTRODUCTION
these elements the proposed technique uses total                  phases
Sorting is a computational building block of fundamental
importance and is one of the most widely studied algorithmic              where each phase contains two sub phases. The operations
problems [1, 2]. The importance of sorting has also lead to the           performed by two sub phases are different while the functions
design of efficient sorting algorithms for a variety of fields            of all phases are identical.
like: parallel architectures [3], database systems [4], computer          In the first sub phase the algorithm divides the n elements
graphics and geographic information systems [5, 6], parallel
programming patterns [7, 8] and so on. Many algorithms rely               from position 1 to n into a total of    pairs. The control moves
on the availability of efficient sorting routines as a basis for          from first pair to the last pair and checks whether the first
their own efficiency, and many other algorithms can be                    element of the pair is larger than the second one and if yes,
conveniently phrased in terms of sorting [9]. It is therefore             then these elements are interchanged. In the second sub phase,
important to provide efficient sorting routines on practically            (n-2) elements from position 2 to (n-1) are divided unto a
any programming platform.
In this paper, we present a new sorting technique which is             total of     -1 pairs and the similar checking and swapping
based on pair wise comparison. It works separately in two
occur as mentioned earlier.
different situations: when total number of elements to be
An additional check is maintained at the end of each phase
sorted is odd and when total number of elements to be sorted
to detect whether any swapping has occurred or not. If no
is even. This sorting technique has no major efficiencies over
interchange has been occurred, then the array is declared to be
other already existing techniques. But its symmetric structure
sorted and there is no need to continue for further phases.
makes it simpler than other techniques.
Otherwise, the phases are continued in the similar fashion till
This paper is organized as follows. Section 2 describes the
the final phase. Introduction of this checking may increase
proposed technique in details, and the paper continues in
Section 3 by calculating complexity of the proposed sorting               complexity as it requires initialization, changing and testing
technique. Section 4 compares the performance of the proposed             during the phases. But it is very effective for the cases where
technique with other already existing sorting techniques. We              large numbers of elements are to be sorted and elements are
finish by drawing some broad and necessarily speculative and              uniformly distributed.
personal conclusions and future goal in Section 5.                            This procedure indicates that the number of required phases
is constant for a data set and it is half of the total number of

ISSN 1947-5500
(IJCSIS) International Journal of Computer Science and Information Security,
Vol. 8, No. 9, December 2010
elements. This algorithm is therefore very suitable for sorting         Algorithm: PROCESS (A, N)
large number of elements.                                                   A is an array with N elements, where N-1 elements are
sorted in increasing order. This function finds the actual
B. Formulating Algorithm                                                position of the Nth element and inserts it into the array by the
The proposed technique combines three functions:                     general insertion sort technique. As the technique is well
known, hence it is not mentioned here.
1. PAIRWISE (A, N)
2. PROCESS (A, N)
3. INSERT (A, N)                                                C. Example
Figure 1 demonstrates an example where an array of eight
Where A is the array and N is the number of elements to be              (8) elements is sorted using the pair wise sorting technique.
sorted. These functions are illustrated below:                          Here,
Total number of elements, N = 8
Algorithm: PAIRWISE (A, N)
Total number of phases =         =    =4
1.        [Initialize] Set oddEvenChk: = N mod 2
[Detects whether the array contains even number of            Number of comparisons in all passes are same and
elements or odd number of elements]                           that is N-1 = 8-1 =7
2.        [Compare] if oddEvenChk = 0 then
Call PROCESS (A. N)                        In pair wise sorting technique, a flag can be used to cut down
Else                                            the passes, but no flag is used in this example.
a. Call PROCESS (A, N-1) [Sorts
the first N-1 elements]                                                 Total number of pairs in the first sub phase =           =    =4
b. Call INSERT (A, N) [Sorts the
last Nth elements]                                                      Total number of pairs in the second sub phase =               -1=      -1
3. Exit.
= 4-1 = 3
Algorithm: PROCESS (A, N)
//This function sorts array A with N elements where N is even.
1.       [Initialize control variable] Set PTR: = 1
2.       [Start of external loop] Repeat Steps from 3 to 8
While (PTR ≤ N/2)
3.       [Initialize variable for first inner loop]
Set START: =1, END: = N, FLAG: = 0
4.       [Start of first inner loop]
Repeat While (START < END)
a. if A[START] < A [START+1] then:
swap (A[START], A [START+1])
and
Set FLAG: = FLAG+1.
b. Set START: = START+2
[End of first inner loop]
5.       [Initialize variable for second inner loop]
Set START: =2, END: = N-1
6.       [Start of second inner loop]
Repeat While (START < END)
a. if A[START] < A [START+1] then:
swap (A[START], A [START+1])
and
Set FLAG: = FLAG+1.
b. Set START: = START+2
[End of second inner loop]
7.       [Detect whether A is sorted or not]
If FLAG = 0 then go to Step 9
8.       [Increment counter for the external loop]
Set PTR: = PTR+1
[End of external loop]
9.       Exit

Figure 1. Example of sorting an array of 8 elements with the proposed
Pair Wise Sorting Technique

ISSN 1947-5500
(IJCSIS) International Journal of Computer Science and Information Security,
Vol. 8, No. 9, December 2010
III.       COMPLEXITY ANALYSIS OF PAIR WISE SORTING               B. Complexity Analysis: With a Flag
The proposed technique has a special feature that a number               In this case also, the first check is the one that determines
of unnecessary passes can be cu t down by using a flag                   whether the number of elements in the array is even or odd
variable. In this technique, the array can be sorted at any pass         and depending on this, the next step is chosen. It requires a
and not all time it is required to complete all of the passes.           comparison at the very beginning.
Flag variable is used for the purpose of reducing the total
number of passes and therefore the total number of                       So, C1 = 1                                                              (6)
comparisons.
So the total number of comparisons varies in two different               Let, the number of elements in the array is n
cases:
1. Without a Flag Variable                                      When n is even:
2. With a Flag Variable                                         Number of comparisons in the first sub phase =
A. Complexity Analysis: Without a Flag
In this case, the first check is the one that determines whether         Number of comparisons in the second sub phase =              -1; when
the number of elements in the array is even or odd and
n>1
depending on this, the next step is chosen. It requires a
comparison at the very beginning.
After each phase, there is a check to detect whether any
interchange has been made or not. Hence, the total number of
So, C1 = 1                                                   (1)         comparisons after each phase is:
Let, the number of elements in the array is n
+(    -1) + 1; when n>1
When n is even:

C2 = p * q                                                   (2)         Therefore,   the   total    number of comparisons                  is     =
+ n; where =
Here, p = total number of passes =
q = total number of comparisons ate each pass
=     +   -1
= n-1

Hence, from (2) we get
C2 =                                                         (3)

When n is odd:

For the last element, total comparisons will be,
C3 =                                                                     In average case, total number of comparisons,

=                                                                     C2 =

=                                                         (4)
=
A control variable k is used for this odd-even checking.
Hence, from equation (1), (3) and (4) we get, total number of            When n is odd:
comparisons when no Flag variable is used is:
C = C1 + C2 + C3                                                         For the last element, total comparisons will be,
= 1+                        +   ,                          (5)         C3 =

ISSN 1947-5500
(IJCSIS) International Journal of Computer Science and Information Security,
Vol. 8, No. 9, December 2010
the proposed pair wise sorting technique is same as other
=                                                                                    sorting techniques for around 15000 data but it takes higher
time with the increase in the number of elements to be sorted.
=                                                                      (8)
TABLE II.     TIME REQUIRED BY EACH OF THE CANDIDATE METHODS FOR
SORTING LARGE NUMBER OF RANDOM DATA

A control variable k is used for this odd-even checking.                             Number of                    Time in Milliseconds (ms)
Hence, from equation (6), (7) and (8) we get, total number of                           Elements       Pair Wise       Bubble         Insertion      Quick Sort
comparisons when no Flag variable is used is:                                                        Sort (Without      Sort            Sort
Flag)
5000          989.02         934.06          934.04          879.12
C = C1 + C2 + C3                                                                          10000         2307.67        2142.86         2087.91         1868.13
15000         3846.13        3351.65         3351.65         2857.14
= 1+                        +      ,                                    (9)             20000         5669.34        4780.22         4270.33         3846.15
25000         7637.36        6208.79         6208.79         4835.17

IV.      COMPARISON WITH OTHER SORTING TECHNIQUES
The proposed technique has no major efficiencies over
other sorting techniques rather it’s running time is a little bit
higher than other techniques. It is just a new method that sorts
numeric data. Its symmetric structure makes it simpler than
other sorting techniques. Table 1 shows a comparison of
average case complexity for the proposed technique and other

TABLE I.         COMPARISON TABLE OF DIFFERENT SORTING TECHNIQUES IN
AVERAGE CASE COMPLEXITY

Sorting Technique                     Average Case Complexity
Bubble Sort
=

Insertion Sort
=

Selection Sort
=

Quick Sort

Merge Sort
Figure 2. Performance graph
Heap Sort

Pair Wise Sort (Proposed                      Without a Flag:
Technique)                                                                                    V.    CONCLUSION AND FUTURE WORK
1+                           +      =                         The proposed pair wise sorting technique is a simple one
With a Flag:                           based on odd-even strategy. We strongly believe that this
strategy can be further used to develop techniques which will
1+                           +      =                      be more efficient. However, the limitation of the proposed
technique is, it requires different approach for the last member
Here,       is the number of elements to be sorted         of the array when the array contains odd number of elements.
Most of the time, it is not required to complete all of the
and                                                phases to sort elements. Here a flag is used to cut down the
total number of phases. A flag is very useful for the cases when
the elements of the array exist close to their exact positions.
We compared the performance of the proposed pair wise                               But the use of this flag causes the average number of
sorting technique with other sorting techniques namely Bubble                           comparisons to be higher than other algorithms. It would be
Sort, Insertion Sort and Quick Sort for sorting large number of                         best if it is possible to introduce a technique which will be able
random numeric data. The comparison was based on time                                   to sort elements efficiently without using a flag. This point will
required by each method for sorting these data. Table 2 and                             get our future focus.
Figure 2 show the result. The results shows that performance of                            .

ISSN 1947-5500
(IJCSIS) International Journal of Computer Science and Information Security,
Vol. 8, No. 9, December 2010
REFERENCES

[1]   D. E. Knuth, The Art of Computer Programming, Volume 3: Sorting and             Mohammad Shabbir Hasan received his
Searching, Second ed. Boston, MA: Addison-Wesley, 1998.                         B.Sc. (Engg.) in Computer Science and
[2]   T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction         Engineering from Khulna University of
to Algorithms, Second ed. MIT Press, Sep. 2001.                                 Engineering and Technology (KUET),
[3]   S. G. Akl, Parallel Sorting Algorithms. Orlando: Academic Press, Inc.,          Bangladesh in 2008. His research interest
1990.                                                                           includes different areas of Software
Engineering       like     Requirement
[4]   G. Graefe, “Implementing sorting in database systems,” ACM Comput.
Engineering, Software Metric, Software
Surv., vol. 38, no. 3, pp. 1–37, 2006.
Security and Software Maintenance. He
[5]   C. Lauterbach, M. Garland, S. Sengupta, D. Luebke, and D. Manocha,              has coauthored numerous research
“Fast BVH construction on GPUs,” in Proc. Eurographics ’09, Mar.                papers published in International
2009.                                                                           Journals and Conference Proceedings.
[6]   V. Havran, “Heuristic ray shooting algorithms,” Ph.D. Thesis,                   Currently he is working as a researcher
Department of Computer Science and Engineering, Faculty of Electrical           of Panacea Research Lab, Dhaka,
Engineering, Czech Technical University in Prague, Nov. 2000.                   Bangladesh. He is also a lecturer of
[7]   J. Dean and S. Ghemawat, “MapReduce: Simplified data processing on              Department of Computer Science and
large clusters,” Commun. ACM, vol. 51, no. 1, pp. 107–113, 2008.                Engineering in Institute of Science,
[8]   B. He, W. Fang, N. K. Govindaraju, Q. Luo, and T. Wang, “Mars: A                Trade    and     Technology,     Dhaka,
MapReduce framework on graphics processors,” in Proc. 17th Int’l                Bangladesh.
Conference on Parallel Architectures and Compilation Techniques,
2008, pp. 260–269.
[9]   Nadathur Satish, Mark Harris, and Michael Garland, “Designing
efficient sorting algorithms for manycore GPUs”, in Proc. 23rd IEEE              Abdullah Al Mahmood received his
International Parallel and Distributed Processing Symposium, May                 B.Sc. (Engg.) in Computer Science and
2009, pp. 1-10.                                                                  Engineering from Khulna University
of Engineering and Technology
AUTHORS’ PROFILE                                                 research interest includes Robotics,
Artificial    Intelligence,     Internet
Security and various areas of Software
Engineering      like      Requirement
Gathering, Requirement Prioritization
Md. Jahangir Alam received his B.Sc.
and Software Security. He has
(Engg.) and M.Sc. in Computer
coauthored a good number of research
Science and Engineering from Dhaka
papers published in International
International    University     (DIU),
Journals and Conference Proceedings.
Currently he is working as a researcher
include Software Engineering and Data
of Panacea Research Lab, Dhaka,
Structure. Currently he is working as a
Bangladesh. He is also a lecturer at
Senior Lecturer of Department of
Computer Science and Engineering,
Dhaka International University, Dhaka,
Project Coordinator of Technocrats
BD.

his B.Sc. (Engg.) in Computer Science
and Engineering from Institute of
research interests include Software
Engineering and Data Structure.