32
5/1/2012 PSD_Merge_Quick_S ort 1 Topics of Discussion 1. Merging of Sorted Arrays. 2. Merge Sort. 3. Quick Sort.

Lec 19 Merge Quick Sort

Embed Size (px)

Citation preview

Page 1: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 1/32

5/1/2012 PSD_Merge_Quick_Sort 1

Topics of Discussion

1. Merging of Sorted Arrays.

2. Merge Sort.

3. Quick Sort.

Page 2: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 2/32

5/1/2012 PSD_Merge_Quick_Sort 2

Merging of two Sorted

ArraysThe Task at hand:

• Given a sorted  Array A [M] [ 0 < M < 20 (say) ] of Data Items .

• Given a 2nd sorted  Array B [N] [ 0 < N < 20 (say) ] of the same

type of Data Items . M, N need not be equal .• Assumed that elements in the sorted  Array A [M] as well as in

the sorted  Array B [N] are both arranged in Ascending Order .{One can apply the same technique even if the elements arearranged in Descending Order as well. }

• Merge the elements of the two sorted arrays A[M] & B[N] to form a3rd sorted array C[M+N] in such away that the Data Items in this3rd Array C[M+N] are also sorted in Ascending Order  and itcontains all the Elements of the two sorted arrays A[M] & B[N].

Page 3: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 3/32

5/1/2012 PSD_Merge_Quick_Sort 3

Merging of two Sorted

Arrays (Example)Given a sorted  Array A [10] [ M = 10 (say) ] of signed Integers :

A[10] = { -23, -12, 5, 5, 15, 15, 15, 56, 78, 90 }.

Here clearly elements are sorted in Ascending Order .

Given a 2nd sorted  Array B [8] [ N = 8 (say) ] of signed Integers

B[8] = { -20, -12, -5, 25, 55, 55, 65, 90 }.

Here also elements are sorted in Ascending Order .

The Merged Array C [18] = {-23, -20, -12, -12, -5, 5, 5, 15, 15, 15, 25,

55, 55, 56, 65, 78, 90, 90 }

Page 4: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 4/32

5/1/2012 PSD_Merge_Quick_Sort 4

Merging of two Sorted

Arrays (Code) - 1void VF_Merge (AI_1D01_T AI_A, int I_A_Size,

AI_1D01_T AI_B, int I_B_Size,

AI_1D01_T AI_C, int I_C_Size)

{//begin Merge

int I_A_Idx;

int I_B_Idx;

int I_C_Idx;

 // Initialization

I_A_Idx = I_B_Idx = I_C_Idx = 0;

Page 5: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 5/32

5/1/2012 PSD_Merge_Quick_Sort 5

Merging of two Sorted

Arrays (Code) - 2while ((I_A_Idx <=I_A_Size) && (I_B_Idx <=I_B_Size))

 // Neither the Array A nor the Array B has been Exhausted{//begin while #1

if (AI_A [I_A_Idx] > AI_B [I_B_Idx])/* Array B‟s Element is SMALLER */ {//begin Then # 1

printf("\n\n The Current Element of array B is Smaller \n\n");printf ("\n\n Hence copying from array B to array C \n\n");

printf (“ \n\n Also Moving Forward within Both the Arrays \n\ n”); 

AI_C [I_C_Idx++] = AI_B [I_B_Idx++];

}//end Then # 1

Page 6: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 6/32

5/1/2012 PSD_Merge_Quick_Sort 6

Merging of two Sorted

Arrays (Code) - 3else /* AI_A [I_A_Idx] <= AI_B [I_B_Idx],

 Array A‟s Element is NOT LARGER */

{//begin Else #1

printf("\n\n The Current Element of array A is NOT Larger \n\n");printf ("\n\n Hence copying from array A to array C \n\n");

printf (“ \n\n Moving Forward within Both the Arrays \n\ n”);

AI_C [I_C_Idx++] = AI_A [I_A_Idx++];

}//end Else #1

}//end while #1

 /* At this point Either Array A or Array B is Exhausted */ 

