SlideShare une entreprise Scribd logo
1  sur  72
Data Structures and Algorithms
Objectives


                In this session, you will learn to:
                   Sort data by using quick sort
                   Sort data by using merge sort
                   Search data by using linear search technique
                   Search data by using binary search technique




     Ver. 1.0                                                     Session 5
Data Structures and Algorithms
Sorting Data by Using Quick Sort


                Quick sort algorithm:
                   Is one of the most efficient sorting algorithms
                   Is based on the divide and conquer approach
                   Successively divides the problem into smaller parts until the
                   problems become so small that they can be directly solved




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm


                In quick sort algorithm, you:
                   Select an element from the list called as pivot.
                   Partition the list into two parts such that:
                     – All the elements towards the left end of the list are smaller than
                       the pivot.
                     – All the elements towards the right end of the list are greater than
                       the pivot.
                   Store the pivot at its correct position between the two parts of
                   the list.
                You repeat this process for each of the two sublists created
                after partitioning.
                This process continues until one element is left in each
                sublist.



     Ver. 1.0                                                                      Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      To understand the implementation of quick sort algorithm,
                      consider an unsorted list of numbers stored in an array.




                         0   1    2    3    4    5    6    7
                arr     28   55   46   38   16   89   83   30




     Ver. 1.0                                                              Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Let us sort this unsorted list.




                         0    1    2     3    4     5   6    7
                arr     28    55   46    38   16   89   83   30




     Ver. 1.0                                                     Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Select a Pivot.




                         0      1    2    3    4    5    6    7
                arr     28      55   46   38   16   89   83   30


                        Pivot




     Ver. 1.0                                                      Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Start from the left end of the list (at index 1).
                      Move in the left to right direction.
                      Search for the first element that is greater than the pivot
                      value.
                                     Greater element


                         0      1     2      3    4    5    6    7
                arr     28      55    46     38   16   89   83   30


                        Pivot
                             Greater Value




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Start from the right end of the list.
                      Move in the right to left direction.
                      Search for the first element that is smaller than or equal to
                      the pivot value.
                                                                          Smaller element


                         0      1    2       3     4     5       6   7
                arr     28      55   46      38    16   89   83      30


                        Pivot
                             Greater Value       Smaller Value




     Ver. 1.0                                                                      Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Interchange the greater value with smaller value.




                                         Swap
                         0      1    2       3     4     5       6   7
                arr     28      16
                                55   46      38    16
                                                   55   89   83      30


                        Pivot
                             Greater Value       Smaller Value




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Continue the search for an element greater than the pivot.
                      Start from arr[2] and move in the left to right direction.
                      Search for the first element that is greater than the pivot
                      value.
                                           Greater element

                         0      1      2     3      4    5    6    7
                arr     28      16     46    38     55   89   83   30


                        Pivot
                                    Greater Value




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Continue the search for an element smaller than the pivot.
                      Start from arr[3] and move in the right to left direction.
                      Search for the first element that is smaller than or equal to
                      the pivot value.
                                                                        Smaller element


                         0      1      2    3       4    5    6    7
                arr     28      16     46   38      55   89   83   30


                        Pivot
                                    Greater Value
                        Smaller Value



     Ver. 1.0                                                                     Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      The smaller value is on the left hand side of the greater
                      value.
                      Values remain same.




                         0      1      2    3       4    5    6    7
                arr     28      16     46   38      55   89   83   30


                        Pivot
                                    Greater Value
                        Smaller Value



     Ver. 1.0                                                                Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      List is now partitioned into two sublists.
                      List 1 contains all values less than or equal to the pivot.
                      List 2 contains all the values greater than the pivot.




                         0 0 1 1 2      3    4 2 53 6 4 7 5         6    7
                arr     28 16 16 46
                         28             38   55 46 38 55 89
                                                 89 83 30           83   30

                            List 1                         List 2
                        Pivot




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Replace the pivot value with the last element of List 1.
                      The pivot value, 28 is now placed at its correct position in
                      the list.



                           Swap
                           0     1             2    3    4        5   6    7
                arr       28
                          16    16
                                28             46   38   55      89   83   30

                            List 1                           List 2




     Ver. 1.0                                                                   Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Truncate the last element, that is, pivot from List 1.




                           00    1              2    3    4        5   6    7
                arr       16    28              46   38   55      89   83   30

                            List 1                            List 2




     Ver. 1.0                                                                    Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      List 1 has only one element.
                      Therefore, no sorting required.




                           0                  2    3    4        5   6    7
                arr       16                  46   38   55      89   83   30

                            List 1                          List 2




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Sort the second list, List 2.




                           0                    2     3    4        5   6    7
                arr       16                    46    38   55      89   83   30

                            List 1                             List 2




     Ver. 1.0                                                                     Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Select a pivot.
                      The pivot in this case will be arr[2], that is, 46.




                           0                     2      3    4        5   6    7
                arr       16                     46     38   55      89   83   30

                            List 1                               List 2
                                                Pivot




     Ver. 1.0                                                                       Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Start from the left end of the list (at index 3).
                      Move in the left to right direction.
                      Search for the first element that is greater than the pivot
                      value.
                                                      Greater element


                           0                   2      3      4        5   6    7
                arr       16                    46    38     55      89   83   30

                            List 1                               List 2
                                              Pivot
                                                          Greater Value




     Ver. 1.0                                                                       Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Start from the right end of the list (at index 7).
                      Move in the right to left direction.
                      Search for the first element that is smaller than or equal to
                      the pivot value.
                                                                           Smaller element


                           0                   2      3      4        5   6    7
                arr       16                   46     38     55      89   83   30

                            List 1                               List 2
                                              Pivot
                                                          Greater Value Smaller Value




     Ver. 1.0                                                                       Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Interchange the greater value with smaller value.




                                                                   Swap
                           0                  2      3      4       5 6       7
                arr       16                  46     38     30
                                                            55      89   83   30
                                                                              55

                            List 1                              List 2
                                             Pivot
                                                         Greater Value Smaller Value




     Ver. 1.0                                                                      Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Continue the search for an element greater than the pivot.
                      Start from arr[5] and move in the left to right direction.
                      Search for the first element that is greater than the pivot
                      value.
                                                                   Greater element


                           0                  2      3    4        5   6      7
                arr       16                   46    38   30      89   83     55

                            List 1                            List 2
                                             Pivot
                                                              Greater Value




     Ver. 1.0                                                                      Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Continue the search for an element smaller than the pivot.
                      Start from arr[6] and move in the right to left direction.
                      Search for the first element that is smaller than the pivot
                      value.
                                                                           Smaller element


                           0                  2      3    4        5   6      7
                arr       16                  46     38   30      89   83     55

                            List 1                            List 2
                                             Pivot
                                                              Greater Value
                                                     Smaller Value



     Ver. 1.0                                                                       Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      The smaller value is on the left hand side of the greater
                      value.
                      Values remain same.




                           0                   2      3    4        5   6      7
                arr       16                   46     38   30      89   83     55

                            List 1                             List 2
                                              Pivot
                                                               Greater Value
                                                      Smaller Value



     Ver. 1.0                                                                       Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Divide the list into two sublists.
                      Sublist 1 contains all values less than or equal to the pivot.
                      Sublist 2 contains all the values greater than the pivot.




                           0    1          2    3    4     5    6    7
                arr       16    28        46   38   30     89   83   55




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                      Replace the pivot value with the last element of Sublist 1.
                      The pivot value, 46 is now placed at its correct position in
                      the list.
                      This process is repeated until all elements reach their
                      correct position.
                                               Swap
                           0    1          2      3        4   5      6    7
                arr       16    28        46
                                          30     38    30
                                                       46      89     83    55


                                               Sublist 1            Sublist 2




     Ver. 1.0                                                                    Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                Write an algorithm to implement quick sort:
                 QuickSort(low,high)
                    1. If (low > high):
                          a. Return
                    2.   Set pivot = arr[low]
                    3.   Set i = low + 1
                    4.   Set j = high
                    5.   Repeat step 6 until i > high or arr[i] > pivot // Search for an

                               // element greater than
                                                                                               //
                    pivot
                    6. Increment i by 1
                    7. Repeat step 8 until j < low or arr[j] < pivot // Search for an element

                            // smaller than pivot
                    8. Decrement j by 1
                    9. If i < j: // If greater element is on the left of smaller element
     Ver. 1.0                                                                              Session 5
