Upcoming SlideShare
×

# Sorting

14,234 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• السلام عليكم

Are you sure you want to  Yes  No

### Sorting

1. 1. S O R T I N G A lecture by ABDUL GHAFFAR Chapter 3 Of Reference #1
2. 2. Reference Books <ul><ul><ul><li>Data Structures and Algorithm Analysis in C </li></ul></ul></ul><ul><ul><ul><ul><li>By Mark Allen Weiss </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Published by Addison Wesley </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Data Structures (Schaum’s Outline Series) </li></ul></ul></ul><ul><ul><ul><ul><li>By Seymour Lipschutz </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Published by Mc Graw Hill </li></ul></ul></ul></ul></ul>
3. 3. Contents <ul><li>Selection sort </li></ul><ul><li>Insertion sort </li></ul><ul><li>Merge sort </li></ul><ul><li>Quick sort </li></ul><ul><li>Heap Sort </li></ul><ul><li>External Sort </li></ul>link2
4. 4. Selection Sort ( Ref # 2- 9.4) <ul><li>Find the Largest element in the list and put it in the last position. </li></ul><ul><li>Find the second largest element in the list and put it in the second last position </li></ul><ul><li>Repeat the process for all elements </li></ul>
5. 5. Selection Sort (continue..)
6. 6. Insertion Sort (Ref #1-7.2, Ref 2- 9.3)
7. 7. Insertion Sort (continue..)
8. 8. Insertion Sort (continue..)
9. 9. Insertion Sort (continue..)
10. 10. Merge Sort (Ref # 1- 7.6, Ref #2-8,9.6) <ul><li>In merge, two sorted sequences are merged into one. Clearly, two sorted sequences each of length n can be merged into a sorted sequence of length 2 n in O (2 n )= O ( n ) steps. However in order to do this, we need space in which to store the result. </li></ul>
11. 11. Merge Sort (continue..) <ul><li>Sorting by merging is a recursive, divide-and-conquer strategy. In the base case, we have a sequence with exactly one element in it. Since such a sequence is already sorted, there is nothing to be done. To sort a sequence of n> 1 elements: </li></ul><ul><ul><li>Divide the sequence into two sequences of length  n/2  </li></ul></ul><ul><ul><li>and  n/2  ; </li></ul></ul><ul><ul><li>recursively sort each of the two subsequences; and then, </li></ul></ul><ul><ul><li>merge the sorted subsequences to obtain the final result. </li></ul></ul>
12. 12. Merge Sort (continue..)
13. 13. Merge Sort (continue..) Implementation in C++
14. 14. Merge Sort (continue..) Implementation in C++ Merging The Merge function of the TwoWayMergeSorter<T> class is defined in Program  . Altogether, this function takes four parameters: The first is a reference to the array to be sorted. The remaining three, left, middle, and right, are unsigned integers. It is assumed that Furthermore, it is assumed that the two subsequences of the array, and are both sorted. The Merge routine merges the two sorted subsequences using the temporary array specified by tempArray. It then copies the merged (and sorted) sequence into the array at
15. 15. Merge Sort (continue..) Implementation in C++
16. 16. Merge Sort (continue..) Implementation in C++
17. 17. Quick Sort ( Ref #1-7.x, Ref#2-x,y) <ul><li>Quicksort is a divide-and-conquer style algorithm. A divide-and-conquer algorithm solves a given problem by splitting it into two or more smaller sub problems, recursively solving each of the sub problems, and then combining the solutions to the smaller problems to obtain a solution to the original one. </li></ul><ul><li>To sort the sequence S={s1,s2,s3,…..,sn} , Quicksort performs the following steps: </li></ul><ul><ul><ul><li>Select one of the elements of S . The selected element, p , is called the pivot  . </li></ul></ul></ul><ul><ul><ul><li>Remove p from S and then partition the remaining elements of S into two distinct sequences, L and G , such that every element in L is less than or equal to the pivot and every element in G is greater than or equal to the pivot. In general, both L and G are unsorted . </li></ul></ul></ul><ul><ul><ul><li>Rearrange the elements of the sequence as follows: (continue..) </li></ul></ul></ul>
18. 18. Quick Sort ( Continue… ) <ul><li>Notice that the pivot is now in the position in which it belongs in the sorted sequence, since all the elements to the left of the pivot are less than or equal to the pivot and all the elements to the right are greater than or equal to it. </li></ul><ul><ul><ul><li>Recursively quicksort the unsorted sequences L and G . </li></ul></ul></ul><ul><li>The first step of the algorithm is a crucial one. We have not specified how to select the pivot. Fortunately, the sorting algorithm works no matter which element is chosen to be the pivot. However, the pivot selection affects directly the running time of the algorithm. If we choose poorly the running time will be poor. </li></ul>
19. 19. Quick Sort ( Continue… ) <ul><li>Next example illustrates the detailed operation of quicksort as it sorts the sequence { 3,1,4,1,5,9,2,5,4) . To begin the sort, we select a pivot. In this example, the value 4 in the last array position is chosen. </li></ul><ul><li>Next, the remaining elements are partitioned into two sequences, one which contains values less than or equal to 4 ( L={3,1,2,1} ) , and one which contains values greater than or equal to 4 ( G={5,9,4,6,5} ). Notice that the partitioning is accomplished by exchanging elements. This is why quicksort is considered to be an exchange sort. </li></ul>Example
20. 20. Quick Sort ( Continue… ) Example
21. 21. Quick Sort ( Continue… ) Example
22. 22. Quick Sort ( Continue… ) <ul><li>void Quicksort( ElementType A[], int N ) </li></ul><ul><li>{ </li></ul><ul><li>Qsort(A, 0, N-1 ); </li></ul><ul><li>} </li></ul>Implementation in C
23. 23. Quick Sort ( Continue… ) Implementation in C
24. 24. Heap Sort ( Ref #1-7.7, Ref #2 -9.10) <ul><li>Some Definitions </li></ul><ul><ul><li>A tree is said to be complete if if all it’s levels, except possibly the last have the maximum number of possible nodes ( 2 for binary tree) </li></ul></ul><ul><ul><li>Heaps can be implemented using an array. The position of an element in the array relates it to other elements of the HEAP. </li></ul></ul>
25. 25. Heap Sort (Continue..) <ul><li>Given an element at position i: </li></ul><ul><ul><li>Parent of i = int[ i / 2 ] , Left child of i = 2i , Right child of i = 2i+1 </li></ul></ul>
26. 26. Heap Sort (Continue..) Array representation of Binary Tree
27. 27. Heap Sort (Continue..) <ul><ul><li>Inserting into a heap </li></ul></ul><ul><ul><ul><li>Suppose H is a heap with n elements </li></ul></ul></ul>
28. 28. Heap Sort (Continue..) <ul><ul><li>Insert into a heap: </li></ul></ul><ul><ul><li>If we want to insert an ITEM into H then we have to follow two steps </li></ul></ul><ul><ul><ul><li>First adjoin ITEM at the end of the H so that H is still a complete tree, but not necessarily a heap. </li></ul></ul></ul><ul><ul><ul><li>Then let ITEM raise to its ‘appropriate place’ in H so that |H is finally a heap </li></ul></ul></ul>
29. 29. Heap Sort (Continue..) <ul><ul><li>Suppose we want to insert 70 in our heap H, the procedure will follow these steps… </li></ul></ul>
30. 30. Heap Sort (Continue..) <ul><ul><li>Final tree after the insertion of element 70, will become like as below, </li></ul></ul>
31. 31. Heap Sort (Continue..) <ul><ul><li>Building a heap: </li></ul></ul><ul><ul><ul><li>Suppose we want to build a heap H from the following list of numbers; </li></ul></ul></ul><ul><ul><ul><li>44, 30, 22, 60, 55, 77, 55 </li></ul></ul></ul><ul><ul><ul><li>This can be build by inserting these number on by one into an empty heap using the insertion procedure, as.. </li></ul></ul></ul>
32. 32. Heap Sort (Continue..) Building a heap
33. 33. Heap Sort (Continue..) Building a heap
34. 34. Heap Sort (Continue..) Building a heap The Algorithm
35. 35. Heap Sort (Continue..) <ul><ul><li>Deleting the root of a heap: </li></ul></ul><ul><ul><li>The procedure to delete the root of a heap H with N elements will follow three steps as, </li></ul></ul><ul><ul><ul><li>Assign the root R to some variable ITEM. </li></ul></ul></ul><ul><ul><ul><li>Replace The deleted node R by the last node L of H so that H is still a complete tree, but not necessarily a heap. </li></ul></ul></ul><ul><ul><ul><li>(Reheap) Let L sink to its appropriate place in H so that H is finally a heap. </li></ul></ul></ul>
36. 36. Heap Sort (Continue..) Deleting the Root, an Example
37. 37. Heap Sort (Continue..) Deleting the root (Algorithm)
38. 38. Heap Sort (Continue..) Application algorithm
39. 39. Heap Sort (Continue..) Implementation in C++
40. 40. Heap Sort (Continue..) Implementation in C++
41. 41. Heap Sort (Continue..) Implementation in C++
42. 42. Heap Sort (Continue..) Implementation in C++
43. 43. Heap Sort (Continue..) Implementation in C++
44. 44. Heap Sort (Continue..) Implementation in C++ The Sorting Phase : Once the max heap has been built, heapsort proceeds to the sorting phase.b The sorting phase of heapsort works like this: We repeatedly swap the largest element in the heap (always in position 1) into the next position of the sorted sequence. After each such swap, there is a new value at the root of the heap and this new value is pushed down into the correct position in the heap using the Percolate Down routine.
45. 45. Heap Sort (Continue..) Implementation in C++ The Sorting Phase : The next example explains this phase
46. 46. Heap Sort (Continue..) Example
47. 47. Heap Sort (Continue..) Example
48. 48. Heap Sort (Continue..) Example
49. 49. Heap Sort (Continue..) Example
50. 50. External Sort
51. 51. External Sort (continue…)
52. 52. External Sort (continue…)