×
Language:

(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
2, b
3, c
, Abdullah Al Mahmood
4, c
E-mail:
1
palash_14@yahoo.com,
2
dolon_it@yahoo.com,
3
shabbir_cse03@yahoo.com,
4
sajibcseofkuet@gmail.com
a.

b.

c.

Abstract
— This paper presents a technique for sorting numericaldata in an efficient way. The numbers of comparisons i.e. therunning time of this technique is dependent on distribution ordiversity of the value of data items as like as other efficientalgorithms. When the total number of data is even, this methodgroups that data into a collection of pairs and thereforeestablishes the sorting constraints on each of the pairs. Thecontrol is traversed through the list of elements by changing theposition of each pair which is the major principle of thistechnique. On the other hand, when the total number of elementsis odd, this method sorts all elements except the last one in thesame was as mentioned earlier and the last element is sortedusing the general Insertion Sort. This algorithm is therefore ahybrid sorting method that sorts elementary numeric data in afaster and efficient manner.
Keywords- Sorting, Pair Wise Sorting, Sorting Techniques.
I.

I
NTRODUCTION
Sorting is a computational building block of fundamentalimportance and is one of the most widely studied algorithmicproblems [1, 2]. The importance of sorting has also lead to thedesign of efficient sorting algorithms for a variety of fieldslike: parallel architectures [3], database systems [4], computergraphics and geographic information systems [5, 6], parallelprogramming patterns [7, 8] and so on. Many algorithms relyon the availability of efficient sorting routines as a basis fortheir own efficiency, and many other algorithms can beconveniently phrased in terms of sorting [9]. It is thereforeimportant to provide efficient sorting routines on practicallyany programming platform.In this paper, we present a new sorting technique which isbased on pair wise comparison. It works separately in twodifferent situations: when total number of elements to besorted is odd and when total number of elements to be sortedis even. This sorting technique has no major efficiencies overother already existing techniques. But its symmetric structuremakes it simpler than other techniques.This paper is organized as follows. Section 2 describes theproposed technique in details, and the paper continues inSection 3 by calculating complexity of the proposed sortingtechnique. Section 4 compares the performance of the proposedtechnique with other already existing sorting techniques. Wefinish by drawing some broad and necessarily speculative andpersonal conclusions and future goal in Section 5.II.

P
ROPOSED
T
ECHNIQUE
-

P
AIR
W
ISE
S
ORTING
The proposed sorting technique works in two differentstrategies depending on whether the number of elements to besorted is odd or even. When the total number of data is even,this method groups that data into a collection of pairs andtherefore establishes the sorting constraints on each of thepairs. The control is traversed through the list of elements bychanging the position of each pair which is the major principleof this technique. On the other hand, when the total number of elements is odd, this method sorts all elements except the lastone in the same was as mentioned earlier and the last elementis sorted using the general Insertion Sort. This sectiondescribes the proposed sorting technique in details.
A.

Working Procedure:
Let an array contains
n
elements where
n
is even. To sortthese elements the proposed technique uses total phaseswhere each phase contains two sub phases. The operationsperformed by two sub phases are different while the functionsof all phases are identical.In the first sub phase the algorithm divides the
n
elementsfrom position 1 to
n
into a total of pairs. The control movesfrom first pair to the last pair and checks whether the firstelement of the pair is larger than the second one and if yes,then these elements are interchanged. In the second sub phase,(
n-2)
elements from position 2 to
(n-1)
are divided unto atotal of
-
1
pairs and the similar checking and swappingoccur as mentioned earlier.An additional check is maintained at the end of each phaseto detect whether any swapping has occurred or not. If nointerchange has been occurred, then the array is declared to besorted and there is no need to continue for further phases.Otherwise, the phases are continued in the similar fashion tillthe final phase. Introduction of this checking may increasecomplexity as it requires initialization, changing and testingduring the phases. But it is very effective for the cases wherelarge numbers of elements are to be sorted and elements areuniformly distributed.This procedure indicates that the number of required phasesis constant for a data set and it is half of the total number of

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

Figure 1. Example of sorting an array of 8 elements with the proposedPair Wise Sorting Technique
elements. This algorithm is therefore very suitable for sortinglarge number of elements.
B.

Formulating Algorithm
The proposed technique combines three functions:1.

PAIRWISE (A, N)2.

PROCESS (A, N)3.

INSERT (A, N)Where A is the array and N is the number of elements to besorted. These functions are illustrated below:
Algorithm:
PAIRWISE (A, N)1. [Initialize] Set oddEvenChk: = N mod 2[Detects whether the array contains even number of elements or odd number of elements]2. [Compare] if oddEvenChk = 0 thenCall PROCESS (A. N)Elsea. Call PROCESS (A, N-1) [Sortsthe first N-1 elements]b. Call INSERT (A, N) [Sorts thelast N
th
elements]3. Exit.
Algorithm:
PROCESS (A, N) //This function sorts array A with N elements where N is even.1. [Initialize control variable] Set PTR: = 12. [Start of external loop] Repeat Steps from 3 to 8While (PTR
N/2)3. [Initialize variable for first inner loop]Set START: =1, END: = N, FLAG: = 04. [Start of first inner loop]Repeat While (START < END)a. if A[START] < A [START+1] then:swap (A[START], A [START+1])andSet 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-16. [Start of second inner loop]Repeat While (START < END)a. if A[START] < A [START+1] then:swap (A[START], A [START+1])andSet 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 98. [Increment counter for the external loop]Set PTR: = PTR+1[End of external loop]9. Exit
Algorithm:
PROCESS (A, N)
A
is an array with
N
elements, where
N-1
elements aresorted in increasing order. This function finds the actualposition of the
N
th
element and inserts it into the array by thegeneral insertion sort technique. As the technique is wellknown, hence it is not mentioned here.
C.