Page 7: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 7/32

5/1/2012 PSD_Merge_Quick_Sort 7

Merging of two Sorted

Arrays (Code) - 4if (I_A_Idx > I_A_Size)

 /* Array A is Exhausted Copy remaining elements of B into C */ 

{// begin Then #2

printf("\n\n Array A is Exhausted \n\n");

printf("\n\n Copying remaining Elements of the Array B into Array C \n\n");

while (I_B_Idx <= I_B_Size)

AI_C [I_C_Idx++] = AI_B [I_B_Idx++];

}//end Then #2

Page 8: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 8/32

5/1/2012 PSD_Merge_Quick_Sort 8

Merging of two Sorted

Arrays (Code) - 5if (I_B_Idx > I_B_Size)

 /* Array B is Exhausted Copy remaining elements of A */ 

{// begin Then #3

printf("\n\n Array B is Exhausted \n\n");printf("\n\n Copying remaining Elements of the Array A into Array C \n\n");

while (I_A_Idx <= I_A_Size)

AI_C [I_C_Idx++] = AI_A [I_A_Idx++];

}//end Then #3

return;

}//end Merge

Page 9: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 9/32

5/1/2012 PSD_Merge_Quick_Sort 9

Sorting an Array of Elements :In Ascending Order 

• Given n data elements

( 0 < n < 20 [Say] ) stored in an Array A incomplete Random Order.

•  Task at Hand : To arrange these n dataelements in the Array A in Ascending Order.

Page 10: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 10/32

5/1/2012 PSD_Merge_Quick_Sort 10

Merge Sort : In Ascending 

Order 1. If n=1. It is already sorted.

2. If n >1 . The Array is A [ Low_Index, High_Index]

(a) Split the Data Set in the Middle I.e. find out the Middle IndexMid_Index=(Low_Index+High_Index) / 2

(b) Merge Sort recursively the lower half of the ArrayMerge_Sort (A, Low_Index, Mid_Index).

( c) Merge Sort recursively the Upper half of the Array

Merge_Sort (A, Mid_Index +1, High_Index).

(d) Merge the two sorted Halves into one.

N.B: This essentially will lead to Merging of several single elementsinto sorted pairs , merging two sorted pairs into sorted quads andso on.

Page 11: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 11/32

5/1/2012 PSD_Merge_Quick_Sort 11

Merge Sort : An Example.

Page 12: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 12/32

5/1/2012 PSD_Merge_Quick_Sort 12

Merge Sort : Example #2

Page 13: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 13/32

5/1/2012 PSD_Merge_Quick_Sort 13

Merge Sort : Example # 3

Page 14: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 14/32

5/1/2012 PSD_Merge_Quick_Sort 14

Merge Sort : Example # 4

Page 15: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 15/32

5/1/2012 PSD_Merge_Quick_Sort 15

Merge Sort : Example #5

Page 16: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 16/32

5/1/2012 PSD_Merge_Quick_Sort 16

Analysis of Merge Sort - 1

Page 17: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 17/32

5/1/2012 PSD_Merge_Quick_Sort 17

Analysis of Merge Sort - 2

Page 18: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 18/32

5/1/2012 PSD_Merge_Quick_Sort 18

Analysis of Merge Sort - 3

Page 19: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 19/32

5/1/2012 PSD_Merge_Quick_Sort 19

Extra Space needed for Merge Sort

• An Extra Array Size of at most n isrequired to Split & subsequently Mergethe Data.

• The Depth of Recursive Call is O ( log2n).

• Hence Extra Space required

O (n) + O (log 2 n ) = O (n)

Page 20: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 20/32

5/1/2012 PSD_Merge_Quick_Sort 20

Quick Sort : In Ascending Order 

1. If n=1. It is already sorted.2. If n >1 Current array is A [Low_Index, High_Index]

(a) Select a Data Element „p‟ of the array as the Pivot Element.For simplicity let‟s choose the very first element (The 0th Element) as the Pivot.

