Sorting

  • 10,582 views
Uploaded on

 

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
10,582
On Slideshare
0
From Embeds
0
Number of Embeds
18

Actions

Shares
Downloads
0
Comments
1
Likes
36

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. S O R T I N G A lecture by ABDUL GHAFFAR Chapter 3 Of Reference #1
  • 2. Reference Books
        • Data Structures and Algorithm Analysis in C
          • By Mark Allen Weiss
            • Published by Addison Wesley
        • Data Structures (Schaum’s Outline Series)
          • By Seymour Lipschutz
            • Published by Mc Graw Hill
  • 3. Contents
    • Selection sort
    • Insertion sort
    • Merge sort
    • Quick sort
    • Heap Sort
    • External Sort
    link2
  • 4. Selection Sort ( Ref # 2- 9.4)
    • Find the Largest element in the list and put it in the last position.
    • Find the second largest element in the list and put it in the second last position
    • Repeat the process for all elements
  • 5. Selection Sort (continue..)
  • 6. Insertion Sort (Ref #1-7.2, Ref 2- 9.3)
  • 7. Insertion Sort (continue..)
  • 8. Insertion Sort (continue..)
  • 9. Insertion Sort (continue..)
  • 10. Merge Sort (Ref # 1- 7.6, Ref #2-8,9.6)
    • 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.
  • 11. Merge Sort (continue..)
    • 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:
      • Divide the sequence into two sequences of length  n/2 
      • and  n/2  ;
      • recursively sort each of the two subsequences; and then,
      • merge the sorted subsequences to obtain the final result.
  • 12. Merge Sort (continue..)
  • 13. Merge Sort (continue..) Implementation in C++
  • 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. Merge Sort (continue..) Implementation in C++
  • 16. Merge Sort (continue..) Implementation in C++
  • 17. Quick Sort ( Ref #1-7.x, Ref#2-x,y)
    • 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.
    • To sort the sequence S={s1,s2,s3,…..,sn} , Quicksort performs the following steps:
        • Select one of the elements of S . The selected element, p , is called the pivot  .
        • 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 .
        • Rearrange the elements of the sequence as follows: (continue..)
  • 18. Quick Sort ( Continue… )
    • 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.
        • Recursively quicksort the unsorted sequences L and G .
    • 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.
  • 19. Quick Sort ( Continue… )
    • 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.
    • 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.
    Example
  • 20. Quick Sort ( Continue… ) Example
  • 21. Quick Sort ( Continue… ) Example
  • 22. Quick Sort ( Continue… )
    • void Quicksort( ElementType A[], int N )
    • {
    • Qsort(A, 0, N-1 );
    • }
    Implementation in C
  • 23. Quick Sort ( Continue… ) Implementation in C
  • 24. Heap Sort ( Ref #1-7.7, Ref #2 -9.10)
    • Some Definitions
      • 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)
      • Heaps can be implemented using an array. The position of an element in the array relates it to other elements of the HEAP.
  • 25. Heap Sort (Continue..)
    • Given an element at position i:
      • Parent of i = int[ i / 2 ] , Left child of i = 2i , Right child of i = 2i+1
  • 26. Heap Sort (Continue..) Array representation of Binary Tree
  • 27. Heap Sort (Continue..)
      • Inserting into a heap
        • Suppose H is a heap with n elements
  • 28. Heap Sort (Continue..)
      • Insert into a heap:
      • If we want to insert an ITEM into H then we have to follow two steps
        • First adjoin ITEM at the end of the H so that H is still a complete tree, but not necessarily a heap.
        • Then let ITEM raise to its ‘appropriate place’ in H so that |H is finally a heap
  • 29. Heap Sort (Continue..)
      • Suppose we want to insert 70 in our heap H, the procedure will follow these steps…
  • 30. Heap Sort (Continue..)
      • Final tree after the insertion of element 70, will become like as below,
  • 31. Heap Sort (Continue..)
      • Building a heap:
        • Suppose we want to build a heap H from the following list of numbers;
        • 44, 30, 22, 60, 55, 77, 55
        • This can be build by inserting these number on by one into an empty heap using the insertion procedure, as..
  • 32. Heap Sort (Continue..) Building a heap
  • 33. Heap Sort (Continue..) Building a heap
  • 34. Heap Sort (Continue..) Building a heap The Algorithm
  • 35. Heap Sort (Continue..)
      • Deleting the root of a heap:
      • The procedure to delete the root of a heap H with N elements will follow three steps as,
        • Assign the root R to some variable ITEM.
        • 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.
        • (Reheap) Let L sink to its appropriate place in H so that H is finally a heap.
  • 36. Heap Sort (Continue..) Deleting the Root, an Example
  • 37. Heap Sort (Continue..) Deleting the root (Algorithm)
  • 38. Heap Sort (Continue..) Application algorithm
  • 39. Heap Sort (Continue..) Implementation in C++
  • 40. Heap Sort (Continue..) Implementation in C++
  • 41. Heap Sort (Continue..) Implementation in C++
  • 42. Heap Sort (Continue..) Implementation in C++
  • 43. Heap Sort (Continue..) Implementation in C++
  • 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. Heap Sort (Continue..) Implementation in C++ The Sorting Phase : The next example explains this phase
  • 46. Heap Sort (Continue..) Example
  • 47. Heap Sort (Continue..) Example
  • 48. Heap Sort (Continue..) Example
  • 49. Heap Sort (Continue..) Example
  • 50. External Sort
  • 51. External Sort (continue…)
  • 52. External Sort (continue…)