Linear Array

download Linear Array

of 22

Transcript of Linear Array

  • 8/8/2019 Linear Array

    1/22

    LINEAR ARRAY

    JYOTIKA JAIN

    MTECH 2009 IS 13ABV-IIITM Gwalior

    Gwalior-474 010, MP, India

    September 12, 2010

    1 / 2 2

  • 8/8/2019 Linear Array

    2/22

    OUTLINE

    Introduction

    Odd-Even Transposition Sort

    Merge-Splitting Sort

    Merge Sort on a Pipeline

    Enumeration Sort

    2 / 2 2

  • 8/8/2019 Linear Array

    3/22

    INTRODUCTION

    Parallel sorting algo for SIMD machines in which processorsare interconnected to form a linear array

    Pi linked by a communication path to processors Pi1 andPi+1

    No other link available

    3 / 2 2

  • 8/8/2019 Linear Array

    4/22

    ODD-EVEN TRANSPOSITION SORT

    no of processors = no of elements in the input sequence Algorithm

    for k=1 to [n/2] do

    1. for i=1,3,...,2[n/2]-1 do in parallelif yi>yi+1 then yiyi+1 end ifend for

    2. for i=2,4,...,2[(n-1)/2] do in parallelif yi>yi+1 then yiyi+1 end ifend for

    4 / 2 2

  • 8/8/2019 Linear Array

    5/22

    EXAMPLE

    Figure: S=7,6,5,4,3,2,1

    5 / 2 2

  • 8/8/2019 Linear Array

    6/22

    ANALYSIS

    Running time of algo t(n) = O(n)

    Cost c(n) = t(n) p(n) = o(n) n = O(n2)

    6 / 2 2

  • 8/8/2019 Linear Array

    7/22

    MERGE SPLITTING SORT

    Algorithm:

    Preprocessing stepfor i=1,2,..,p do in parallelprocessor Pi sorts Si using a sequential algoend forend of preprocessing

    for k=1 to [p/2] do1. for i=1,3,...,2[p/2]-1 do in parallel

    1.1 merge Si and Si+1 into a sorted subsequence Ai1.2 Si first(n/p) elements ofAi1.3 Si+1second(n/p) elements ofAi

    end for2. for i=2,4,...,2[?(p-1)/2] do in parallel

    2.1 merge Si and Si+1 into a sorted subsequence Ai2.2 Si first(n/p) elements ofAi2.3 Si+1second(n/p) elements ofAi

    end for

    7 / 2 2

  • 8/8/2019 Linear Array

    8/22

    EXAMPLE

    Figure: Sort (12,9,10,11,7,4,3,6,2,1,8,5)

    8 / 2 2

  • 8/8/2019 Linear Array

    9/22

    ANALYSIS

    By using heap sort in preprocessing step

    The total running time is

    t(n) = o[(n/p)log(n/p)] + O(n) = O((nlogn)/p+ O(n)) Cost of the algorithm

    c(n) = t(n) p= O(nlogn) + o(np)optimal for p logn

    9 / 2 2

  • 8/8/2019 Linear Array

    10/22

    MERGING SORT ON A PIPELINE

    DO steps 1,2,3 in parallel

    1. P1 performs the following steps

    1.1 read x1 from q1

    1.2 j01.3 for i=2 to n do place xi1 on q2+j read xi from q1 jj+1 mod 2

    end for1.4 place xn on q3

    10/22

  • 8/8/2019 Linear Array

    11/22

    STEP 2

    for i=2 to r do in parallel

    1. j02. k13. while k n do

    if q2i2 is 2i2 elements long and q2(i1)+1 contains one

    element

    then3.1 for m=1 to 2i1 do

    Pi compares the first element in q2(i1) to the first element inq2(i1)+1removes the larger of the two and places it on q2i+j

    end for3.2 jj+1 mod 23.3 kk + 2i1

    end ifend while

    end for11/22

  • 8/8/2019 Linear Array

    12/22

    STEP 3

    if q2r is 2r1 elements long and q2r+1 contains one element then

    for m=1 to 2r do

    Pr+1 compares the first element in q2r to the first element inq2r+1,removes the larger of the two and places it on q2(r+1)

    end for

    end if

    12/22

  • 8/8/2019 Linear Array

    13/22

    EXAMPLE

    Figure: Sort (1,5,3,2,8,7,4,6)

    13/22

  • 8/8/2019 Linear Array

    14/22

    Figure: Sort (1,5,3,2,8,7,4,6)

    14/22

  • 8/8/2019 Linear Array

    15/22

    ANALYSIS

    Running time O(n)

    Cost is given by:c(n) = t(n) p(n) = O(n) (logn + 1) = O(nlogn)

    15/22

    O SO

  • 8/8/2019 Linear Array

    16/22

    ENUMERATION SORT

    ALGORITHM

    1. for i=1 to n do in parallelPi sets its register C to 1end for

    2. for k=1 to 2n do2.1 if kn then h1 else hkn end if2.2 for i=hto n do in parallel

    if its registers X and Y are non empty and X

  • 8/8/2019 Linear Array

    17/22

    CONTD..

    2.3 for i=h to n-1 do in parallel

    if its register Y is nonempty then processor Pi shifts the integer init to Pi+1 which stores it in its own register Y end if

    2.4 if kn then processors P1 and Pk read the next integer xkfrom the input queue and store it in their registers Y andX,respectively end if

    2.5 if k>n then processor Pkn stores in register Z ofPj thecontent of its register X, where j is the value stored in its register

    C end if

    end for

    17/22

    CONTD

  • 8/8/2019 Linear Array

    18/22

    CONTD..

    Step 3 for k=1 to n do

    1. processor Pn places the contents of its register Z on theoutput queue

    2. for i=k to n-1 do in parallelprocessor Pi shifts the contents of its register Z to the registerZ ofPi+1end for

    end for

    18/22

    EXAMPLE

  • 8/8/2019 Linear Array

    19/22

    EXAMPLE

    Figure: Sort (8,9,7)

    19/22

  • 8/8/2019 Linear Array

    20/22

    Figure: Sort (8,9,7)

    20/22

    ANALYSIS

  • 8/8/2019 Linear Array

    21/22

    ANALYSIS

    Running time O(n)

    Cost is given by:c(n) = t(n) p(n) = O(n) n = O(n2)

    cannot handle sequences with repeated numbers

    21/22

  • 8/8/2019 Linear Array

    22/22

    THANK YOU

    22/22