0% found this document useful (0 votes)
41 views32 pages

Topics of Discussion: Merging of Sorted Arrays. 2. Merge Sort. 3. Quick Sort

The document discusses merging sorted arrays, merge sort, and quick sort. It provides examples and pseudocode for merging two sorted arrays into a single sorted array. It also describes the merge sort algorithm which sorts an array by recursively dividing it into halves and merging the sorted halves. Quick sort is also covered, which works by picking pivots and partitioning the array into subarrays with all elements smaller than the pivot coming before all larger elements. Worst and average case time complexities are analyzed for both algorithms.

Uploaded by

Nitish Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views32 pages

Topics of Discussion: Merging of Sorted Arrays. 2. Merge Sort. 3. Quick Sort

The document discusses merging sorted arrays, merge sort, and quick sort. It provides examples and pseudocode for merging two sorted arrays into a single sorted array. It also describes the merge sort algorithm which sorts an array by recursively dividing it into halves and merging the sorted halves. Quick sort is also covered, which works by picking pivots and partitioning the array into subarrays with all elements smaller than the pivot coming before all larger elements. Worst and average case time complexities are analyzed for both algorithms.

Uploaded by

Nitish Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Topics of Discussion

1. Merging of Sorted Arrays.

2. Merge Sort. 3. Quick Sort.

5/1/2012

PSD_Merge_Quick_Sort

Merging of two Sorted Arrays


The 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 are arranged in Descending Order as well. } Merge the elements of the two sorted arrays A[M] & B[N] to form a 3rd sorted array C[M+N] in such away that the Data Items in this 3rd Array C[M+N] are also sorted in Ascending Order and it contains all the Elements of the two sorted arrays A[M] & B[N].

5/1/2012

PSD_Merge_Quick_Sort

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 }
5/1/2012 PSD_Merge_Quick_Sort 3

Merging of two Sorted Arrays (Code) - 1


void 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;
5/1/2012 PSD_Merge_Quick_Sort 4

Merging of two Sorted Arrays (Code) - 2


while ((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 Bs 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
5/1/2012 PSD_Merge_Quick_Sort 5

Merging of two Sorted Arrays (Code) - 3


else /* AI_A [I_A_Idx] <= AI_B [I_B_Idx], Array As 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 */
5/1/2012 PSD_Merge_Quick_Sort 6

Merging of two Sorted Arrays (Code) - 4


if (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
5/1/2012 PSD_Merge_Quick_Sort 7

Merging of two Sorted Arrays (Code) - 5


if (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

5/1/2012

PSD_Merge_Quick_Sort

Sorting an Array of Elements : In Ascending Order


Given n data elements ( 0 < n < 20 [Say] ) stored in an Array A in complete Random Order. Task at Hand : To arrange these n data elements in the Array A in Ascending Order.

5/1/2012

PSD_Merge_Quick_Sort

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 Index Mid_Index=(Low_Index+High_Index) / 2 (b) Merge Sort recursively the lower half of the Array Merge_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 elements into sorted pairs , merging two sorted pairs into sorted quads and so on.
5/1/2012 PSD_Merge_Quick_Sort 10

Merge Sort : An Example.

5/1/2012

PSD_Merge_Quick_Sort

11

Merge Sort : Example #2

5/1/2012

PSD_Merge_Quick_Sort

12

Merge Sort : Example # 3

5/1/2012

PSD_Merge_Quick_Sort

13

Merge Sort : Example # 4

5/1/2012

PSD_Merge_Quick_Sort

14

Merge Sort : Example #5

5/1/2012

PSD_Merge_Quick_Sort

15

Analysis of Merge Sort - 1

5/1/2012

PSD_Merge_Quick_Sort

16

Analysis of Merge Sort - 2

5/1/2012

PSD_Merge_Quick_Sort

17

Analysis of Merge Sort - 3

5/1/2012

PSD_Merge_Quick_Sort

18

Extra Space needed for Merge Sort


An Extra Array Size of at most n is required to Split & subsequently Merge the Data. The Depth of Recursive Call is O ( log2n).

Hence Extra Space required O (n) + O (log 2 n ) = O (n)


5/1/2012 PSD_Merge_Quick_Sort 19

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 lets choose the very first element (The 0th Element) as the Pivot. (b) Partition the Array Elements in such a way around the Pivot that 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 are greater than it. Note here that the pivot element position WILL change . Note this Pivot Position (Part_Index) (c) Quick Sort recursively the left part of the Array by choosing a New 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 the elements around the chosen pivots.
PSD_Merge_Quick_Sort

5/1/2012

20

Partition : Algorithm
Partition ( 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 do do 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

5/1/2012

PSD_Merge_Quick_Sort

21

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);

5/1/2012

PSD_Merge_Quick_Sort

22

Partitioning in Quick Sort : Example #1

5/1/2012

PSD_Merge_Quick_Sort

23

Partitioning in Quick Sort : Example #2

5/1/2012

PSD_Merge_Quick_Sort

24

Partitioning in Quick Sort : Example # 3

5/1/2012

PSD_Merge_Quick_Sort

25

Partitioning in Quick Sort : Example #4

5/1/2012

PSD_Merge_Quick_Sort

26

Partitioning in Quick Sort : Example # 5

5/1/2012

PSD_Merge_Quick_Sort

27

Analysis of Quick Sort - 1

5/1/2012

PSD_Merge_Quick_Sort

28

Analysis of Quick Sort - 2

5/1/2012

PSD_Merge_Quick_Sort

29

Analysis of Quick Sort - 3

5/1/2012

PSD_Merge_Quick_Sort

30

Analysis of Quick Sort 4 Solution for tn

5/1/2012

PSD_Merge_Quick_Sort

31

Analysis of Quick Sort [ Extra Space ]

5/1/2012

PSD_Merge_Quick_Sort

32

You might also like