0% found this document useful (0 votes)
9 views13 pages

Merge Algo

Uploaded by

samarth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Merge Algo

Uploaded by

samarth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Merge Sort

•Merge Sort works on the idea of divide and


conquer.

•Divide: Repeatedly divide the list of elements in


half size until the size becomes 1.

•Size 1 array is always considered as sorted array


as there is only one element in the array.

•Conquer and combine: Solve the sub problems


and combine them.
Example
14 7 3 12 9 11 6 2
Divide
step
14 7 3 12 9 11 6 2

14 7 3 12 9 11 6 2 Divide
step
14 7 3 12 9 11 6 2

7 14 3 12 9 11 2 6

Conquer
3 7 12 14 and
2 6 9 11
combine
step

2 3 6 7 9 11 12 14
Simple_merge(K,FIRST,SECOND,THIRD)
1.[Initialize] 3. [Copy remaining unprocessed
i=FIRST elements]
j=SECOND If i>=SECOND
l=0
Then Repeat while j<=THIRD
2.[Compare]
l=l+1
Repeat while i<SECOND and
Temp[l]=k[j]
j<=THIRD
j=j+1
If K[i]<=K[j]
Else Repeat while i<SECOND
Then l=l+1
l=l+1
Temp[l]=k[i]
Temp[l]=k[i]
i=i+1
i=i+1
Else
4.Repeat for i=1,2,3…l
l=l+1
K[FIRST-1+l]=Temp[l]
Temp[l]=k[j]
j=j+1 5. return
Two_way_merge_sort_r(K,START,FINISH)
1.[Compare the size of the current subtable]
SIZE=FINISH-START+1
2.[Test base condition]
If SIZE<=1
Return
3.[Calculate midpoint]
Middle=START+ ΓSIZE/2˥-1
4. [Recursively sort first subtable]
Call Two_way_merge_sort_r(K,START,Middle)
5. [Recursively sort second subtable]
Call Two_way_merge_sort_r(K,Middle+1, FINISH)
6.[Merge two ordered subtables]
Call Simple_merge(K, START,Middle+1,FINISH)
7. return
Example
T1: 5,12,25 T2: 6,15

Simple_merge(K,FIRST,SECOND,THIRD K= 5 12 25 6 15
) FIRST=1 SECOND=4 THIRD=5
1.[Initialize]
i=FIRST
j=SECOND
1. i=1
l=0 j=4
2.[Compare]
Repeat while i<SECOND and
l=0
j<=THIRD
If K[i]<=K[j]
Then l=l+1
Temp[l]=k[i]
i=i+1
Else
l=l+1
Temp[l]=k[j]
j=j+1
while 3<4 &
i<SECOND & 1<4 & Y 2<4 & 2<4 & 3<4 &
Y Y 6<=5
j<=THIRD 4<=5 4<=5 5<=5 Y 5<=5 False
If K[i]<=K[j] cond
K[1]5<=K[4]6 K[2]12<=k[4]6 K[2]12<=k[5]15 K[3]25<=k[5]15
Yes: l=l+1
l=1 l=3
Temp[l]=k[i]
Temp[1]=k[1] Temp[3]=k[2]
i=i+1
i=2 i=3
No: l=l+1
l=2 l=4
Temp[l]=k[j]
Temp[2]=k[4] Temp[4]=k[5]
j=j+1
j=5 j=6

i j l
Temp[1]=5
1 4 0 index 1 2 3 4 5
Temp[2]=6 value 5 12 25 6 15
2 1 Temp[3]=12
5 2
3 3 Temp[4]=15 FIRST=1
6 4 SECOND=4
THIRD=5
3. [Copy remaining i=3 j=6 l=4
unprocessed elements] 3. If (3)>=4
If i>=SECOND
While(3<4)
Then Repeat while j<=THIRD
l=5
l=l+1
Temp[5]=k[3]=25
Temp[l]=k[j]
j=j+1
i=4
Else Repeat while i<SECOND While(4<4) false
l=l+1 Now,1 temp
Temp 2 array
3 is:4 5
val 5 6 12 15 25
Temp[l]=k[i]
i=i+1 4. k[1]=Temp[1]=5
4.Repeat for i=1,2,3…l k[2]=Temp[2]=6
K[FIRST-1+i]=Temp[i] k[3]=Temp[3]=12
k[4]=Temp[4]=15
5. return
k[5]=Temp[5]=25
T_w_m_s(K,Start,Finish)
Recursion explanation for: 6 5 25 12 15
1.SIZE=FINISH-START+1
T_w_m_s(K,1,5)
2.If SIZE<=1 Return 6 5 25 12 15 T_w_m_s(K,1,3)
6 5 25
3. Middle=START+ ΓSIZE/2˥-1 1.SIZE=5-1+1=5
2.If SIZE<=1 no 1.SIZE=3-1+1=3
4. Call
Two_way_merge_sort_r(K,START,Mid 3. Middle=1+ Γ5/2˥-1= 3 2.If SIZE<=1 no
dle) 3. Middle=1+ Γ3/2˥-1= 2
4. Call T_w_m_s(K,1,3)
5.Call 5 6
Two_way_merge_sort_r(K,Middle+1, 4. Call T_w_m_s(K,1,2)
FINISH) 5. Call T_w_m_s(K,4,5)
6. Call Simple_merge(K, 5. Call T_w_m_s(K,3,3) 25
6. Call S_m(K,1,4,5)
START,Middle+1,FINISH)
7. Return 6. Call S_m(K,1,3,3)
7. Return
7. Return

T_w_m_s(K,3,3) 25 T_w_m_s(K,1,1) 6 T_w_m_s(K,1,2)


Call T_w_m_s(K,2,2) 5 6 5
1.SIZE=3-3+1=1 1.SIZE=1-1+1=1
1.SIZE=2-2+1=1 1.SIZE=2-1+1=2
2.If SIZE<=1 yes 2.If SIZE<=1 yes
2.If SIZE<=1 yes 2.If SIZE<=1 no
3. return 3. return
3. return 3. Middle=1+ Γ2/2˥-1= 1
4. Call T_w_m_s(K,1,1)
In last, two tables which
contains 5 6 and 25 resp. are 5. Call T_w_m_s(K,2,2)
sorted and output is 5 6 25 6. Call S_m(K,1,2,2)
In 6th step, two tables which
contains 6 and 5 resp. are 7. Return
sorted and output is 5 6
• For function call T_w_m_s(K,1,5) [6 5 25 12

15] , one recursive call T_w_m_s(K,1,3) which

sort the half list and output of that function


call is : 5 6 25 is shown in previous slide.

• Do the same for remaining function call


T_w_m_s(K,4,5) Call S_m(K,1,4,5) for
complete trace of algorithm.
Analysis
• Complexity is : O(n logn)
• Merge function runs in O(n ) time when merging n
elements.
• Divide step: constant time
• Conquer Step: recursively sort two subarrays of n/2
size. O(logn)
• Combine step: O(n)time
• Total : : O(n logn)

You might also like