<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Sliding Window Sort for Multidimension Array</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Krishanu Majumder</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Dipankar Das</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Dept of Computer Science &amp; Engineering, Jadavpur University</institution>
          ,
          <addr-line>Kolkata</addr-line>
          ,
          <country country="IN">India</country>
        </aff>
      </contrib-group>
      <fpage>81</fpage>
      <lpage>85</lpage>
      <abstract>
        <p>Sorting numbers in an array are one of the most basic problems that we learn to solve and have always proved useful. Though there are many algorithms for sorting in 1 dimensional array of numbers, there are not many algorithms which can be applied to 2 dimension or higher dimension arrays. In this paper, we will discuss a new algorithm for sorting 2-dimensional array and will see how it can be modified to sort n-dimensional array.</p>
      </abstract>
      <kwd-group>
        <kwd>1 Sorting</kwd>
        <kwd>Multidimensional array</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
    </sec>
    <sec id="sec-2">
      <title>2. Related Works</title>
      <p>
        Sorting is one of the very first problem that we learn. There are many sorting algorithms proposed
and some of the widely accepted ones include bubble sort, selection sort, insertion sort, quick sort [1],
merge sort [2] to name a few. These algorithms are fit for linear collection of data but mostly not for
complex data structures like tensors or multidimensional arrays. Among this, bubble sort, insertion sort
and selection sort have the overall time complexity of O(n2). Quick sort has an average case time
complexity as Ɵ(nlogn) but have a worst time complexity of O(n2). On the other hand, merge sort has
both average and worst time complexity of O(nlogn) but it uses extra space with worst case space
complexity as O(n). Many authors like Oyelami et. al. [
        <xref ref-type="bibr" rid="ref1 ref2">3,4</xref>
        ] have proposed modifications over the
existing algorithms by still they are confined to one-dimension arrays only.
      </p>
      <p>
        But very few works have been done in the field of multidimensional arrays. One of the interesting
works have been presented by Sandeep Sen et. al. [
        <xref ref-type="bibr" rid="ref3 ref4">5,6</xref>
        ]. They proposed a new algorithm called shear
sort which sorts a 2-D array by alternatingly sorting the rows and columns. The authors used it to sort
networks. Other works on 2-dimensional sorting was done in the field of VLSI and networks by
Thompson and Kung [
        <xref ref-type="bibr" rid="ref5">7</xref>
        ], Nissimi and Sahni [
        <xref ref-type="bibr" rid="ref6">8</xref>
        ], Hans-Warner et. al. [
        <xref ref-type="bibr" rid="ref9">11</xref>
        ] which mainly uses bitonic
sort [
        <xref ref-type="bibr" rid="ref7">9</xref>
        ] or odd even merge sort [
        <xref ref-type="bibr" rid="ref8">10</xref>
        ] using parallel computing. Douglas [
        <xref ref-type="bibr" rid="ref10">12</xref>
        ] tried to implement bubble
sort in 2-dimension array. Hanan Samet [
        <xref ref-type="bibr" rid="ref11 ref12">13,14</xref>
        ] discussed the applications of multidimensional sorting
in real world like in graphics, game theory, etc.
      </p>
    </sec>
    <sec id="sec-3">
      <title>3. Algorithm</title>
      <p>
        We take a window of 2x2 for a 2-dimensional matrix of size m x n. So, the window will have the
elements (i,j), (i,j+1), (i+1,j) and (i+1,j+1) for all 0&lt;=i&lt;m and 0&lt;=j&lt;n. We start from the top left corner.
In doing so, we have four numbers under the window. We sort the four numbers and then rearrange the
four numbers within the window such that the smallest number is on the top left of the window, next
smallest number on the top right, next on the bottom left and the largest number on the bottom right of
the window. Now we slide the window one place to the right along the row and will repeat the process
until the we reach the end of the row, i.e., top right cell of the window is the last cell along the row. We
repeat this for all the rows. This total process forms one pass. We again start form the top left corner of
the new matrix until we have done max(m,n) passes. There will be some corner cases like when the
window moves to the end of a row or column, i+1 (or j+1) will be out of index range. For simplification,
we use mod number_of_rows and mod number_of_columns to locate the elements under the window.
A depiction of the process is shown in Fig. 2 and Fig. 3.
4. Pseudo Code
function window_sort(array[][], r, c):
//array[][] is the given array
// r is the number of rows
// c is the number of columns
flag := 0
for x := 0 to max(r,c) do:
for i := 0 to r-1 do:
for j := 0 to c-1 do:
k := (i+1) % r
l := (j+1) % c
a := min(i,k)
b := max(i,k)
c := min(j,l)
d := max(j,l)
array[a][c] := window[0]
array[a][d] := window[1]
array[b][c] := window[2]
array[b][d] := window[
        <xref ref-type="bibr" rid="ref1">3</xref>
        ]
end for
end for
if flag = 0 do:
      </p>
      <p>break loop