Example
Figure 1 demonstrates an example where an array of eight(8) elements is sorted using the pair wise sorting technique.Here,Total number of elements, N = 8Total number of phases = = = 4Number of comparisons in all passes are same andthat is N-1 = 8-1 =7In pair wise sorting technique, a flag can be used to cut downthe passes, but no flag is used in this example.Total number of pairs in the first sub phase = = = 4Total number of pairs in the second sub phase = - 1 = - 1= 4-1 = 3

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

C
OMPLEXITY
A
NALYSIS OF
P
AIR
W
ISE
S
ORTING
The proposed technique has a special feature that a numberof unnecessary passes can be cu t down by using a flagvariable. In this technique, the array can be sorted at any passand not all time it is required to complete all of the passes.Flag variable is used for the purpose of reducing the totalnumber of passes and therefore the total number of comparisons.So the total number of comparisons varies in two differentcases:1.

Without a Flag Variable2.

With a Flag Variable
A.

Complexity Analysis: Without a Flag
In this case, the first check is the one that determines whetherthe number of elements in the array is even or odd anddepending on this, the next step is chosen. It requires acomparison at the very beginning.So, C
1
= 1

(1)Let, the number of elements in the array is
n

When
n
is even:
C
2
=
p * q
(2)Here, p = total number of passes =q = total number of comparisons ate each pass= + - 1= n-1Hence, from (2) we getC
2
= (3)
When
n
is odd:
For the last element, total comparisons will be,C
3
=== (4)A control variable
is used for this odd-even checking.Hence, from equation (1), (3) and (4) we get, total number of comparisons when no Flag variable is used is:C = C
1
+ C
2
+ C
3
= 1+ + , (5)
B.

Complexity Analysis: With a Flag
In this case also, the first check is the one that determineswhether the number of elements in the array is even or oddand depending on this, the next step is chosen. It requires acomparison at the very beginning.So, C
1
= 1

(6)Let, the number of elements in the array is
n

When
n
is even:
Number of comparisons in the first sub phase =Number of comparisons in the second sub phase = -1; whenn>1After each phase, there is a check to detect whether anyinterchange has been made or not. Hence, the total number of comparisons after each phase is:+ ( -1) + 1; when n>1Therefore, the total number of comparisons is =+n; where =In average case, total number of comparisons,C
2
==
When
n
is odd:
For the last element, total comparisons will be,C
3
=