(b) Partition the Array Elements in such a way around the Pivotthat all elements to the left of the Pivot „p‟ are less than or equal to it , while all elements to the right of the Pivot „p‟ aregreater than it. Note here that the pivot element positionWILL change . Note this Pivot Position (Part_Index)

(c) Quick Sort recursively the left part of the Array by choosing aNew Pivot Quick_Sort (A, Low_Index, Part_Index).

(d) Quick Sort recursively the Right half of the Array

Quick_Sort (A, Part_Index +1, High_Index).

N.B: This essentially will lead to repeated rearranging of theelements around the chosen pivots.

Page 21: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 21/32

5/1/2012 PSD_Merge_Quick_Sort 21

Partition : AlgorithmPartition ( Array Type A , Integer Low_Idx, Integer High_Idx )

Pivot

A [ Low_Idx ] ; Left_Idx

Low_Idx + 1; Right_Idx

High_Idx -1;

WHILE TRUE dodo Right_Idx ( j ) Right_Idx –1 while A [ Right_Idx (j) ].Key > Pivot.Key

 /* Move TOWARDS Left till the Array Element. Key > Pivot.Key*/ 

do Left_Idx ( i ) Left_Idx + 1 while A [ Left_Idx (i) ].Key <= Pivot.Key /* Move TOWARDS Right till Array Element. Key <= Pivot.Key */ 

 /* The Above Loop Conditions ARE Violated i.e.

Either A [ Right_Idx (j) ] .Key <= Pivot .Key OR A [ Left_Idx (i) ]. Key > Pivot.Key. */ 

IF (Left_Idx (i) < Right _Idx (j) ) THEN /* The Two Running Indices have NOT Crossed */ 

SWAP (A [Left_Idx (i) ] , A [ Right_Idx (j) ] );  /* Exchange Out of Order Elements */  ELSE /* The Two Running Indices Have Crossed */  

RETURN (Right_Idx (j) );end WHILE

Page 22: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 22/32

5/1/2012 PSD_Merge_Quick_Sort 22

Quick Sort : Algorithm

Quick_Sort (Array Type A, Integer Low_Idx, Integer High_Idx)

if (Low_Idx < High_Idx)

THEN

Part_Idx

Partition ( A, Low_Idx, High_Idx);Quick_Sort ( A, Low_Idx, Part_Idx );

Quick_Sort (A, Part_Idx+1, High_Idx);

Page 23: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 23/32

5/1/2012 PSD_Merge_Quick_Sort 23

Partitioning in Quick Sort : Example #1 

Page 24: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 24/32

5/1/2012 PSD_Merge_Quick_Sort 24

Partitioning in Quick Sort : Example #2

Page 25: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 25/32

5/1/2012 PSD_Merge_Quick_Sort 25

Partitioning in Quick Sort : Example # 3

Page 26: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 26/32

5/1/2012 PSD_Merge_Quick_Sort 26

Partitioning in Quick Sort : Example #4

Page 27: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 27/32

5/1/2012 PSD_Merge_Quick_Sort 27

Partitioning in Quick Sort : Example # 5

Page 28: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 28/32

5/1/2012 PSD_Merge_Quick_Sort 28

Analysis of Quick Sort - 1

Page 29: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 29/32

5/1/2012 PSD_Merge_Quick_Sort 29

Analysis of Quick Sort - 2

Page 30: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 30/32

5/1/2012 PSD_Merge_Quick_Sort 30

Analysis of Quick Sort - 3

Page 31: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 31/32

5/1/2012 PSD_Merge_Quick_Sort 31

Analysis of Quick Sort – 4Solution for tn

Page 32: Lec 19 Merge Quick Sort

8/3/2019 Lec 19 Merge Quick Sort

http://slidepdf.com/reader/full/lec-19-merge-quick-sort 32/32

5/1/2012 PSD Merge Quick Sort 32

Analysis of Quick Sort[ Extra Space ]