else do:</p>
      <p>
        flag = 0
end if
end for
end function
window := [array[i][j], array[i][l], array[k][j], array[k][l]]
window := sort(window)
if array[a][c] ≠ window[0] or array[a][d] ≠ window[1] or array[b][c] ≠ window[2] or
array[b][d] ≠ window[
        <xref ref-type="bibr" rid="ref1">3</xref>
        ] and flag = 0 do:
      </p>
      <p>flag := 1
end if</p>
    </sec>
    <sec id="sec-4">
      <title>5. Time and Space Complexity Analysis</title>
      <p>When sorting the numbers in the window, we are every time sorting four number and place them at
their respective positions in the window. It will take O(1) time. At every pass, we are sliding the window
m*n times and this takes O(m*n). We are repeating the above steps for getting the array sorted and it
will take a maximum of max(m,n) passes since a misplaced element needs to be moved a maximum of
this distance to be placed in its right place. We may even stop the execution if there is no rearrangement
during a pass by using a flag variable. Thus, the total time complexity in the worst case is
O(m*n*max(m,n)). This can also be written as O((max(m,n))3) in the worst case. Since, we did not use
any additional space, space complexity is O(1).</p>
    </sec>
    <sec id="sec-5">
      <title>6. Extension to N-Dimension Arrays</title>
      <p>Let us extend this to 3-D array first with dimensions d1, d2 and d3. We take the window size as 2 x
2 x 2. The window will be in a shape of smaller cube where the 3-D array will be a bigger cube. The
total number of elements under the window to be sorted first is 8. For now we can take the sorting time
of the elements under the window be constant, i.e., O(1) as the number of elements under the window
to be sorted is constant every time. Now, we move the smaller window cube over the whole array cube
such that the (0,0,0) element of the smaller window cube coincides will all (i,j,k) of the bigger array for
all 0&lt;=i&lt;d1, 0&lt;=j&lt;d2, 0&lt;=k&lt;d3. Thus, the time complexity of each pass will be O(d1 x d2 x d3). We
have to do max(d1, d2, d3) passes as this will be the longest distance that a misplaced element have to
be moved to its correct place. So, the total time complexity will be O(d1 x d2 x d3 x max(d1, d2, d3)).
But as the dimension increases, the size of the window increases and hence the number of elements to
be sorted each time. If we have n dimensions, the window size will be 2n and hence, we need to sort 2n
elements each time.</p>
      <p>Let us generalize for n-D array, we can choose the window size as 2 x 2 x 2 x… upto n times. Here,
the total number of elements in the window will be 2n. Now, we can use the best existing algorithm for
sorting the elements in the window and getting it to their proper positions within the window which will
take O(n x logn) for n elements. In this case, this will take in the worst case, we have 2n elements, hence
O(2n x log2n)
or, O(2n x n x log2)
or, O(2n x n)</p>
      <p>Then we can follow the above explained procedure to first sort within the window and then slide the
window. If di, for i from 1 to n, are dimension sizes of the n dimensions, then the total worse case time
complexity of the given algorithm is</p>
      <p>O(2n x n) x O(max(d1,d2,…,dn)^(n+1))</p>
      <p>When this array is implemented for 1-D array, it will be similar to Bubble sort. But as it will go in
higher dimension, its complexity will be better compared to other general sorting algorithm.</p>
    </sec>
    <sec id="sec-6">
      <title>7. Conclusion</title>
      <p>We know that most of the 1D sorting algorithms including quick sort [1] has worst time complexity
O(n2). If we sort the 2-D array of size m x n by transforming it into a linear array, the number of
elements become m*n and thus the time complexity becomes O(m2 x n2). On the other hand, the above
proposed algorithm can do it more efficiently. Only merge sort [2] can do it differently with worst time
complexity O(m x n x log(m x n)) but it will use extra space with space complexity O(m x n).</p>
    </sec>
    <sec id="sec-7">
      <title>8. Acknowledgement</title>
      <p>I would also like to thank Department of Computer Science &amp; Engineering, Jadavpur University,