Data Structures and Algorithms
Implementing Quick Sort Algorithm (Contd.)


                10. If i <= j:
                      a. Go to step 5 // Continue the search
                11. If low < j:
                      a. Swap arr[low] with arr[j] // Swap pivot with last element in
                                                      // first part of the list
                12. QuickSort(low, j – 1) // Apply quicksort on list left to pivot
                13. QuickSort(j + 1, high) // Apply quicksort on list right to pivot




     Ver. 1.0                                                                           Session 5
Data Structures and Algorithms
Determining the Efficiency of Quick Sort Algorithm


                • The total time taken by this sorting algorithm depends on
                  the position of the pivot value.
                • The worst case occurs when the list is already sorted.
                • If the first element is chosen as the pivot, it leads to a worst
                                           2
                  case efficiency of O(n ).
                • If you select the median of all values as the pivot, the
                  efficiency would be O(n log n).




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Just a minute


                What is the total number of comparisons for an average
                case in a quick sort algorithm?




                Answer:
                   O(n log n)




     Ver. 1.0                                                       Session 5
Data Structures and Algorithms
Activity: Sorting Data by Using Quick Sort Algorithm


                Problem Statement:
                   Write a program that stores 10 numbers in an array, and sorts
                   them by using the quick sort algorithm.




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Sorting Data by Using Merge Sort


                Merge sort algorithm:
                   Is based on the divide and conquer approach
                   Divides the list into two sublists of sizes as nearly equal as
                   possible
                   Sorts the two sublists separately by using merge sort
                   Merges the sorted sublists into one single list




     Ver. 1.0                                                                 Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm


                      To understand the implementation of merge sort algorithm,
                      consider an unsorted list of numbers stored in an array.




                        0    1    2    3    4   5    6
                arr     53   10   30   76   3   57   24




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      Let us sort this unsorted list.




                         0    1    2     3    4     5   6
                arr     53    10   30    76   3   57    24




     Ver. 1.0                                                Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      The first step to sort data by using merge sort is to split the
                      list into two parts.




                         0    1    2    3    4     5   6
                arr     53   10    30   76   3    57   24




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      The first step to sort data by using merge sort is to split the
                      list into two parts.




                         0    1    2    3      4    5     6
                arr     53   10    30   76     3    57   24




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      The list has odd number of elements, therefore, the left
                      sublist is longer than the right sublist by one entry.




                         0   1    2    3     4     5    6
                arr     53   10   30   76    3    57   24




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      Further divide the two sublists into nearly equal parts.




                         0    1    2 2 3 3    4 4 5 5 6      6
                arr     53   10
                             10    30 76
                                  30 76       3 3 5757 24 24




     Ver. 1.0                                                                    Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      Further divide the sublists.




                         0    11     22 3     3 4    45    56   6
                arr     53   1010 30 30 76 76 3
                             10    30 76             357   24
                                                           57   24




     Ver. 1.0                                                        Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      There is a single element left in each sublist.
                      Sublists with one element require no sorting.




                         0      1      2      3      4      5       6
                arr     53     10     30     76      3      57     24




     Ver. 1.0                                                           Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      Start merging the sublists to obtain a sorted list.




                         0    11     22 3     3 4     5
                                                      4     6
                                                            5       6
                arr     10
                        53   5310   3030 76   76 3   57
                                                     3      57
                                                            24
                                                            57      24




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      Further merge the sublists.




                        0    1    22 3 3     4 4 55   6 6
                arr     10   30
                             53   53 7676
                                    30       3 3 2457 57 57
                                                         24




     Ver. 1.0                                                 Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      Again, merge the sublists.




                        0
                        0    1
                             1    2
                                  2   3    44      55 6 6
                arr     10
                        3    30
                             10   53 76
                                  24 30    53 5724 7657
                                            3




     Ver. 1.0                                               Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                      The list is now sorted.




                        0    1    2    3    4    5    6
                arr     3    10   24   30   53   57   76




     Ver. 1.0                                              Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                Write an algorithm to implement merge sort:
                MergeSort(low,high)
                   1. If (low >= high):
                       a. Return
                   2. Set mid = (low + high)/2
                   3. Divide the list into two sublists of nearly equal lengths, and sort
                       each sublist by using merge sort. The steps to do this are as
                           follows:
                       a. MergeSort(low, mid
                       b. MergeSort(mid + 1, high)
                   4. Merge the two sorted sublists:
                       a. Set i = low
                      b. Set j = mid + 1
                       c. Set k = low
                       d. Repeat until i > mid or j > high: // This loop will terminate when
                                                                                     // you reach the
                   end of one of the
                                        // two sublists.

     Ver. 1.0                                                                               Session 5
Data Structures and Algorithms
Implementing Merge Sort Algorithm (Contd.)


                          i. If (arr[i] <= arr[j])
                               Store arr[i] at index k in array B
                               Increment i by 1
                              Else
                               Store arr[j] at index k in array B
                               Increment j by 1
                          ii. Increment k by 1
                        e. Repeat until j > high: // If there are still some elements in the
                                                        // second sublist append them to the new list

                          i. Store arr[j] at index k in array B
                          ii. Increment j by 1
                          iii. Increment k by 1
                       f. Repeat until i > mid: // If there are still some elements in the
                                                        // first sublist append them to the new list
                          i. Store arr[i] at index k in array B
                          ii. Increment I by 1
                          iii. Increment k by 1
                5. Copy all elements from the sorted array B into the original array arr


     Ver. 1.0                                                                               Session 5
Data Structures and Algorithms
Determining the Efficiency of Merge Sort Algorithm


                To sort the list by using merge sort algorithm, you need to
                recursively divide the list into two nearly equal sublists until
                each sublist contains only one element.
                To divide the list into sublists of size one requires log n
                passes.
                In each pass, a maximum of n comparisons are performed.
                Therefore, the total number of comparisons will be a
                maximum of n × log n.
                The efficiency of merge sort is equal to O(n log n)
                There is no distinction between best, average, and worst
                case efficiencies of merge sort because all of them require
                the same amount of time.



     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Just a minute


                Which algorithm uses the following procedure to sort a
                given list of elements?
                1. Select an element from the list called a pivot.
                2. Partition the list into two parts such that one part contains
                   elements lesser than the pivot, and the other part contains
                   elements greater than the pivot.
                3. Place the pivot at its correct position between the two lists.
                4. Sort the two parts of the list using the same algorithm.


                Answer:
                    Quick sort




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Just a minute


                On which algorithm design technique are quick sort and
                merge sort based?




                Answer:
                   Quick sort and merge sort are based on the divide and conquer
                   technique.




     Ver. 1.0                                                            Session 5
Data Structures and Algorithms
Performing Linear Search


                Linear Search:
                   Is the simplest searching method
                   Is also referred to as sequential search
                   Involves comparing the items sequentially with the elements in
                   the list




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Implementing Linear Search


                The linear search would begin by comparing the required
                element with the first element in the list.
                If the values do not match:
                   The required element is compared with the second element in
                    the list.
                If the values still do not match:
                   The required element is compared with the third element in the
                   list.
                This process continues, until:
                   The required element is found or the end of the list is reached.




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Linear Search (Contd.)


                Write an algorithm to search for a given employee ID in a
                list of employee records by using linear search algorithm:
                 1.   Read the employee ID to be searched
                 2.   Set i = 0
                 3.   Repeat step 4 until i > n or arr[i] = employee ID
                 4.   Increment i by 1
                 5.   If i > n:
                       Display “Not Found”
                      Else
                       Display “Found”




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Determining the Efficiency of Linear Search


                The efficiency of a searching algorithm is determined by the
                running time of the algorithm.
                In the best case scenario:
                   The element is found at the first position in the list.
                   The number of comparisons in this case is 1.
                   The best case efficiency of linear search is therefore, O(1).
                In the worst case scenario:
                   The element is found at the last position of the list or does not
                   exists in the list.
                   The number of comparisons in this case is equal to the number
                   of elements.
                   The worst case efficiency of linear search is therefore, O(n).




     Ver. 1.0                                                                Session 5
Data Structures and Algorithms
Determining the Efficiency of Linear Search (Contd.)


                In the average case scenario:
                   The number of comparisons for linear search can be
                   determined by finding the average of the number of
                   comparisons in the best and worst case.
                The average case efficiency of linear search is 1/2(n + 1).




     Ver. 1.0                                                           Session 5
Data Structures and Algorithms
Just a minute


                You have to apply linear search to search for an element in
                an array containing 5,000 elements. If, at the end of the
                search, you find that the element is not present in the array,
                how many comparisons you would have made to search the
                required element in the given list?




                Answer:
                   5,000




     Ver. 1.0                                                          Session 5
Data Structures and Algorithms
Activity: Performing Linear Search


                Problem Statement:
                   Write a program to search a given number in an array that
                   contains a maximum of 20 numbers by using the linear search
                   algorithm. If there are more than one occurrences of the
                   element to be searched, then the program should display the
                   position of the first occurrence. The program should also
                   display the total number of comparisons made.




     Ver. 1.0                                                           Session 5
Data Structures and Algorithms
Performing Binary Search


                Binary search algorithm:
                   Is used for searching large lists
                   Searches the element in very few comparisons
                   Can be used only if the list to be searched is sorted




     Ver. 1.0                                                              Session 5
Data Structures and Algorithms
Implementing Binary Search


                Consider an example.
                You have to search for the name Steve in a telephone
                directory that is sorted alphabetically.
                   To search the name Steve by using binary search algorithm:
                    – You open the telephone directory at the middle to determine which
                      half contains the name.
                    – Open that half at the middle to determine which quarter of the
                      directory contains the name.
                – Repeat this process until the name Steve is not found.
                – Binary search reduces the number of pages to be searched by
                  half each time.




     Ver. 1.0                                                                  Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                      Consider a list of 9 elements in a sorted array.




                         0    1    2    3    4    5    6    7    8
                arr      9   13    17   19   25   29   39   40   47




     Ver. 1.0                                                            Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                      You have to search an element 13 in the given list.




                         0   1    2    3    4    5    6    7    8
                arr      9   13   17   19   25   29   39   40   47




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                      Determine the index of the middlemost element in the list:
                         Mid = (Lower bound + Upper bound)/2
                             = (0 + 8)/2
                             =4




                         0   1    2    3    4     5     6    7    8
                arr      9   13   17   19   25   29     39   40   47


                  Lower bound          Middle element        Upper bound




     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                      13 is not equal to the middle element, therefore, again
                      divide the list into two halves:
                          Mid = (Lower bound + Upper bound)/2
                               = (0 + 3)/2
                               =1


                         0   1    2     3    4     5     6    7    8
                arr      9   13   17    19   25   29     39   40   47


            Lower bound            Upper bound                Upper bound

                       Middle element   Middle element



     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                      13 is equal to middle element.
                      Element found at index 1.




                         0   1    2    3    4    5     6    7    8
                arr      9   13   17   19   25   29    39   40   47


            Lower bound            Upper bound

                       Element found



     Ver. 1.0                                                         Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                Write an algorithm to implement binary search algorithm.
                 1.   Accept the element to be searched
                 2.   Set lowerbound = 0
                 3.   Set upperbound = n – 1
                 4.   Set mid = (lowerbound + upperbound)/2
                 5.   If arr[mid] = desired element:
                       a. Display “Found”
                       b. Go to step 10
                 6. If desired element < arr[mid]:
                       a. Set upperbound = mid – 1




     Ver. 1.0                                                        Session 5
Data Structures and Algorithms
Implementing Binary Search (Contd.)


                1. If desired element > arr[mid]:
                    a. Set lowerbound = mid + 1
                2. If lowerbound <= upperbound:
                    a. Go to step 4
                3. Display “Not Found”
                4. Exit




     Ver. 1.0                                       Session 5
Data Structures and Algorithms
Determining the Efficiency of Binary Search


                In binary search, with every step, the search area is
                reduced to half.
                In the best case scenario, the element to be search is found
                at the middlemost position of the list:
                   The number of comparisons in this case is 1.
                In the worst case scenario, the element is not found in the
                list:
                 – After the first bisection, the search space is reduced to n/2
                   elements, where n is the number of elements in the original list.
                 – After the second bisection, the search space is reduced to n/4
                                           2
                   elements, that is, n/2 elements.
                 – After ith bisections, the number of comparisons would be n/2i
                   elements.



     Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Just a minute


                In ___________ search algorithm, you begin at one end of
                the list and scan the list until the desired item is found or the
                end of the list is reached.




                Answer:
                   linear




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Just a minute


                To implement __________ search algorithm, the list should
                be sorted.




                Answer:
                   binary




     Ver. 1.0                                                      Session 5
Data Structures and Algorithms
Activity: Performing Binary Search


                Problem Statement:
                   Write a program to search a number in an array that contains a
                   maximum of 20 elements by using binary search. Assume that
                   the array elements are entered in ascending order. If the
                   number to be searched is present at more than one location in
                   the array, the search should stop when one match is found.
                   The program should also display the total number of
                   comparisons made.




     Ver. 1.0                                                             Session 5
Data Structures and Algorithms
Summary


               In this session, you learned that:
                   Quick sort and merge sort algorithms are based on the divide
                   and conquer technique.
                   To sort a list of items by using the quick sort algorithm, you
                   need to:
                       Select a pivot value.
                       Partition the list into two sublists such that one sublist contains
                       all items less than the pivot, and the second sublist contains all
                       items greater than the pivot.
                       Place the pivot at its correct position between the two sublists.
                       Sort the two sublists by using quick sort.




    Ver. 1.0                                                                       Session 5
Data Structures and Algorithms
Summary (Contd.)


               –   The total time taken by the quick sort algorithm depends on
                   the position of the pivot value and the initial ordering of
                   elements.
                                                                               2
               –   The worst case efficiency of the quick sort algorithm is O(n ).
               –   The best case efficiency of the quick sort algorithm is O(n log
                   n).
               –   To sort a list of items by using merge sort, you need to:
                   –   Divide the list into two sublists.
                   –   Sort each sublist by using merge sort.
                   –   Merge the two sorted sublists.
               –   The merge sort algorithm has an efficiency of O(n log n).




    Ver. 1.0                                                               Session 5
Data Structures and Algorithms
Summary (Contd.)


               The best case efficiency of linear search is O(1) and the worst
               case efficiency of linear search is O(n).
               To apply binary search algorithm, you should ensure that the
               list to be searched is sorted.
               The best case efficiency of binary search is O(1) and the
               worst case efficiency of binary search is O(log n).




    Ver. 1.0                                                           Session 5

Contenu connexe

Tendances

Assembler design options
Assembler design optionsAssembler design options
Assembler design optionsMohd Arif
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna universitysangeethajames07
 
Chapter 5 software design
Chapter 5 software designChapter 5 software design
Chapter 5 software designdespicable me
 
Chapter 6 software metrics
Chapter 6 software metricsChapter 6 software metrics
Chapter 6 software metricsdespicable me
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To DotnetSAMIR BHOGAYTA
 
Software engineering Questions and Answers
Software engineering Questions and AnswersSoftware engineering Questions and Answers
Software engineering Questions and AnswersBala Ganesh
 
Software quality assurance
Software quality assuranceSoftware quality assurance
Software quality assuranceAman Adhikari
 
Software Engineering (Short & Long Questions)
Software Engineering (Short & Long Questions)Software Engineering (Short & Long Questions)
Software Engineering (Short & Long Questions)MuhammadTalha436
 
INTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMSINTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMSAshita Agrawal
 
source code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquessource code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquesSiva Priya
 
Principles Of Good Screen Design
Principles Of Good Screen DesignPrinciples Of Good Screen Design
Principles Of Good Screen Designguest7af47
 
01 iec t1_s1_plt_session_01
01 iec t1_s1_plt_session_0101 iec t1_s1_plt_session_01
01 iec t1_s1_plt_session_01Niit Care
 
component based development model
component based development modelcomponent based development model
component based development modelMuneeba Qamar
 
software requirement specification
software requirement specificationsoftware requirement specification
software requirement specificationmaliksiddique1
 
PROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONS
PROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONSPROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONS
PROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONSSatyendra Singh
 

Tendances (20)

Assembler design options
Assembler design optionsAssembler design options
Assembler design options
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
Chapter 5 software design
Chapter 5 software designChapter 5 software design
Chapter 5 software design
 
Chapter 6 software metrics
Chapter 6 software metricsChapter 6 software metrics
Chapter 6 software metrics
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To Dotnet
 
Software engineering Questions and Answers
Software engineering Questions and AnswersSoftware engineering Questions and Answers
Software engineering Questions and Answers
 
Software quality assurance
Software quality assuranceSoftware quality assurance
Software quality assurance
 
Software Engineering (Short & Long Questions)
Software Engineering (Short & Long Questions)Software Engineering (Short & Long Questions)
Software Engineering (Short & Long Questions)
 
.net framework
.net framework.net framework
.net framework
 
INTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMSINTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMS
 
source code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniquessource code metrics and other maintenance tools and techniques
source code metrics and other maintenance tools and techniques
 
Component level design
Component   level designComponent   level design
Component level design
 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
 
Swe notes
Swe notesSwe notes
Swe notes
 
Principles Of Good Screen Design
Principles Of Good Screen DesignPrinciples Of Good Screen Design
Principles Of Good Screen Design
 
01 iec t1_s1_plt_session_01
01 iec t1_s1_plt_session_0101 iec t1_s1_plt_session_01
01 iec t1_s1_plt_session_01
 
component based development model
component based development modelcomponent based development model
component based development model
 
software requirement specification
software requirement specificationsoftware requirement specification
software requirement specification
 
Asp Architecture
Asp ArchitectureAsp Architecture
Asp Architecture
 
PROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONS
PROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONSPROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONS
PROJECT MANAGEMENT - (2016) SEM-VI - PRACTICAL (SLIP) QUESTIONS
 

En vedette

Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10Niit Care
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07Niit Care
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structuresWipro
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and PolynomialAroosa Rajput
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsDr Sandeep Kumar Poonia
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matricesZain Zafar
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Adam Mukharil Bachtiar
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Top10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing WorldTop10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing WorldMahindra Rise
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

En vedette (20)

Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matrices
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
linked list
linked list linked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Top10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing WorldTop10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing World
 
Data Structure
Data StructureData Structure
Data Structure
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Plus de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Dernier

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 

Dernier (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 

04 ds and algorithm session_05

  • 1. Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge sort Search data by using linear search technique Search data by using binary search technique Ver. 1.0 Session 5
  • 2. Data Structures and Algorithms Sorting Data by Using Quick Sort Quick sort algorithm: Is one of the most efficient sorting algorithms Is based on the divide and conquer approach Successively divides the problem into smaller parts until the problems become so small that they can be directly solved Ver. 1.0 Session 5
  • 3. Data Structures and Algorithms Implementing Quick Sort Algorithm In quick sort algorithm, you: Select an element from the list called as pivot. Partition the list into two parts such that: – All the elements towards the left end of the list are smaller than the pivot. – All the elements towards the right end of the list are greater than the pivot. Store the pivot at its correct position between the two parts of the list. You repeat this process for each of the two sublists created after partitioning. This process continues until one element is left in each sublist. Ver. 1.0 Session 5
  • 4. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) To understand the implementation of quick sort algorithm, consider an unsorted list of numbers stored in an array. 0 1 2 3 4 5 6 7 arr 28 55 46 38 16 89 83 30 Ver. 1.0 Session 5
  • 5. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Let us sort this unsorted list. 0 1 2 3 4 5 6 7 arr 28 55 46 38 16 89 83 30 Ver. 1.0 Session 5
  • 6. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Select a Pivot. 0 1 2 3 4 5 6 7 arr 28 55 46 38 16 89 83 30 Pivot Ver. 1.0 Session 5
  • 7. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Start from the left end of the list (at index 1). Move in the left to right direction. Search for the first element that is greater than the pivot value. Greater element 0 1 2 3 4 5 6 7 arr 28 55 46 38 16 89 83 30 Pivot Greater Value Ver. 1.0 Session 5
  • 8. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Start from the right end of the list. Move in the right to left direction. Search for the first element that is smaller than or equal to the pivot value. Smaller element 0 1 2 3 4 5 6 7 arr 28 55 46 38 16 89 83 30 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 9. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Interchange the greater value with smaller value. Swap 0 1 2 3 4 5 6 7 arr 28 16 55 46 38 16 55 89 83 30 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 10. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Continue the search for an element greater than the pivot. Start from arr[2] and move in the left to right direction. Search for the first element that is greater than the pivot value. Greater element 0 1 2 3 4 5 6 7 arr 28 16 46 38 55 89 83 30 Pivot Greater Value Ver. 1.0 Session 5
  • 11. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Continue the search for an element smaller than the pivot. Start from arr[3] and move in the right to left direction. Search for the first element that is smaller than or equal to the pivot value. Smaller element 0 1 2 3 4 5 6 7 arr 28 16 46 38 55 89 83 30 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 12. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) The smaller value is on the left hand side of the greater value. Values remain same. 0 1 2 3 4 5 6 7 arr 28 16 46 38 55 89 83 30 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 13. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) List is now partitioned into two sublists. List 1 contains all values less than or equal to the pivot. List 2 contains all the values greater than the pivot. 0 0 1 1 2 3 4 2 53 6 4 7 5 6 7 arr 28 16 16 46 28 38 55 46 38 55 89 89 83 30 83 30 List 1 List 2 Pivot Ver. 1.0 Session 5
  • 14. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Replace the pivot value with the last element of List 1. The pivot value, 28 is now placed at its correct position in the list. Swap 0 1 2 3 4 5 6 7 arr 28 16 16 28 46 38 55 89 83 30 List 1 List 2 Ver. 1.0 Session 5
  • 15. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Truncate the last element, that is, pivot from List 1. 00 1 2 3 4 5 6 7 arr 16 28 46 38 55 89 83 30 List 1 List 2 Ver. 1.0 Session 5
  • 16. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) List 1 has only one element. Therefore, no sorting required. 0 2 3 4 5 6 7 arr 16 46 38 55 89 83 30 List 1 List 2 Ver. 1.0 Session 5
  • 17. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Sort the second list, List 2. 0 2 3 4 5 6 7 arr 16 46 38 55 89 83 30 List 1 List 2 Ver. 1.0 Session 5
  • 18. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Select a pivot. The pivot in this case will be arr[2], that is, 46. 0 2 3 4 5 6 7 arr 16 46 38 55 89 83 30 List 1 List 2 Pivot Ver. 1.0 Session 5
  • 19. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Start from the left end of the list (at index 3). Move in the left to right direction. Search for the first element that is greater than the pivot value. Greater element 0 2 3 4 5 6 7 arr 16 46 38 55 89 83 30 List 1 List 2 Pivot Greater Value Ver. 1.0 Session 5
  • 20. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Start from the right end of the list (at index 7). Move in the right to left direction. Search for the first element that is smaller than or equal to the pivot value. Smaller element 0 2 3 4 5 6 7 arr 16 46 38 55 89 83 30 List 1 List 2 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 21. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Interchange the greater value with smaller value. Swap 0 2 3 4 5 6 7 arr 16 46 38 30 55 89 83 30 55 List 1 List 2 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 22. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Continue the search for an element greater than the pivot. Start from arr[5] and move in the left to right direction. Search for the first element that is greater than the pivot value. Greater element 0 2 3 4 5 6 7 arr 16 46 38 30 89 83 55 List 1 List 2 Pivot Greater Value Ver. 1.0 Session 5
  • 23. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Continue the search for an element smaller than the pivot. Start from arr[6] and move in the right to left direction. Search for the first element that is smaller than the pivot value. Smaller element 0 2 3 4 5 6 7 arr 16 46 38 30 89 83 55 List 1 List 2 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 24. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) The smaller value is on the left hand side of the greater value. Values remain same. 0 2 3 4 5 6 7 arr 16 46 38 30 89 83 55 List 1 List 2 Pivot Greater Value Smaller Value Ver. 1.0 Session 5
  • 25. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Divide the list into two sublists. Sublist 1 contains all values less than or equal to the pivot. Sublist 2 contains all the values greater than the pivot. 0 1 2 3 4 5 6 7 arr 16 28 46 38 30 89 83 55 Ver. 1.0 Session 5
  • 26. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Replace the pivot value with the last element of Sublist 1. The pivot value, 46 is now placed at its correct position in the list. This process is repeated until all elements reach their correct position. Swap 0 1 2 3 4 5 6 7 arr 16 28 46 30 38 30 46 89 83 55 Sublist 1 Sublist 2 Ver. 1.0 Session 5
  • 27. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) Write an algorithm to implement quick sort: QuickSort(low,high) 1. If (low > high): a. Return 2. Set pivot = arr[low] 3. Set i = low + 1 4. Set j = high 5. Repeat step 6 until i > high or arr[i] > pivot // Search for an // element greater than // pivot 6. Increment i by 1 7. Repeat step 8 until j < low or arr[j] < pivot // Search for an element // smaller than pivot 8. Decrement j by 1 9. If i < j: // If greater element is on the left of smaller element Ver. 1.0 Session 5
  • 28. Data Structures and Algorithms Implementing Quick Sort Algorithm (Contd.) 10. If i <= j: a. Go to step 5 // Continue the search 11. If low < j: a. Swap arr[low] with arr[j] // Swap pivot with last element in // first part of the list 12. QuickSort(low, j – 1) // Apply quicksort on list left to pivot 13. QuickSort(j + 1, high) // Apply quicksort on list right to pivot Ver. 1.0 Session 5
  • 29. Data Structures and Algorithms Determining the Efficiency of Quick Sort Algorithm • The total time taken by this sorting algorithm depends on the position of the pivot value. • The worst case occurs when the list is already sorted. • If the first element is chosen as the pivot, it leads to a worst 2 case efficiency of O(n ). • If you select the median of all values as the pivot, the efficiency would be O(n log n). Ver. 1.0 Session 5
  • 30. Data Structures and Algorithms Just a minute What is the total number of comparisons for an average case in a quick sort algorithm? Answer: O(n log n) Ver. 1.0 Session 5
  • 31. Data Structures and Algorithms Activity: Sorting Data by Using Quick Sort Algorithm Problem Statement: Write a program that stores 10 numbers in an array, and sorts them by using the quick sort algorithm. Ver. 1.0 Session 5
  • 32. Data Structures and Algorithms Sorting Data by Using Merge Sort Merge sort algorithm: Is based on the divide and conquer approach Divides the list into two sublists of sizes as nearly equal as possible Sorts the two sublists separately by using merge sort Merges the sorted sublists into one single list Ver. 1.0 Session 5
  • 33. Data Structures and Algorithms Implementing Merge Sort Algorithm To understand the implementation of merge sort algorithm, consider an unsorted list of numbers stored in an array. 0 1 2 3 4 5 6 arr 53 10 30 76 3 57 24 Ver. 1.0 Session 5
  • 34. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Let us sort this unsorted list. 0 1 2 3 4 5 6 arr 53 10 30 76 3 57 24 Ver. 1.0 Session 5
  • 35. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) The first step to sort data by using merge sort is to split the list into two parts. 0 1 2 3 4 5 6 arr 53 10 30 76 3 57 24 Ver. 1.0 Session 5
  • 36. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) The first step to sort data by using merge sort is to split the list into two parts. 0 1 2 3 4 5 6 arr 53 10 30 76 3 57 24 Ver. 1.0 Session 5
  • 37. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) The list has odd number of elements, therefore, the left sublist is longer than the right sublist by one entry. 0 1 2 3 4 5 6 arr 53 10 30 76 3 57 24 Ver. 1.0 Session 5
  • 38. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Further divide the two sublists into nearly equal parts. 0 1 2 2 3 3 4 4 5 5 6 6 arr 53 10 10 30 76 30 76 3 3 5757 24 24 Ver. 1.0 Session 5
  • 39. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Further divide the sublists. 0 11 22 3 3 4 45 56 6 arr 53 1010 30 30 76 76 3 10 30 76 357 24 57 24 Ver. 1.0 Session 5
  • 40. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) There is a single element left in each sublist. Sublists with one element require no sorting. 0 1 2 3 4 5 6 arr 53 10 30 76 3 57 24 Ver. 1.0 Session 5
  • 41. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Start merging the sublists to obtain a sorted list. 0 11 22 3 3 4 5 4 6 5 6 arr 10 53 5310 3030 76 76 3 57 3 57 24 57 24 Ver. 1.0 Session 5
  • 42. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Further merge the sublists. 0 1 22 3 3 4 4 55 6 6 arr 10 30 53 53 7676 30 3 3 2457 57 57 24 Ver. 1.0 Session 5
  • 43. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Again, merge the sublists. 0 0 1 1 2 2 3 44 55 6 6 arr 10 3 30 10 53 76 24 30 53 5724 7657 3 Ver. 1.0 Session 5
  • 44. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) The list is now sorted. 0 1 2 3 4 5 6 arr 3 10 24 30 53 57 76 Ver. 1.0 Session 5
  • 45. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) Write an algorithm to implement merge sort: MergeSort(low,high) 1. If (low >= high): a. Return 2. Set mid = (low + high)/2 3. Divide the list into two sublists of nearly equal lengths, and sort each sublist by using merge sort. The steps to do this are as follows: a. MergeSort(low, mid b. MergeSort(mid + 1, high) 4. Merge the two sorted sublists: a. Set i = low b. Set j = mid + 1 c. Set k = low d. Repeat until i > mid or j > high: // This loop will terminate when // you reach the end of one of the // two sublists. Ver. 1.0 Session 5
  • 46. Data Structures and Algorithms Implementing Merge Sort Algorithm (Contd.) i. If (arr[i] <= arr[j]) Store arr[i] at index k in array B Increment i by 1 Else Store arr[j] at index k in array B Increment j by 1 ii. Increment k by 1 e. Repeat until j > high: // If there are still some elements in the // second sublist append them to the new list i. Store arr[j] at index k in array B ii. Increment j by 1 iii. Increment k by 1 f. Repeat until i > mid: // If there are still some elements in the // first sublist append them to the new list i. Store arr[i] at index k in array B ii. Increment I by 1 iii. Increment k by 1 5. Copy all elements from the sorted array B into the original array arr Ver. 1.0 Session 5
  • 47. Data Structures and Algorithms Determining the Efficiency of Merge Sort Algorithm To sort the list by using merge sort algorithm, you need to recursively divide the list into two nearly equal sublists until each sublist contains only one element. To divide the list into sublists of size one requires log n passes. In each pass, a maximum of n comparisons are performed. Therefore, the total number of comparisons will be a maximum of n × log n. The efficiency of merge sort is equal to O(n log n) There is no distinction between best, average, and worst case efficiencies of merge sort because all of them require the same amount of time. Ver. 1.0 Session 5
  • 48. Data Structures and Algorithms Just a minute Which algorithm uses the following procedure to sort a given list of elements? 1. Select an element from the list called a pivot. 2. Partition the list into two parts such that one part contains elements lesser than the pivot, and the other part contains elements greater than the pivot. 3. Place the pivot at its correct position between the two lists. 4. Sort the two parts of the list using the same algorithm. Answer: Quick sort Ver. 1.0 Session 5
  • 49. Data Structures and Algorithms Just a minute On which algorithm design technique are quick sort and merge sort based? Answer: Quick sort and merge sort are based on the divide and conquer technique. Ver. 1.0 Session 5
  • 50. Data Structures and Algorithms Performing Linear Search Linear Search: Is the simplest searching method Is also referred to as sequential search Involves comparing the items sequentially with the elements in the list Ver. 1.0 Session 5
  • 51. Data Structures and Algorithms Implementing Linear Search The linear search would begin by comparing the required element with the first element in the list. If the values do not match: The required element is compared with the second element in the list. If the values still do not match: The required element is compared with the third element in the list. This process continues, until: The required element is found or the end of the list is reached. Ver. 1.0 Session 5
  • 52. Data Structures and Algorithms Implementing Linear Search (Contd.) Write an algorithm to search for a given employee ID in a list of employee records by using linear search algorithm: 1. Read the employee ID to be searched 2. Set i = 0 3. Repeat step 4 until i > n or arr[i] = employee ID 4. Increment i by 1 5. If i > n: Display “Not Found” Else Display “Found” Ver. 1.0 Session 5
  • 53. Data Structures and Algorithms Determining the Efficiency of Linear Search The efficiency of a searching algorithm is determined by the running time of the algorithm. In the best case scenario: The element is found at the first position in the list. The number of comparisons in this case is 1. The best case efficiency of linear search is therefore, O(1). In the worst case scenario: The element is found at the last position of the list or does not exists in the list. The number of comparisons in this case is equal to the number of elements. The worst case efficiency of linear search is therefore, O(n). Ver. 1.0 Session 5
  • 54. Data Structures and Algorithms Determining the Efficiency of Linear Search (Contd.) In the average case scenario: The number of comparisons for linear search can be determined by finding the average of the number of comparisons in the best and worst case. The average case efficiency of linear search is 1/2(n + 1). Ver. 1.0 Session 5
  • 55. Data Structures and Algorithms Just a minute You have to apply linear search to search for an element in an array containing 5,000 elements. If, at the end of the search, you find that the element is not present in the array, how many comparisons you would have made to search the required element in the given list? Answer: 5,000 Ver. 1.0 Session 5
  • 56. Data Structures and Algorithms Activity: Performing Linear Search Problem Statement: Write a program to search a given number in an array that contains a maximum of 20 numbers by using the linear search algorithm. If there are more than one occurrences of the element to be searched, then the program should display the position of the first occurrence. The program should also display the total number of comparisons made. Ver. 1.0 Session 5
  • 57. Data Structures and Algorithms Performing Binary Search Binary search algorithm: Is used for searching large lists Searches the element in very few comparisons Can be used only if the list to be searched is sorted Ver. 1.0 Session 5
  • 58. Data Structures and Algorithms Implementing Binary Search Consider an example. You have to search for the name Steve in a telephone directory that is sorted alphabetically. To search the name Steve by using binary search algorithm: – You open the telephone directory at the middle to determine which half contains the name. – Open that half at the middle to determine which quarter of the directory contains the name. – Repeat this process until the name Steve is not found. – Binary search reduces the number of pages to be searched by half each time. Ver. 1.0 Session 5
  • 59. Data Structures and Algorithms Implementing Binary Search (Contd.) Consider a list of 9 elements in a sorted array. 0 1 2 3 4 5 6 7 8 arr 9 13 17 19 25 29 39 40 47 Ver. 1.0 Session 5
  • 60. Data Structures and Algorithms Implementing Binary Search (Contd.) You have to search an element 13 in the given list. 0 1 2 3 4 5 6 7 8 arr 9 13 17 19 25 29 39 40 47 Ver. 1.0 Session 5
  • 61. Data Structures and Algorithms Implementing Binary Search (Contd.) Determine the index of the middlemost element in the list: Mid = (Lower bound + Upper bound)/2 = (0 + 8)/2 =4 0 1 2 3 4 5 6 7 8 arr 9 13 17 19 25 29 39 40 47 Lower bound Middle element Upper bound Ver. 1.0 Session 5
  • 62. Data Structures and Algorithms Implementing Binary Search (Contd.) 13 is not equal to the middle element, therefore, again divide the list into two halves: Mid = (Lower bound + Upper bound)/2 = (0 + 3)/2 =1 0 1 2 3 4 5 6 7 8 arr 9 13 17 19 25 29 39 40 47 Lower bound Upper bound Upper bound Middle element Middle element Ver. 1.0 Session 5
  • 63. Data Structures and Algorithms Implementing Binary Search (Contd.) 13 is equal to middle element. Element found at index 1. 0 1 2 3 4 5 6 7 8 arr 9 13 17 19 25 29 39 40 47 Lower bound Upper bound Element found Ver. 1.0 Session 5
  • 64. Data Structures and Algorithms Implementing Binary Search (Contd.) Write an algorithm to implement binary search algorithm. 1. Accept the element to be searched 2. Set lowerbound = 0 3. Set upperbound = n – 1 4. Set mid = (lowerbound + upperbound)/2 5. If arr[mid] = desired element: a. Display “Found” b. Go to step 10 6. If desired element < arr[mid]: a. Set upperbound = mid – 1 Ver. 1.0 Session 5
  • 65. Data Structures and Algorithms Implementing Binary Search (Contd.) 1. If desired element > arr[mid]: a. Set lowerbound = mid + 1 2. If lowerbound <= upperbound: a. Go to step 4 3. Display “Not Found” 4. Exit Ver. 1.0 Session 5
  • 66. Data Structures and Algorithms Determining the Efficiency of Binary Search In binary search, with every step, the search area is reduced to half. In the best case scenario, the element to be search is found at the middlemost position of the list: The number of comparisons in this case is 1. In the worst case scenario, the element is not found in the list: – After the first bisection, the search space is reduced to n/2 elements, where n is the number of elements in the original list. – After the second bisection, the search space is reduced to n/4 2 elements, that is, n/2 elements. – After ith bisections, the number of comparisons would be n/2i elements. Ver. 1.0 Session 5
  • 67. Data Structures and Algorithms Just a minute In ___________ search algorithm, you begin at one end of the list and scan the list until the desired item is found or the end of the list is reached. Answer: linear Ver. 1.0 Session 5
  • 68. Data Structures and Algorithms Just a minute To implement __________ search algorithm, the list should be sorted. Answer: binary Ver. 1.0 Session 5
  • 69. Data Structures and Algorithms Activity: Performing Binary Search Problem Statement: Write a program to search a number in an array that contains a maximum of 20 elements by using binary search. Assume that the array elements are entered in ascending order. If the number to be searched is present at more than one location in the array, the search should stop when one match is found. The program should also display the total number of comparisons made. Ver. 1.0 Session 5
  • 70. Data Structures and Algorithms Summary In this session, you learned that: Quick sort and merge sort algorithms are based on the divide and conquer technique. To sort a list of items by using the quick sort algorithm, you need to: Select a pivot value. Partition the list into two sublists such that one sublist contains all items less than the pivot, and the second sublist contains all items greater than the pivot. Place the pivot at its correct position between the two sublists. Sort the two sublists by using quick sort. Ver. 1.0 Session 5
  • 71. Data Structures and Algorithms Summary (Contd.) – The total time taken by the quick sort algorithm depends on the position of the pivot value and the initial ordering of elements. 2 – The worst case efficiency of the quick sort algorithm is O(n ). – The best case efficiency of the quick sort algorithm is O(n log n). – To sort a list of items by using merge sort, you need to: – Divide the list into two sublists. – Sort each sublist by using merge sort. – Merge the two sorted sublists. – The merge sort algorithm has an efficiency of O(n log n). Ver. 1.0 Session 5
  • 72. Data Structures and Algorithms Summary (Contd.) The best case efficiency of linear search is O(1) and the worst case efficiency of linear search is O(n). To apply binary search algorithm, you should ensure that the list to be searched is sorted. The best case efficiency of binary search is O(1) and the worst case efficiency of binary search is O(log n). Ver. 1.0 Session 5

Notes de l'éditeur

  1. You can mention to students that in chapter 8, you have studied algorithms that have O(n 2 ) order of growth. These algorithms with O(n 2 ) order of growth are not efficient to sort large lists. In this chapter, students will study the algorithms with O(n log n) order of growth.
  2. Ask students to answer this question and then come to the given example.
  3. Ask students to answer this question and then come to the given example.
  4. Ask students to answer this question and then come to the given example.
  5. Ask students to answer this question and then come to the given example.
  6. Ask students to answer this question and then come to the given example.
  7. Ask students to answer this question and then come to the given example.
  8. Ask students to answer this question and then come to the given example.
  9. Ask students to answer this question and then come to the given example.
  10. Ask students to answer this question and then come to the given example.
  11. Ask students to answer this question and then come to the given example.
  12. Ask students to answer this question and then come to the given example.
  13. Ask students to answer this question and then come to the given example.
  14. Ask students to answer this question and then come to the given example.
  15. Ask students to answer this question and then come to the given example.
  16. Ask students to answer this question and then come to the given example.
  17. Ask students to answer this question and then come to the given example.
  18. Ask students to answer this question and then come to the given example.
  19. Ask students to answer this question and then come to the given example.
  20. Ask students to answer this question and then come to the given example.
  21. Ask students to answer this question and then come to the given example.
  22. Ask students to answer this question and then come to the given example.
  23. Ask students to answer this question and then come to the given example.
  24. Ask students to answer this question and then come to the given example.
  25. Ask students to answer this question and then come to the given example.
  26. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  27. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  28. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  29. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  30. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  31. Ask students to answer this question and then come to the given example.
  32. Ask students to answer this question and then come to the given example.
  33. Ask students to answer this question and then come to the given example.
  34. Ask students to answer this question and then come to the given example.
  35. Ask students to answer this question and then come to the given example.
  36. Ask students to answer this question and then come to the given example.
  37. Ask students to answer this question and then come to the given example.
  38. Ask students to answer this question and then come to the given example.
  39. Ask students to answer this question and then come to the given example.
  40. Ask students to answer this question and then come to the given example.
  41. Ask students to answer this question and then come to the given example.
  42. Ask students to answer this question and then come to the given example.
  43. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  44. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  45. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  46. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  47. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  48. Ask students to answer this question and then come to the given example.
  49. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  50. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  51. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  52. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  53. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  54. Ask students to answer this question and then come to the given example.
  55. Ask students to answer this question and then come to the given example.
  56. Ask students to answer this question and then come to the given example.
  57. Ask students to answer this question and then come to the given example.
  58. Ask students to answer this question and then come to the given example.
  59. Ask students to answer this question and then come to the given example.
  60. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  61. Ask students to write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  62. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  63. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  64. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  65. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.