1 Name: Class: Muhammad Saim ul haq BSSE(3A) Roll Number: 21-Arid-3807 Subject: Data Structure And Algorithm 2 Name: Class: Kamran Arshad BSSE(3A) Roll Number: 21-Arid-3799 Subject: Data Structure And Algorithm 3 Name: Ayesha Ameen Class: BSSE(3A) Roll Number: 21-Arid-3794 Subject: Data Structure And Algorithm 4 MERGE SORT The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer paradigm. In this algorithm, the array is initially divided into two equal halves and then they are combined in a sorted manner. 5 How Merge Sort Works? • Merge sort first divides the array into equal halves and then combines them in a sorted manner. • To understand merge sort, we take an unsorted array as the following − 6 We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4. 7 • This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves. • We further divide these arrays and we achieve atomic value which can no more be divided. • Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists. 8 • In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a sorted order. • After the final merging, the list should look like this − 9 • Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller sorted lists keeping the new list sorted too. 10 INSERTION SORT INTRODUCTION: Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each iteration. It works similar to the way you sort playing cards in your hands. Insertion sort is efficient for small data values. 11 Working of Insertion Sort Suppose we need to sort the following array. The first element in the array is assumed to be sorted. Take the second element and store it separately in (KEY) Compare (KEY) with the first element. Ifthe first element is greater than (key), then (KEY) is placed in front of the first element. 12 Now, the first two elements are sorted. Take the third element and compare it with the elements on the left of it. Placed it just behind the element smaller than it. Ifthere is no element smaller than it, then place it at the beginning of the array. 13 Similarly, place every unsorted element at its correct position. Shell sort Shell sort is a generalized version of the insertion sort algorithm. It first sorts elements that are far apart from each other and suc c essively reduces the interval between the elements to be sorted. The interval between the elements is reduced based on the sequence used. We keep reducing the value of the interval until it becomes 1. Sort the Data Set by Shell Sorting Method 70 80 15 30 60 20 First We have to find the Gap 0 1 2 3 4 5 70 80 15 30 60 20 Formula : Interval/G ap= Floor(n/2) n= Length= 6 Put the Value in Formula : Interval/G ap= Floor(6/2) =3 0 1 2 3 4 5 70 80 15 30 60 20 70 30 80 60 20 15 Pass 1: (0,3) = 30 40 15 70 60 20 (1,4) = 30 60 15 70 80 20 (2,5) = 30 60 15 70 80 20 18 30 60 15 70 80 20 Find the Again Gap Value by Using the Previous Gap Value • Interval/Gap=Floor(n/2) =Floor(3/2) Pass 2: =Floor(1.5)=1 30 60 30 60 15 15 30 60 15 60 30 15 60 15 20 15 30 30 70 80 20 70 80 20 80 20 80 20 70 70 70 60 80 70 20 80 20 Bubble Sorting Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items. 21 How Bubble Sort Works? We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise. 14 33 27 35 10 Bubble sort starts with very first two elements, comparing them to check which one is greater. 14 33 27 35 10 22 In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27. 14 33 27 35 10 We find that 27 is smaller than 33 and these two values must be swapped. 14 27 33 35 10 Next we compare 33 and 35. We find that both are in already sorted positions. 14 27 33 35 10 23 Then we move to the next two values, 35 and 10. We know then that 10 is smaller 35. Hence they are not sorted. 14 27 33 35 10 We swap these values. We find that we have reached the end of the array. After one iteration, the array should look like this 14 27 33 10 35 To be precise, we are now showing how an array should look like after each iteration. After the second iteration, it should look like this 14 27 10 33 35 24 Notice that after each iteration, at least one value moves at the end. 14 10 27 33 35 And when there's no swap required, bubble sorts learns that an array is completely sorted. 10 14 27 33 35 25 Selection Sort • The selection sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted part and putting it at the beginning. • In every iteration of the selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarrays. 26 Selection Sort • The selection sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted part and putting it at the beginning. • In every iteration of the selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarrays. 27 How Selection Sort work? Consider the following array as an example. 7 6 3 4 1 For the first position in the sorted list, 7 in first position is stored presently, we search the whole list and find 1 is the lowest value. 7 6 3 4 1 28 How Selection Sort work? Consider the following array as an example. 7 6 3 4 1 For the first position in the sorted list, 7 in first position is stored presently, we search the whole list and find 1 is the lowest value. 7 6 3 4 1 29 So we replace 7 with 1.After one iteration 10, which happens to the minimum value in the list, appears in the first position of the sorted list. 1 6 3 4 7 For the second position, where 6 is residing, we start scanning the rest of the list in a linear manner. 1 6 3 4 7 We find that 3 is the second lowest value in the list and it should appear at the second place. We swap these values. 30 So we replace 7 with 1.After one iteration 10, which happens to the minimum value in the list, appears in the first position of the sorted list. 1 6 3 4 7 For the second position, where 6 is residing, we start scanning the rest of the list in a linear manner. 1 6 3 4 7 We find that 3 is the second lowest value in the list and it should appear at the second place. We swap these values. 31 1 3 6 4 7 After two iterations, two least values are positioned at the beginning in a sorted manner. 1 3 6 4 7 The same process is applied to the rest of the entire in the array. 1 3 4 6 7 32 1 3 6 4 7 After two iterations, two least values are positioned at the beginning in a sorted manner. 1 3 6 4 7 The same process is applied to the rest of the entire in the array. 1 3 4 6 7