Kolkata for giving me the platform to learn, grow, explore and research. Last but not the least, I would
like to thank my parents and family for being the pillars of my life, without whom, nothing would have
been possible.</p>
    </sec>
    <sec id="sec-8">
      <title>9. References</title>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [3]
          <string-name>
            <surname>Oyelami</surname>
            <given-names>MO</given-names>
          </string-name>
          (
          <year>2008</year>
          ).
          <article-title>A Modified Diminishing Increment Sort for Overcoming the Search for Best Sequence of Increment for Shellsort”</article-title>
          .
          <source>J. Appl. Sci. Res</source>
          .,
          <volume>4</volume>
          (
          <issue>6</issue>
          ):
          <fpage>760</fpage>
          -
          <lpage>766</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [4]
          <string-name>
            <surname>Oyelami</surname>
            <given-names>MO</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Azeta</surname>
            <given-names>AA</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ayo</surname>
            <given-names>CK</given-names>
          </string-name>
          (
          <year>2007</year>
          ).
          <article-title>Improved Shellsort for the Worst-Case, the Best-Case and a Subset of the Average-Case Scenarios</article-title>
          .
          <source>J. Comput. Sci Appl</source>
          .
          <volume>14</volume>
          (
          <issue>2</issue>
          ):
          <fpage>73</fpage>
          -
          <lpage>84</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [5]
          <string-name>
            <surname>Sen</surname>
            ,
            <given-names>Sandeep</given-names>
          </string-name>
          &amp; Scherson, Isaac &amp; Shamir,
          <string-name>
            <surname>Adi.</surname>
          </string-name>
          (
          <year>1986</year>
          ).
          <article-title>Shear Sort: A True TwoDimensional Sorting Techniques for VLSI Networks</article-title>
          ..
          <volume>903</volume>
          -
          <fpage>908</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>I.D.</given-names>
            <surname>Scherson</surname>
          </string-name>
          and
          <article-title>Sandeep sen, “A characterization of a parallel row-column sorting technique for rectangular arrays</article-title>
          ”
          <source>ECE Technical Report No</source>
          .
          <fpage>85</fpage>
          -14, U.C.S.B.
          <year>August 1985</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>C.D.</given-names>
            <surname>Thompson &amp; H. T. Kung</surname>
          </string-name>
          ,
          <article-title>"Sorting on a Mesh-Connected Parallel Computer," Communications or the ACM</article-title>
          , vol
          <volume>20</volume>
          , no.
          <issue>4</issue>
          ,
          <string-name>
            <surname>April</surname>
          </string-name>
          <year>1977</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>D.</given-names>
            <surname>Nassimi</surname>
          </string-name>
          &amp; S. Sahni,
          <article-title>"Bitonic Sort on a Mesh-Connected Parallel Computer,"</article-title>
          <source>IEEE Transactions on Computers</source>
          , vol C-
          <volume>27</volume>
          , no\,
          <year>January 1979</year>
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>K.</given-names>
            <surname>Batcher</surname>
          </string-name>
          ,
          <article-title>"Sorting Networks and their applications,"</article-title>
          <source>in Proc. AFIPS Spring Joint Comput. Conf</source>
          , vol
          <volume>32</volume>
          ,
          <year>1968</year>
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>M.</given-names>
            <surname>Kumar</surname>
          </string-name>
          &amp;
          <string-name>
            <given-names>D.S.</given-names>
            <surname>Hirschberg</surname>
          </string-name>
          ,
          <article-title>"An efficient implementation of Batcher's odd-even merge algorithm and its application in parallel sorting schemes,"</article-title>
          <source>IEEE Transaction on Computers</source>
          , vol C-
          <volume>32</volume>
          ,
          <year>March</year>
          .198
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>Lang</given-names>
            <surname>Hans-Werner</surname>
          </string-name>
          et al.,
          <article-title>"Systolic sorting on a Mesh Connected Network,"</article-title>
          <source>IEEE Transactions on Computers</source>
          , vol C-
          <volume>34</volume>
          , no.
          <issue>7</issue>
          ,
          <issue>July1985</issue>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [12]
          <string-name>
            <surname>Ierardi</surname>
            ,
            <given-names>Douglas.</given-names>
          </string-name>
          (
          <year>1994</year>
          ).
          <article-title>2d-bubblesorting in average time O (√ N lg N )*</article-title>
          .
          <fpage>36</fpage>
          -
          <lpage>45</lpage>
          .
          <fpage>10</fpage>
          .1145/181014.181025.
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>Hanan</given-names>
            <surname>Samet</surname>
          </string-name>
          .
          <year>2016</year>
          .
          <article-title>Sorting in space: multidimensional data structures for computer graphics and vision applications</article-title>
          .
          <source>In SIGGRAPH ASIA 2016 Courses (SA '16)</source>
          .
          <article-title>Association for Computing Machinery</article-title>
          , New York, NY, USA, Article
          <volume>16</volume>
          ,
          <fpage>1</fpage>
          -
          <lpage>42</lpage>
          . DOI:https://doi.org/10.1145/2988458.2988503
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [14]
          <string-name>
            <surname>Samet</surname>
            ,
            <given-names>Hanan.</given-names>
          </string-name>
          (
          <year>2010</year>
          ).
          <article-title>Sorting in space: multidimensional, spatial, and metric data structures for computer graphics applications</article-title>
          .
          <volume>3</volume>
          . 10.1145/1900520.1900523.
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [15]
          <string-name>
            <given-names>D.E.</given-names>
            <surname>Knuth</surname>
          </string-name>
          ,
          <article-title>"The Art of Computer Programming,"</article-title>
          vol.
          <volume>3</volume>
          ,
          <string-name>
            <surname>Addison-Wesley</surname>
          </string-name>
          ,
          <year>1973</year>
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>