Eng: Mohammed Hussein1Republic of YemenTHAMAR UNIVERSITYFaculty of Computer Science&Information System
Outlines :Eng: Mohammed Hussein2 Recursive algorithms There are two classes of Sorting Algorithms: O(n log n): Quick Sort Merge Sort Heap Sort
Recursive algorithms The rules for designing a recursive algorithm: First, determine the base case. Then determine the general case. Combine the base case and the general cases into an algorithm Each recursive call must reduce the size of the problem and move ittoward the base case. The base case, when reached, must terminate without a call to therecursive algorithm; that is, it must execute a return.3 Eng: Mohammed Hussein
Recursive Algorithm. (Example 1) Computing the factorial function F(n) Factorial (n) =RecursiveAlgorithm----------------------------------Factorial (n)if n=0then return 1return Factorial(n-1) * nIterative Algorithm-----------------------------------Factorial (n)fact ← 1for i ← 2 to n dofact =fact * ireturn fact1 if n=0n * Factorial (n) otherwise4 Eng: Mohammed Hussein
Recursive Algorithm (Example 2) Computing Fibonacci sequence 0 if =01 if =11 2 otherwisenFibonacci n nFibonacci n Fibonacci n RecursiveAlgorithm----------------------------------Fibonacci (n)if n=0 thenreturn 0if n=1 thenreturn 1return Fibonacci (n-1) + Fibonacci (n-2)Iterative Algorithm-----------------------------------Fibonacci (n)if n=0 return 0if n=1 return 1fib1 ← 0fib2 ← 1for i ← 2 to n dofib ← fib1 + fib2fib1 ← fib2fib2 ← fibreturn fib5 Eng: Mohammed Hussein
Recursive Algorithm Analysis When an algorithm contains a recursive call to itself, its runningtime can often be described by a recurrence equation orrecurrence It describes the overall running time on a problem of size n interms of running time on smaller inputs.6 Eng: Mohammed Hussein
Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each withn/2 elements and implemented by means of a doubly linked list, takes at most bn steps,for some constant b. Likewise, the basis case (n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed formsolution to the above equation. That is, a solution that has T(n) only on the left-hand side.2if)2/(22if)(nbnnTnbnT7 Eng: Mohammed Hussein
Quick sort The Quick sort steps are:1. Select an element, called a pivot, from the list.2. The partition step is a process that divides an unsorted array of elements intotwo smaller arrays and the pivot element.The elements to the left of the pivot areall smaller than the pivot and the elements to the right of the pivot are all largerthan the pivot.After this partitioning, the pivot is in its final position.3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements.4. The base case of the recursion are lists of size zero or one, which never need to besorted.8Eng: Mohammed HusseinQuick sort is a divide and conqueralgorithm.Quick sort first divides a large listinto two smaller sub-lists: the lowelements and the high elements.
Quick sort The quick sort is performed by partition step, which divide the arrayinto two sets. To explain the algorithm we begin with unsorted array then we selectone element of the array to be the pivot (.)المحور The pivot should be the middle value of the array like in this example 17or 34 are the best pivots quick sort algorithm face one difficulty which is selecting a correctpivot.9Eng: Mohammed Hussein
Quick sort examples10Eng: Mohammed Hussein In this example the first pivot is 4. When the array elements becamelike: [smaller <= 4 <= larger]. We call again the Partition functionrecursively, which divide the arraybefore and after of the first pivot. At the end all array have beendivided and all pivots collected inthe final array.
Quick sort code: Partition step function11 Eng: Mohammed Hussein If the size less than 2 so it is sorted. Select pivot randomly. The first (Lower) and last (Upper) elements in anarray. The while loops that runs until the upper and lowerare equal. When L=U then we call partition ()recursively untilall elements are sorted.U=(L+1,size-L-1)
Quick sort codeEng: Mohammed Hussein12 Inside this while loopElements that are < pivot . Inside this while loopElements that are > pivot .
Partition stepWe move forward until wefind a value => pivotWe move backward until wefind a value <= pivot13Eng: Mohammed HusseinSmaller < =pivot <= largerSwap the values
Partition step: exampleEng: Mohammed Hussein14 First, pivot in this example is (19). Each time we divide each array intotwo arrays according to a pivot. Second, the pivot (23) of the right array and the pivot (5) of the left array. Last, the pivot (35) of the right array and the pivot (3) of the left array.
Merge sort To merge two sorted arrays, we index both arrays starting at zero,where the smallest element is located. Comparing the elements at each index, we choose the smallerelement, put it into the array that we are merging into. Increment the index of the smaller element. By this method, we continually select the next smallest element fromthe two arrays and merge them into one sorted array.15 Eng: Mohammed Hussein
Merge sort Conceptually, a merge sort worksas follows: Recursion divide the unsorted listinto n sub lists, each containing 1element (a list of 1 element isconsidered sorted). Repeatedly Merge sub lists toproduce new sub lists until there isonly 1 sub list remaining. (This willbe the sorted list.)16 Eng: Mohammed Hussein
Merging stepMerging step is the center future of merge algorithm.Merging step takes two sorted array and merging them in oneorder array.which the first element in array is the smallest one.17Eng: Mohammed Hussein
Merging step Suppose if we have unsorted array like We can think this array as 8 sorted array like We merge each pair into larger array using merging step18 Eng: Mohammed Hussein
Merging stepEng: Mohammed Hussein19 We start by considering each element of the array to be a sorted arrayof length 1.Then we merge each pair of elements into an array oflength 2. Then pairs of arrays of length 2 can be merged into arrays of length4, and so on until we reach the size of the array. The stages of the sort for an array of length 8 that is merge sorted bythree merging step passes.For merge steps we need to allocatenew array for each merge, like thisexample we need 7 array to sort thisarray.Thus, west implement of mergesort with fewer allocation.
Merge sort• The outer loop start at one and doubles the index for each loop until size ofthe array is reached. In side the inner loop, we call the merge function toperform the merging steps.1. &A[j]= pointer to start of the first array to be merged.2. j= the start index of each arrays to be merged.3. i= length of the first array (end index of first array).4. Min(2i,size-j)=is the size of both arrays with the second array and based onthe pointers of first array.• The second array may be shorter than i at the end of the arrayA.20Eng: Mohammed Hussein
Merge step functions First while loop runs twoelements in two arrays to bemerged and copy each smallestelements into temp array. Temp is dynamically allocated, soin this case we need to allocateevery time new array to performmerge steps.Thus, led to westmemory.22 Eng: Mohammed Hussein
Merge step functions The two while loops run tocopy the remaining of largestelements from first or secondarrays into temp array. This final for loop runs tocopy the merge element fromthe temp array backed to theoriginal array (A).23 Eng: Mohammed Hussein
Heap sort After removing the largest item, it reconstructs the heap, removesthe largest remaining item, and places it in the next open positionfrom the end of the partially sorted array.This is repeated until thereare no items left in the heap and the sorted array is full. Elementary implementations require two arrays - one to hold theheap and the other to hold the sorted elements.24 Eng: Mohammed HusseinHeap sort begins by building aheap out of the data set, andthen removing the largest itemand placing it at the end of thepartially sorted array.
Heap sort stepsEng: Mohammed Hussein25 The heap sort algorithm consists of two primary steps: First, we construct a heap from the elements. Second, we repeatedly take the largest element of the heap and swapit with the end until we fully sort the array.
Heap sort code functions26 Eng: Mohammed HusseinAddElement(int,int*,int)RemoveRoot(int*,int)Main ()SwapWithChild(int, int*, int)Left(int)Right(int)Swap(int &, int &)Parent(int)SwapWithParent(int,int*)Swap(int &, int &)
Add element to the heapEng: Mohammed Hussein27 The procedure for adding an element into the heap runs as follows:1. Expand the size of the heap so that it contains the next element of thearray. Put the new element in its place by exchanging it with its parentwhile it is larger than its parent.2. Sort the elements of the heap, starting from the last index to the first. In the image below shows what this looks like after nine elements havebeen sorted.The last nine sorted elements are green, while the firstsix gray elements are still in heap order.
Remove element from the heapEng: Mohammed Hussein28 The procedure for removing a single element from the heap runs asfollows:1. Swap the root element to the end of the heap and shrink the size ofthe heap to remove it from the heap.At this point, the root elementhas been put into its proper sorted position and the new root mayviolate the heap property.2. Fix the new root, swap it with its larger child node as long as one ofthe child nodes is larger.
Example of Heap sort: Add ElementEng: Mohammed Hussein29
Example of Heap sort: Add ElementEng: Mohammed Hussein30
Example of Heap sort: Remove RootEng: Mohammed Hussein31
Example of Heap sort: Remove RootEng: Mohammed Hussein32
Divide-and-Conquer algorithmsEng: Mohammed Hussein35 Heap sort, Merge sort and Quick sort. Why do we need multiple sorting algorithms? Different methods work better in different applications.1. Heap sort uses close to the right number of comparisons but needs tomove data around quite a bit. It can be done in a way that uses very littleextra memory. Its probably good when memory is tight, and you aresorting many small items that come stored in an array.2. Merge sort is good for data thats too big to have in memory at once,because its pattern of storage access is very regular. It also uses evenfewer comparisons than heap sort, and is especially suited for data storedas linked lists.3. Quick sort also uses few comparisons (somewhat more than the othertwo). Like heap sort it can sort "in place" by moving data in an array.
Divide-and-Conquer The whole problem we want to solve may too big to understand or solve atonce. We break it up into smaller pieces, solve the pieces separately, andcombine the separate pieces together. Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two or more disjoint subsets S1, S2,… Recur: solve the subproblems recursively Conquer: combine the solutions for S1, S2, …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations36 Eng: Mohammed Hussein
Reference Wikipedia, the free encyclopedia XoaX.net37 Eng: Mohammed Hussein