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


                In this session, you will learn to:
                    Identify the algorithms that can be used to sort data
                    Sort data by using bubble sort
                    Sort data by using selection sort
                    Sort data by using insertion sort
                    Sort data by using shell sort




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Sorting Data


                Suppose, you have to invite your friends and relatives for
                your birthday party. For this, you need to give them a call.
                You are looking for the telephone number of a friend named
                Steve in a telephone directory with 10,000 records.
                However, the records are stored randomly and not in any
                particular order.




     Ver. 1.0                                                         Session 4
Data Structures and Algorithms
Sorting Data (Contd.)


                To search for the telephone number of your friend in such a
                directory, you would need to scan each entry in the
                directory in a sequential manner.
                This would be very time consuming.
                How can you solve this problem?




     Ver. 1.0                                                        Session 4
Data Structures and Algorithms
Sorting Data (Contd.)


                A simple solution to save time, and search records
                efficiently is sorting.
                Sorting is the process of arranging data in some pre-defined
                order or sequence. The order can be either ascending or
                descending.
                If the data is ordered, you can directly go to the section that
                stores the names starting with ‘S’, thereby reducing the
                number of records to be traversed.




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Selecting a Sorting Algorithm


                Sorting is implemented in a program by using an algorithm.
                Some sorting algorithms are:
                     Bubble sort
                     Selection sort
                     Insertion sort
                     Shell sort
                     Merge sort
                     Quick sort
                     Heap sort
                To select an appropriate algorithm, you need to consider the
                following:
                     Execution time
                     Storage space
                     Programming effort

     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Sorting Data by Using Bubble Sort


                Bubble sort algorithm:
                    Is one of the simplest sorting algorithms
                    Has a quadratic order of growth and is therefore suitable for
                    sorting small lists only
                    Works by repeatedly scanning through the list, comparing
                    adjacent elements, and swapping them if they are in the wrong
                    order




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm


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




                        0    1    2   3    4
                arr      5    2   6   7    3




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                      Let us sort this unsorted list.




                         0    1    2     3    4
                arr       5    2   6    7     3




     Ver. 1.0                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Compare the element stored at index 0 with the element
                  stored at index 1.



                      0   1   2    3   4
                arr   5   2   6    7   3




     Ver. 1.0                                                        Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Swap the values if they are not in the correct order.


                      Swap
                      0   1      2   3   4
                arr   2
                      5      5
                             2   6   7   3




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Compare the element stored at index 1 with the element
                  stored at index 2 and swap the values if the value at index 1
                  is greater than the value at index 2.
                          No Change
                      0    1    2     3   4
                arr   2     5   6     7   3




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Compare the element stored at index 2 with the element
                  stored at index 3 and swap the values if the value at index 2
                  is greater than the value at index 3.
                               No Change
                      0   1      2   3     4
                arr   2    5     6   7     3




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Compare the element stored at index 3 with the element
                  stored at index 4 and swap the values if the value at index 3
                  is greater than the value at index 4.
                                     Swap
                      0   1    2    3    4
                arr   2    5   6    3
                                    7    7
                                         3




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Compare the element stored at index 3 with the element
                  stored at index 4 and swap the values if the value at index 3
                  is greater than the value at index 4.


                      0   1    2    3     4
                arr   2    5   6    3     7

                Largest element is placed at its correct position after Pass 1




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 2
                n=5
                  Compare the element stored at index 0 with the element
                  stored at index 1 and swap the values if the value at index 0
                  is greater than the value at index 1.
                      No Change
                       0   1      2   3   4
                arr    2   5      6   3   7




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 2
                n=5
                  Compare the element stored at index 1 with the element
                  stored at index 2 and swap the values if the value at index 1
                  is greater than the value at index 2.
                          No Change
                      0    1    2     3   4
                arr   2     5   6     3   7




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 2
                n=5
                  Compare the element stored at index 2 with the element
                  stored at index 3 and swap the values if the value at index 2
                  is greater than the value at index 3.
                                Swap
                      0   1    2    3    4
                arr   2    5   3
                               6    6
                                    3    7




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 2
                n=5
                  Compare the element stored at index 2 with the element
                  stored at index 3 and swap the values if the value at index 2
                  is greater than the value at index 3.


                      0     1    2     3     4
                arr    2    5    3     6    7

                Second largest element is placed at its correct position after Pass 2




     Ver. 1.0                                                                     Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 3
                n=5
                  Compare the element stored at index 0 with the element
                  stored at index 1 and swap the values if the value at index 0
                  is greater than the value at index 1.
                      No Change
                       0    1     2   3   4
                arr     2   5     3   6   7




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 3
                n=5
                  Compare the element stored at index 1 with the element
                  stored at index 2 and swap the values if the value at index 1
                  is greater than the value at index 2.
                           Swap
                      0   1    2    3    4
                arr   2    5
                           3   5
                               3    6    7




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 3
                n=5
                  Compare the element stored at index 2 with the element
                  stored at index 3 and swap the values if the value at index 2
                  is greater than the value at index 3.


                       0    1     2     3     4
                arr    2     3    5     6     7

                Third largest element is placed at its correct position after Pass 3




     Ver. 1.0                                                                          Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 4
                n=5
                  Compare the element stored at index 0 with the element
                  stored at index 1 and swap the values if the value at index 0
                  is greater than the value at index 1.
                      No Change
                       0    1     2   3   4
                arr     2   3     5   6   7




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 4
                n=5
                  Compare the element stored at index 0 with the element
                  stored at index 1 and swap the values if the value at index 0
                  is greater than the value at index 1.


                      0     1     2     3    4
                arr    2     3 5       6     7

                Fourth largest element is placed at its correct position after Pass 4




     Ver. 1.0                                                                      Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Pass 4
                n=5
                  At the end of Pass 4, the elements are sorted.




                      0   1    2   3    4
                arr   2    3 5     6    7




     Ver. 1.0                                                      Session 4
Data Structures and Algorithms
Implementing Bubble Sort Algorithm (Contd.)


                Write an algorithm to implement bubble sort.
                Algorithm for bubble sort:
                 • Set pass = 1.
                 • Repeat step 3 varying j from 0 to n – 1 – pass.
                 • If the element at index j is greater than the element at index
                   j + 1, swap the two elements.
                 • Increment pass by 1.
                 • If pass <= n – 1, go to step 2.




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Determining the Efficiency of Bubble Sort Algorithm


                • The efficiency of a sorting algorithm is measured in terms of
                  number of comparisons.
                • In bubble sort, there are n – 1 comparisons in Pass 1, n – 2
                  comparisons in Pass 2, and so on.
                • Total number of comparisons = (n – 1) + (n – 2) + (n – 3) +
                  … + 3 + 2 + 1 = n(n – 1)/2.
                • n(n – 1)/2 is of O(n2) order. Therefore, the bubble sort
                  algorithm is of the order O(n2).




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


                What is the order of growth of the bubble sort algorithm?




                Answer:
                    The bubble sort algorithm has a quadratic order of growth.




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


                While implementing bubble sort algorithm, how many
                comparisons will be performed in Pass 1.




                Answer:
                   n – 1 comparisons




     Ver. 1.0                                                        Session 4
Data Structures and Algorithms
Activity: Sorting Data by Using Bubble Sort Algorithm


                Problem Statement:
                   Write a program to store the marks of 10 students in an array.
                   Include a function to sort the elements of the array by using
                   the bubble sort algorithm. After sorting the array, display the
                   sorted list.




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Sorting Data by Using Selection Sort


                Selection sort algorithm:
                    Has a quadratic order of growth and is therefore suitable for
                    sorting small lists only
                    Scans through the list iteratively, selects one item in each
                    scan, and moves the item to its correct position in the list




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm


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




                         0   1    2    3    4
                arr     105 120 10    200 20




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                      Let us sort this unsorted list.




                         0    1    2     3    4
                arr     105 120 10      200 20




     Ver. 1.0                                           Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Search the minimum value in the array, arr[0] to arr[n – 1].




                      0   1    2    3    4
                arr   105 120 10    200 20


                              min




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Search the minimum value in the array, arr[0] to arr[n – 1].
                  Swap the minimum value with the value at index 0.

                          Swap
                      0    1     2     3   4
                arr   105 120 105 200 20
                      10      10


                                 min




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 1
                n=5
                  Search the minimum value in the array, arr[0] to arr[n – 1].
                  Swap the minimum value with the value at index 0.



                       0     1    2     3     4
                arr   10 120 105 200 20


                The smallest value is placed at its correct location after Pass 1




     Ver. 1.0                                                                       Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 2
                n=5
                  Search the minimum value in the array, arr[1] to arr[n – 1].
                  Swap the minimum value with the value at index 1.

                               Swap
                      0   1    2    3    4
                arr   10 120 105 200 120
                          20          20


                                        min




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 2
                n=5
                  Search the minimum value in the array, arr[1] to arr[n – 1].
                  Swap the minimum value with the value at index 1.



                       0    1     2     3    4
                arr   10 120 105 200 120
                          20          20


                The second smallest value is placed at its correct location after
                Pass 2




     Ver. 1.0                                                                       Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 3
                n=5
                  Search the minimum value in the array, arr[2] to arr[n – 1].
                  Swap the minimum value with the value at index 2.



                      0    1   2     3   4
                arr   10 120 105 200 120
                          20          20


                               min




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 3
                n=5
                  Search the minimum value in the array, arr[2] to arr[n – 1].
                  Swap the minimum value with the value at index 2.



                      0     1     2     3     4
                arr   10 120 105 200 120
                          20          20


                                 min
                The third smallest value is placed at its correct location after Pass 3




     Ver. 1.0                                                                        Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 4
                n=5
                  Search the minimum value in the array, arr[3] to arr[n – 1].
                  Swap the minimum value with the value at index 3.

                                    Swap
                      0   1    2    3    4
                arr   10 120 105 120 120
                          20     200 200
                                      20


                                         min




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 4
                n=5
                  Search the minimum value in the array, arr[3] to arr[n – 1].
                  Swap the minimum value with the value at index 3.



                       0     1    2     3     4
                arr   10 120 105 120 200
                          20     200 20


                The fourth smallest value is placed at its correct location after Pass 4




     Ver. 1.0                                                                        Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Pass 4
                n=5
                  The list is now sorted.




                      0   1    2    3       4
                arr   10 120 105 120 200
                          20     200 20




     Ver. 1.0                                    Session 4
Data Structures and Algorithms
Implementing Selection Sort Algorithm (Contd.)


                Write an algorithm to implement selection sort.
                Algorithm for selection sort:
                 1. Repeat steps 2 and 3 varying j from 0 to n – 2
                 2. Find the minimum value in arr[j] to arr[n – 1]:
                     a. Set min_index = j
                     b. Repeat step c varying i from j + 1 to n – 1
                     c. If arr[i] < arr[min_index]:
                          i. min_index = i
                 3. Swap arr[j] with arr[min_index]




     Ver. 1.0                                                         Session 4
Data Structures and Algorithms
Determining the Efficiency of Selection Sort Algorithm


                • In selection sort, there are n – 1 comparisons during Pass 1
                  to find the smallest element, n – 2 comparisons during Pass
                  2 to find the second smallest element, and so on.
                • Total number of comparisons = (n – 1) + (n – 2) + (n – 3) +
                  … + 3 + 2 + 1 = n(n – 1)/2
                • n(n – 1)/2 is of O(n2) order. Therefore, the selection sort
                  algorithm is of the order O(n2).




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


                Read the following statement and specify whether it is true
                or false:
                    The efficiency of selection sort algorithm is same as that of the
                    bubble sort algorithm.




                Answer:
                    True




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


                How many comparisons are performed in the second pass
                of the selection sort algorithm?




                Answer:
                   n–2




     Ver. 1.0                                                   Session 4
Data Structures and Algorithms
Sorting Data by Using Insertion Sort


                Insertion sort algorithm:
                    Has a quadratic order of growth and is therefore suitable for
                    sorting small lists only
                    Is much more efficient than bubble sort, and selection sort, if
                    the list that needs to be sorted is nearly sorted




     Ver. 1.0                                                                 Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm


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




                              0    1    2    3   4
                        arr   70   80 30    10   20




     Ver. 1.0                                                         Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                To sort this list by using the insertion sort algorithm:
                    You need to divide the list into two sublists, sorted and
                    unsorted.




                                0    1     2    3     4
                         arr    70   80 30      10    20




     Ver. 1.0                                                                   Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                To sort this list by using the insertion sort algorithm:
                    You need to divide the list into two sublists, sorted and
                    unsorted.
                    Initially, the sorted list has the first element and the unsorted
                    list has the remaining 4 elements.


                            0                   1      2     3      4
                            70                 80 30        10      20




                         Sorted List                Unsorted List




     Ver. 1.0                                                                  Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 1
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.




                               0                1      2     3      4
                               70              80 30        10      20




                            Sorted List             Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 1
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.



                            0     1               2    3     4
                            70    80              30   10 20




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 2
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.



                            0     1               2    3     4
                            70    80              30   10 20




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 2
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.




                       30   70 80                  10 20




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 3
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.




                       30   70 80                  10 20




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 3
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.



                  0    1    2    3                     4
                  10   30   70 80                      20




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 4
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.



                  0    1    2    3                     4
                  10   30   70 80                      20




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                Pass 4
                  Place the first element from the unsorted list at its correct
                  position in the sorted list.




                  0    1    2     3       4
                  10   20 30     70       80




                            Sorted List           Unsorted List




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      Let us now write an algorithm to      1.   Repeat steps 2, 3, 4, and 5
                                                                 varying i from 1 to n – 1
                      implement insertion sort algorithm.
                                                            3.   Set temp = arr[i]

                                                            5.   Set j = i – 1

                                                            7.   Repeat until j becomes less
                                                                 than 0 or arr[j] becomes less
                                                                 than or equal to temp:
                         0    1   2    3    4                      a. Shift the value at index
                                                                       j to index j + 1
                arr      70   80 30   10    20                     b. Decrement j by 1

                                                            9.   Store temp at index j + 1




     Ver. 1.0                                                                        Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =1
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    70    80 30   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =1
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    70    80 30   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1

                             i




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =1
                                                •    Set temp = arr[i]
                      temp = 80
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    70   80 30     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                            i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =1
                                                •    Set temp = arr[i]
                      temp = 80
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    70   80 30     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                        j   i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =1
                                                •    Set temp = arr[i]
                      temp = 80
                                                •    Set j = i – 1
                      arr[j] < temp
                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                        0    1    2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr     70   80 30    10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                         j   i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                             1.   Repeat steps 2, 3, 4, and 5
                                                           varying i from 1 to n – 1
                      i =1
                                                      3.   Set temp = arr[i]
                      temp = 80
                                                      5.   Set j = i – 1
                      arr[j] < temp
                                                      7.   Repeat until j becomes less
                                                           than 0 or arr[j] becomes less
                                                           than or equal to temp:
                        0    1    2     3     4              a. Shift the value at index
                                                                 j to index j + 1
                arr     70   80 30     10     20             b. Decrement j by 1

                                                      •    Store temp at index j + 1

                         j   i
                Value temp is stored at its correct
                position in the sorted list


     Ver. 1.0                                                                  Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =1
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    70    80 30   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =2
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    70    80 30   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =2
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    70    80 30   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1

                                 i




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =2
                                                •    Set temp = arr[i]
                      temp = 30
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    70   80 30     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                                  i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =2
                                                •    Set temp = arr[i]
                      temp = 30
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    70   80 30     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                            j     i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =2
                                                •    Set temp = arr[i]
                      temp = 30
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    70   80 30     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                            j     i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       1.   Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =2
                                                3.   Set temp = arr[i]
                      temp = 30
                                                5.   Set j = i – 1

                                                7.   Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           •   Shift the value at index
                                                           j to index j + 1
                arr    70   80 30
                               80     10   20          •   Decrement j by 1

                                                9.   Store temp at index j + 1

                            j     i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                         1.   Repeat steps 2, 3, 4, and 5
                                                       varying i from 1 to n – 1
                      i =2
                                                  3.   Set temp = arr[i]
                      temp = 30
                                                  5.   Set j = i – 1

                                                  7.   Repeat until j becomes less
                                                       than 0 or arr[j] becomes less
                                                       than or equal to temp:
                       0       1   2    3    4           •   Shift the value at index
                                                             j to index j + 1
                arr    70          80   10   20          •   Decrement j by 1

                                                  9.   Store temp at index j + 1

                           j   j    i




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =2
                                                 3.   Set temp = arr[i]
                      temp = 30
                                                 5.   Set j = i – 1

                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0       1   2   3    4           •   Shift the value at index
                                                            j to index j + 1
                arr    70      70 80   10   20          •   Decrement j by 1

                                                 9.   Store temp at index j + 1

                           j       i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =2
                                                 3.   Set temp = arr[i]
                      temp = 30
                                                 5.   Set j = i – 1
                      j = –1
                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0       1   2   3    4           •   Shift the value at index
                                                            j to index j + 1
                arr            70 80   10   20          •   Decrement j by 1

                                                 •    Store temp at index j + 1

                           j       i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       1.   Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =2
                                                3.   Set temp = arr[i]
                      temp = 30
                                                5.   Set j = i – 1
                      j = –1
                                                7.   Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    30   70 80     10   20          b. Decrement j by 1

                                                •    Store temp at index j + 1

                                  i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =2
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    30    70 80   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                      •    Repeat steps 2, 3, 4, and 5
                                                    varying i from 1 to n – 1
                      i =3
                                               •    Set temp = arr[i]

                                               •    Set j = i – 1

                                               •    Repeat until j becomes less
                                                    than 0 or arr[j] becomes less
                                                    than or equal to temp:
                       0     1   2   3    4           a. Shift the value at index
                                                          j to index j + 1
                arr    30    70 80   10   20          b. Decrement j by 1

                                               9.   Store temp at index j + 1

                                      i




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =3
                                                •    Set temp = arr[i]
                      temp = 10
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    30   70 80     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                                       i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =3
                                                •    Set temp = arr[i]
                      temp = 10
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    30   70 80     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                                  j    i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       •    Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =3
                                                •    Set temp = arr[i]
                      temp = 10
                                                •    Set j = i – 1

                                                •    Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           a. Shift the value at index
                                                           j to index j + 1
                arr    30   70 80     10   20          b. Decrement j by 1

                                                9.   Store temp at index j + 1

                                  j    i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       1.   Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =3
                                                3.   Set temp = arr[i]
                      temp = 10
                                                5.   Set j = i – 1

                                                7.   Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           •   Shift the value at index
                                                           j to index j + 1
                arr    30   70 80     80
                                      10   20          •   Decrement j by 1

                                                9.   Store temp at index j + 1

                                  j    i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                       1.   Repeat steps 2, 3, 4, and 5
                                                     varying i from 1 to n – 1
                      i =3
                                                3.   Set temp = arr[i]
                      temp = 10
                                                5.   Set j = i – 1

                                                7.   Repeat until j becomes less
                                                     than 0 or arr[j] becomes less
                                                     than or equal to temp:
                       0    1     2   3    4           •   Shift the value at index
                                                           j to index j + 1
                arr    30   70        80   20          •   Decrement j by 1

                                                •    Store temp at index j + 1

                            j     j    i




     Ver. 1.0                                                            Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =3
                                                 3.   Set temp = arr[i]
                      temp = 10
                                                 5.   Set j = i – 1

                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0    1     2    3    4           •   Shift the value at index
                                                            j to index j + 1
                arr    30   70    70   80   20          •   Decrement j by 1

                                                 9.   Store temp at index j + 1

                            j           i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =3
                                                 3.   Set temp = arr[i]
                      temp = 10
                                                 5.   Set j = i – 1

                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0    1     2    3    4           •   Shift the value at index
                                                            j to index j + 1
                arr    30         70   80   20          •   Decrement j by 1

                                                 •    Store temp at index j + 1

                        j   j           i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =3
                                                 3.   Set temp = arr[i]
                      temp = 10
                                                 5.   Set j = i – 1

                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0    1     2    3    4           •   Shift the value at index
                                                            j to index j + 1
                arr    30   30    70   80   20          •   Decrement j by 1

                                                 9.   Store temp at index j + 1

                        j               i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =3
                                                 3.   Set temp = arr[i]
                      temp = 10
                                                 5.   Set j = i – 1
                      j = –1
                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0    1     2    3    4           •   Shift the value at index
                                                            j to index j + 1
                arr         30    70   80   20          •   Decrement j by 1

                                                 •    Store temp at index j + 1

                        j               i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        1.   Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =3
                                                 3.   Set temp = arr[i]
                      temp = 10
                                                 5.   Set j = i – 1
                      j = –1
                                                 7.   Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0    1     2    3    4           a. Shift the value at index
                                                            j to index j + 1
                arr    10   30    70   80   20          b. Decrement j by 1

                                                 •    Store temp at index j + 1

                                        i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                        •    Repeat steps 2, 3, 4, and 5
                                                      varying i from 1 to n – 1
                      i =3
                                                 •    Set temp = arr[i]

                                                 •    Set j = i – 1

                                                 •    Repeat until j becomes less
                                                      than 0 or arr[j] becomes less
                                                      than or equal to temp:
                       0     1    2    3    4           a. Shift the value at index
                                                            j to index j + 1
                arr    10    30   70   80   20          b. Decrement j by 1

                                                 9.   Store temp at index j + 1

                                        i




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           •    Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    •    Set temp = arr[i]

                                                    •    Set j = i – 1

                                                    •    Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0     1    2    3    4              a. Shift the value at index
                                                               j to index j + 1
                arr    10    30   70   80   20             b. Decrement j by 1

                                                    9.   Store temp at index j + 1

                                        i       i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           •    Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    •    Set temp = arr[i]
                      temp = 20
                                                    •    Set j = i – 1

                                                    •    Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              a. Shift the value at index
                                                               j to index j + 1
                arr    10   30    70   80   20             b. Decrement j by 1

                                                    9.   Store temp at index j + 1

                                                i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           •    Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    •    Set temp = arr[i]
                      temp = 20
                                                    •    Set j = i – 1

                                                    •    Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              a. Shift the value at index
                                                               j to index j + 1
                arr    10   30    70   80   20             b. Decrement j by 1

                                                    9.   Store temp at index j + 1

                                        j       i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           •    Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    •    Set temp = arr[i]
                      temp = 20
                                                    •    Set j = i – 1

                                                    •    Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              a. Shift the value at index
                                                               j to index j + 1
                arr    10   30    70   80   20             b. Decrement j by 1

                                                    9.   Store temp at index j + 1

                                        j       i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           1.   Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    3.   Set temp = arr[i]
                      temp = 20
                                                    5.   Set j = i – 1

                                                    7.   Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              •   Shift the value at index
                                                               j to index j + 1
                arr    10   30    70   80   80
                                            20             •   Decrement j by 1

                                                    9.   Store temp at index j + 1

                                        j       i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                          1.   Repeat steps 2, 3, 4, and 5
                                                        varying i from 1 to n – 1
                      i =4
                                                   3.   Set temp = arr[i]
                      temp = 20
                                                   5.   Set j = i – 1

                                                   7.   Repeat until j becomes less
                                                        than 0 or arr[j] becomes less
                                                        than or equal to temp:
                       0    1     2    3   4              •   Shift the value at index
                                                              j to index j + 1
                arr    10   30    70       80             •   Decrement j by 1

                                                   •    Store temp at index j + 1

                                   j   j       i




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           1.   Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    3.   Set temp = arr[i]
                      temp = 20
                                                    5.   Set j = i – 1

                                                    7.   Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              •   Shift the value at index
                                                               j to index j + 1
                arr    10   30    70   70   80             •   Decrement j by 1

                                                    9.   Store temp at index j + 1

                                   j            i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                          1.   Repeat steps 2, 3, 4, and 5
                                                        varying i from 1 to n – 1
                      i =4
                                                   3.   Set temp = arr[i]
                      temp = 20
                                                   5.   Set j = i – 1

                                                   7.   Repeat until j becomes less
                                                        than 0 or arr[j] becomes less
                                                        than or equal to temp:
                       0    1     2   3    4              •   Shift the value at index
                                                              j to index j + 1
                arr    10   30        70   80             •   Decrement j by 1

                                                   •    Store temp at index j + 1

                             j    j            i




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           1.   Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    3.   Set temp = arr[i]
                      temp = 20
                                                    5.   Set j = i – 1

                                                    7.   Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              •   Shift the value at index
                                                               j to index j + 1
                arr    10   30    30   70   80             •   Decrement j by 1

                                                    9.   Store temp at index j + 1

                             j                  i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           1.   Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    3.   Set temp = arr[i]
                      temp = 20
                                                    5.   Set j = i – 1

                                                    7.   Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              •   Shift the value at index
                                                               j to index j + 1
                arr    10         30   70   80             •   Decrement j by 1

                                                    •    Store temp at index j + 1

                        j   j                   i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                           1.   Repeat steps 2, 3, 4, and 5
                                                         varying i from 1 to n – 1
                      i =4
                                                    3.   Set temp = arr[i]
                      temp = 20
                                                    5.   Set j = i – 1

                                                    7.   Repeat until j becomes less
                                                         than 0 or arr[j] becomes less
                                                         than or equal to temp:
                       0    1     2    3    4              a. Shift the value at index
                                                               j to index j + 1
                arr    10   20    30   70   80             b. Decrement j by 1

                                                    •    Store temp at index j + 1

                        j                       i




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Insertion Sort Algorithm (Contd.)


                      n=5                            1.   Repeat steps 2, 3, 4, and 5
                                                          varying i from 1 to n – 1
                      i =4
                                                     3.   Set temp = arr[i]

                                                     5.   Set j = i – 1

                                                     7.   Repeat until j becomes less
                                                          than 0 or arr[j] becomes less
                                                          than or equal to temp:
                       0     1    2     3    4              a. Shift the value at index
                                                                j to index j + 1
                arr    10    20   30    70   80             b. Decrement j by 1

                                                     9.   Store temp at index j + 1

                        j                        i
                        The list is now sorted




     Ver. 1.0                                                                 Session 4
Data Structures and Algorithms
Determining the Efficiency of Insertion Sort Algorithm


                To sort a list of size n by using insertion sort, you need to
                perform (n – 1) passes.
                Best Case Efficiency:
                   Best case occurs when the list is already sorted.
                   In this case, you will have to make only one comparison in
                   each pass.
                   In n – 1 passes, you will need to make n – 1 comparisons.
                   The best case efficiency of insertion sort is of the order O(n).
                Worst Case Efficiency:
                 – Worst case occurs when the list is sorted in the reverse order.
                 – In this case, you need to perform one comparison in the first
                   pass, two comparisons in the second pass, three comparisons
                   in the third pass, and n – 1 comparisons in the (n – 1)th pass.
                 – The worst case efficiency of insertion sort is of the order O(n2).


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


                A sales manager has to do a research on best seller cold
                drinks in the market for the year 2004-2006. David, the
                software developer, has a list of all the cold drink brands
                along with their sales figures stored in a file. David has to
                provide the sorted data to the sales manager. The data in
                the file is more or less sorted. Which sorting algorithm will
                be most efficient for sorting this data and why?


                Answer:
                    Insertion sort provides better efficiency than bubble sort and
                    selection sort when the list is partially sorted. Therefore, David
                    should use the insertion sort algorithm.




     Ver. 1.0                                                                 Session 4
Data Structures and Algorithms
Sorting Data by Using Shell Sort


                Shell sort algorithm:
                    Insertion sort is an efficient algorithm only if the list is already
                    partially sorted and results in an inefficient solution in an
                    average case.
                    To overcome this limitation, a computer scientist, D.L. Shell
                    proposed an improvement over the insertion sort algorithm.
                    The new algorithm was called shell sort after the name of its
                    proposer.




     Ver. 1.0                                                                     Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm


                Shell sort algorithm:
                    Improves insertion sort by comparing the elements separated
                    by a distance of several positions to form multiple sublists
                    Applies insertion sort on each sublist to move the elements
                    towards their correct positions
                    Helps an element to take a bigger step towards its correct
                    position, thereby reducing the number of comparisons




     Ver. 1.0                                                             Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)


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




                0    1   2   3    4    5    6    7     8    9    10
        arr     70   30 40   10   80   20   90   110   75   60   45




     Ver. 1.0                                                         Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)


                To apply shell sort on this array, you need to:
                     Select the distance by which the elements in a group will be
                     separated to form multiple sublists.
                     Apply insertion sort on each sublist to move the elements
                     towards their correct positions.



                0    1    2    3    4     5     6    7     8    9    10
        arr     70   30 40    10    80    20   90   110   75    60   45




     Ver. 1.0                                                               Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                Increment = 3
                       0    3    6   9                    1    4    7    10
          List 1 =   70 10 90        60        List 2 =   30   80 110 45
                Pass = 1


                       2    5    8
            List 3 =   40   20 75


                 0     1    2   3    4    5    6     7    8    9    10
        arr      70    30 40    10   80   20   90   110   75   60   45




     Ver. 1.0                                                              Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                       0      3    6     9                     1    4   7   10
          List 1 =     10
                       70     60 70
                              10 90     90
                                        60          List 2 =   30   80 110 45
                                                                    45 80 110



                       2      5    8
            List 3 =   20
                       40    40 75
                             20



                           Apply insertion sort to sort the
                           The lists are sorted
                           three lists




     Ver. 1.0                                                                Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                       0    3    6    9                      1       4    7    10
          List 1 =     10   60 70     90         List 2 =    30      45   80   110



                       2    5    8
            List 3 =   20   40 75


            0     1    2    3    4     5    6     7     8        9   10
   arr      10    30 20     60   45    40   70    80    75       90 110




     Ver. 1.0                                                                    Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                       0   2     4    6     8       10
                   Increment = 2
            List 1 = 10 2 20 45
                   Pass =             70    75 110



                       1    3    5    7     9
            List 2 =   30   60 40     80    90


            0     1    2    3    4     5        6    7    8    9   10
   arr      10    30 20     60   45    40    70      80   75   90 110




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                       0    2    4    6     8    10
            List 1 =   10   20 45     70   75 110



                       1    3    5    7     9
            List 2 =   30   60 40     80   90




                       Apply insertion sort on each sublist




     Ver. 1.0                                                 Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                       0    2   4     6    8   10
            List 1 =   10   20 45    70   75 110



                       1    3   5     7    9
            List 2 =   30   40 60    80   90




                            The lists are now sorted




     Ver. 1.0                                          Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)

                       0    2    4    6     8       10
            List 1 =   10   20 45     70    75 110



                       1    3    5    7     9
            List 2 =   30   40 60     80    90


            0     1    2    3    4     5        6    7    8    9   10
   arr      10    30 20     40   45    60    70      80   75   90 110




     Ver. 1.0                                                           Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)


                 Increment = 1
                 Pass = 3




            0    1   2    3    4      5     6     7      8   9   10
   arr      10   30 20   40    45     60   70     80    75   90 110



                     Apply insertion sort to sort the list




     Ver. 1.0                                                         Session 4
Data Structures and Algorithms
Implementing Shell Sort Algorithm (Contd.)


                 Increment = 1
                 Pass = 3




            0    1   2   3      4      5    6     7   8    9   10
   arr      10   20 30   40     45    60    70   75   80   90 110



                             The list is now sorted




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


                Which of the following sorting algorithms compares the
                elements separated by a distance of several positions to
                sort the data? The options are:
                1.   Insertion sort
                2.   Selection sort
                3.   Bubble sort
                4.   Shell sort




                Answer:
                4. Shell sort




     Ver. 1.0                                                        Session 4
Data Structures and Algorithms
Summary


               In this session, you learned that:
                  Sorting is a process of arranging data in some pre-defined
                  order of a key value. The order can be either ascending or
                  descending.
                  There are various sorting algorithms that are used to sort data.
                  Some of them are:
                      Bubble sort
                      Selection sort
                      Insertion sort
                      Shell sort
                      Merge sort
                      Quick sort
                      Heap sort




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


                 To select an appropriate algorithm, you need to consider the
                 following:
                   – Execution time
                   – Storage space
                   – Programming effort
               – Bubble sort and selection algorithms have a quadratic order of
                 growth, and are therefore suitable for sorting small lists only.
               – Insertion sort performs different number of comparisons
                 depending on the initial ordering of elements. When the
                 elements are already in the sorted order, insertion sort needs
                 to make very few comparisons.
               – Insertion sort is an efficient algorithm than bubble sort and
                 selection sort if the list that needs to be sorted is nearly sorted.




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


               Shell sort improves insertion sort by comparing the elements
               separated by a distance of several positions. This helps an
               element to take a bigger step towards its correct position,
               thereby reducing the number of comparisons.




    Ver. 1.0                                                          Session 4

Contenu connexe

Tendances

Python an-intro v2
Python an-intro v2Python an-intro v2
Python an-intro v2
Arulalan T
 
Application of greedy method
Application  of  greedy methodApplication  of  greedy method
Application of greedy method
Tech_MX
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
myrajendra
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
myrajendra
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
irdginfo
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
koolkampus
 

Tendances (20)

Difference between group by and order by in sql server
Difference between group by and  order by in sql serverDifference between group by and  order by in sql server
Difference between group by and order by in sql server
 
Python an-intro v2
Python an-intro v2Python an-intro v2
Python an-intro v2
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Inventory receiving processes for serial controlled items
Inventory receiving processes for serial controlled itemsInventory receiving processes for serial controlled items
Inventory receiving processes for serial controlled items
 
TrackBar and TreeView
TrackBar and TreeViewTrackBar and TreeView
TrackBar and TreeView
 
Conditional formatting - Excel
Conditional formatting - ExcelConditional formatting - Excel
Conditional formatting - Excel
 
Database Connection
Database ConnectionDatabase Connection
Database Connection
 
Oops Quiz
Oops QuizOops Quiz
Oops Quiz
 
linked list
linked listlinked list
linked list
 
Regular expression to NFA (Nondeterministic Finite Automata)
Regular expression to NFA (Nondeterministic Finite Automata)Regular expression to NFA (Nondeterministic Finite Automata)
Regular expression to NFA (Nondeterministic Finite Automata)
 
Application of greedy method
Application  of  greedy methodApplication  of  greedy method
Application of greedy method
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Java applet
Java appletJava applet
Java applet
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
Conditional formatting in excel v2
Conditional formatting in excel v2Conditional formatting in excel v2
Conditional formatting in excel v2
 
12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 

Similaire à 03 ds and algorithm session_04

Algoithems and data structures
Algoithems and data structuresAlgoithems and data structures
Algoithems and data structures
adamlongs1983
 
ds and algorithm session
ds and algorithm sessionds and algorithm session
ds and algorithm session
Varun Garg
 
08 ds and algorithm session_11
08 ds and algorithm session_1108 ds and algorithm session_11
08 ds and algorithm session_11
Niit Care
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
trangiaphuc362003181
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...
Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...
Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...
Waqas Tariq
 

Similaire à 03 ds and algorithm session_04 (20)

Data structures
Data structuresData structures
Data structures
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptxKripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
 
Ds
DsDs
Ds
 
Ad
AdAd
Ad
 
Algoithems and data structures
Algoithems and data structuresAlgoithems and data structures
Algoithems and data structures
 
ds and algorithm session
ds and algorithm sessionds and algorithm session
ds and algorithm session
 
08 ds and algorithm session_11
08 ds and algorithm session_1108 ds and algorithm session_11
08 ds and algorithm session_11
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
6-Sorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrti...
 
Improved Frequent Pattern Mining Algorithm using Divide and Conquer Technique...
Improved Frequent Pattern Mining Algorithm using Divide and Conquer Technique...Improved Frequent Pattern Mining Algorithm using Divide and Conquer Technique...
Improved Frequent Pattern Mining Algorithm using Divide and Conquer Technique...
 
The awesome algorithm
The awesome algorithmThe awesome algorithm
The awesome algorithm
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
Unit 4 data storage and querying
Unit 4   data storage and queryingUnit 4   data storage and querying
Unit 4 data storage and querying
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
Data-Structure-Algorithms-.docx
Data-Structure-Algorithms-.docxData-Structure-Algorithms-.docx
Data-Structure-Algorithms-.docx
 
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERINGCOMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
 
Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...
Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...
Reconstruction of a Complete Dataset from an Incomplete Dataset by ARA (Attri...
 
Modern Java - WeAreDevelopers - Berlin - 2023 - Ron Veen.pdf
Modern Java - WeAreDevelopers - Berlin - 2023  - Ron Veen.pdfModern Java - WeAreDevelopers - Berlin - 2023  - Ron Veen.pdf
Modern Java - WeAreDevelopers - Berlin - 2023 - Ron Veen.pdf
 

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

Dernier (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

03 ds and algorithm session_04

  • 1. Data Structures and Algorithms Objectives In this session, you will learn to: Identify the algorithms that can be used to sort data Sort data by using bubble sort Sort data by using selection sort Sort data by using insertion sort Sort data by using shell sort Ver. 1.0 Session 4
  • 2. Data Structures and Algorithms Sorting Data Suppose, you have to invite your friends and relatives for your birthday party. For this, you need to give them a call. You are looking for the telephone number of a friend named Steve in a telephone directory with 10,000 records. However, the records are stored randomly and not in any particular order. Ver. 1.0 Session 4
  • 3. Data Structures and Algorithms Sorting Data (Contd.) To search for the telephone number of your friend in such a directory, you would need to scan each entry in the directory in a sequential manner. This would be very time consuming. How can you solve this problem? Ver. 1.0 Session 4
  • 4. Data Structures and Algorithms Sorting Data (Contd.) A simple solution to save time, and search records efficiently is sorting. Sorting is the process of arranging data in some pre-defined order or sequence. The order can be either ascending or descending. If the data is ordered, you can directly go to the section that stores the names starting with ‘S’, thereby reducing the number of records to be traversed. Ver. 1.0 Session 4
  • 5. Data Structures and Algorithms Selecting a Sorting Algorithm Sorting is implemented in a program by using an algorithm. Some sorting algorithms are: Bubble sort Selection sort Insertion sort Shell sort Merge sort Quick sort Heap sort To select an appropriate algorithm, you need to consider the following: Execution time Storage space Programming effort Ver. 1.0 Session 4
  • 6. Data Structures and Algorithms Sorting Data by Using Bubble Sort Bubble sort algorithm: Is one of the simplest sorting algorithms Has a quadratic order of growth and is therefore suitable for sorting small lists only Works by repeatedly scanning through the list, comparing adjacent elements, and swapping them if they are in the wrong order Ver. 1.0 Session 4
  • 7. Data Structures and Algorithms Implementing Bubble Sort Algorithm To understand the implementation of bubble sort algorithm, consider an unsorted list of numbers stored in an array. 0 1 2 3 4 arr 5 2 6 7 3 Ver. 1.0 Session 4
  • 8. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Let us sort this unsorted list. 0 1 2 3 4 arr 5 2 6 7 3 Ver. 1.0 Session 4
  • 9. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 1 n=5 Compare the element stored at index 0 with the element stored at index 1. 0 1 2 3 4 arr 5 2 6 7 3 Ver. 1.0 Session 4
  • 10. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 1 n=5 Swap the values if they are not in the correct order. Swap 0 1 2 3 4 arr 2 5 5 2 6 7 3 Ver. 1.0 Session 4
  • 11. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 1 n=5 Compare the element stored at index 1 with the element stored at index 2 and swap the values if the value at index 1 is greater than the value at index 2. No Change 0 1 2 3 4 arr 2 5 6 7 3 Ver. 1.0 Session 4
  • 12. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 1 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3. No Change 0 1 2 3 4 arr 2 5 6 7 3 Ver. 1.0 Session 4
  • 13. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 1 n=5 Compare the element stored at index 3 with the element stored at index 4 and swap the values if the value at index 3 is greater than the value at index 4. Swap 0 1 2 3 4 arr 2 5 6 3 7 7 3 Ver. 1.0 Session 4
  • 14. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 1 n=5 Compare the element stored at index 3 with the element stored at index 4 and swap the values if the value at index 3 is greater than the value at index 4. 0 1 2 3 4 arr 2 5 6 3 7 Largest element is placed at its correct position after Pass 1 Ver. 1.0 Session 4
  • 15. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 2 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1. No Change 0 1 2 3 4 arr 2 5 6 3 7 Ver. 1.0 Session 4
  • 16. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 2 n=5 Compare the element stored at index 1 with the element stored at index 2 and swap the values if the value at index 1 is greater than the value at index 2. No Change 0 1 2 3 4 arr 2 5 6 3 7 Ver. 1.0 Session 4
  • 17. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 2 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3. Swap 0 1 2 3 4 arr 2 5 3 6 6 3 7 Ver. 1.0 Session 4
  • 18. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 2 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3. 0 1 2 3 4 arr 2 5 3 6 7 Second largest element is placed at its correct position after Pass 2 Ver. 1.0 Session 4
  • 19. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 3 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1. No Change 0 1 2 3 4 arr 2 5 3 6 7 Ver. 1.0 Session 4
  • 20. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 3 n=5 Compare the element stored at index 1 with the element stored at index 2 and swap the values if the value at index 1 is greater than the value at index 2. Swap 0 1 2 3 4 arr 2 5 3 5 3 6 7 Ver. 1.0 Session 4
  • 21. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 3 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3. 0 1 2 3 4 arr 2 3 5 6 7 Third largest element is placed at its correct position after Pass 3 Ver. 1.0 Session 4
  • 22. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 4 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1. No Change 0 1 2 3 4 arr 2 3 5 6 7 Ver. 1.0 Session 4
  • 23. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 4 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1. 0 1 2 3 4 arr 2 3 5 6 7 Fourth largest element is placed at its correct position after Pass 4 Ver. 1.0 Session 4
  • 24. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Pass 4 n=5 At the end of Pass 4, the elements are sorted. 0 1 2 3 4 arr 2 3 5 6 7 Ver. 1.0 Session 4
  • 25. Data Structures and Algorithms Implementing Bubble Sort Algorithm (Contd.) Write an algorithm to implement bubble sort. Algorithm for bubble sort: • Set pass = 1. • Repeat step 3 varying j from 0 to n – 1 – pass. • If the element at index j is greater than the element at index j + 1, swap the two elements. • Increment pass by 1. • If pass <= n – 1, go to step 2. Ver. 1.0 Session 4
  • 26. Data Structures and Algorithms Determining the Efficiency of Bubble Sort Algorithm • The efficiency of a sorting algorithm is measured in terms of number of comparisons. • In bubble sort, there are n – 1 comparisons in Pass 1, n – 2 comparisons in Pass 2, and so on. • Total number of comparisons = (n – 1) + (n – 2) + (n – 3) + … + 3 + 2 + 1 = n(n – 1)/2. • n(n – 1)/2 is of O(n2) order. Therefore, the bubble sort algorithm is of the order O(n2). Ver. 1.0 Session 4
  • 27. Data Structures and Algorithms Just a minute What is the order of growth of the bubble sort algorithm? Answer: The bubble sort algorithm has a quadratic order of growth. Ver. 1.0 Session 4
  • 28. Data Structures and Algorithms Just a minute While implementing bubble sort algorithm, how many comparisons will be performed in Pass 1. Answer: n – 1 comparisons Ver. 1.0 Session 4
  • 29. Data Structures and Algorithms Activity: Sorting Data by Using Bubble Sort Algorithm Problem Statement: Write a program to store the marks of 10 students in an array. Include a function to sort the elements of the array by using the bubble sort algorithm. After sorting the array, display the sorted list. Ver. 1.0 Session 4
  • 30. Data Structures and Algorithms Sorting Data by Using Selection Sort Selection sort algorithm: Has a quadratic order of growth and is therefore suitable for sorting small lists only Scans through the list iteratively, selects one item in each scan, and moves the item to its correct position in the list Ver. 1.0 Session 4
  • 31. Data Structures and Algorithms Implementing Selection Sort Algorithm To understand the implementation of selection sort algorithm, consider an unsorted list of numbers stored in an array. 0 1 2 3 4 arr 105 120 10 200 20 Ver. 1.0 Session 4
  • 32. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Let us sort this unsorted list. 0 1 2 3 4 arr 105 120 10 200 20 Ver. 1.0 Session 4
  • 33. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 1 n=5 Search the minimum value in the array, arr[0] to arr[n – 1]. 0 1 2 3 4 arr 105 120 10 200 20 min Ver. 1.0 Session 4
  • 34. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 1 n=5 Search the minimum value in the array, arr[0] to arr[n – 1]. Swap the minimum value with the value at index 0. Swap 0 1 2 3 4 arr 105 120 105 200 20 10 10 min Ver. 1.0 Session 4
  • 35. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 1 n=5 Search the minimum value in the array, arr[0] to arr[n – 1]. Swap the minimum value with the value at index 0. 0 1 2 3 4 arr 10 120 105 200 20 The smallest value is placed at its correct location after Pass 1 Ver. 1.0 Session 4
  • 36. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 2 n=5 Search the minimum value in the array, arr[1] to arr[n – 1]. Swap the minimum value with the value at index 1. Swap 0 1 2 3 4 arr 10 120 105 200 120 20 20 min Ver. 1.0 Session 4
  • 37. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 2 n=5 Search the minimum value in the array, arr[1] to arr[n – 1]. Swap the minimum value with the value at index 1. 0 1 2 3 4 arr 10 120 105 200 120 20 20 The second smallest value is placed at its correct location after Pass 2 Ver. 1.0 Session 4
  • 38. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 3 n=5 Search the minimum value in the array, arr[2] to arr[n – 1]. Swap the minimum value with the value at index 2. 0 1 2 3 4 arr 10 120 105 200 120 20 20 min Ver. 1.0 Session 4
  • 39. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 3 n=5 Search the minimum value in the array, arr[2] to arr[n – 1]. Swap the minimum value with the value at index 2. 0 1 2 3 4 arr 10 120 105 200 120 20 20 min The third smallest value is placed at its correct location after Pass 3 Ver. 1.0 Session 4
  • 40. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 4 n=5 Search the minimum value in the array, arr[3] to arr[n – 1]. Swap the minimum value with the value at index 3. Swap 0 1 2 3 4 arr 10 120 105 120 120 20 200 200 20 min Ver. 1.0 Session 4
  • 41. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 4 n=5 Search the minimum value in the array, arr[3] to arr[n – 1]. Swap the minimum value with the value at index 3. 0 1 2 3 4 arr 10 120 105 120 200 20 200 20 The fourth smallest value is placed at its correct location after Pass 4 Ver. 1.0 Session 4
  • 42. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Pass 4 n=5 The list is now sorted. 0 1 2 3 4 arr 10 120 105 120 200 20 200 20 Ver. 1.0 Session 4
  • 43. Data Structures and Algorithms Implementing Selection Sort Algorithm (Contd.) Write an algorithm to implement selection sort. Algorithm for selection sort: 1. Repeat steps 2 and 3 varying j from 0 to n – 2 2. Find the minimum value in arr[j] to arr[n – 1]: a. Set min_index = j b. Repeat step c varying i from j + 1 to n – 1 c. If arr[i] < arr[min_index]: i. min_index = i 3. Swap arr[j] with arr[min_index] Ver. 1.0 Session 4
  • 44. Data Structures and Algorithms Determining the Efficiency of Selection Sort Algorithm • In selection sort, there are n – 1 comparisons during Pass 1 to find the smallest element, n – 2 comparisons during Pass 2 to find the second smallest element, and so on. • Total number of comparisons = (n – 1) + (n – 2) + (n – 3) + … + 3 + 2 + 1 = n(n – 1)/2 • n(n – 1)/2 is of O(n2) order. Therefore, the selection sort algorithm is of the order O(n2). Ver. 1.0 Session 4
  • 45. Data Structures and Algorithms Just a minute Read the following statement and specify whether it is true or false: The efficiency of selection sort algorithm is same as that of the bubble sort algorithm. Answer: True Ver. 1.0 Session 4
  • 46. Data Structures and Algorithms Just a minute How many comparisons are performed in the second pass of the selection sort algorithm? Answer: n–2 Ver. 1.0 Session 4
  • 47. Data Structures and Algorithms Sorting Data by Using Insertion Sort Insertion sort algorithm: Has a quadratic order of growth and is therefore suitable for sorting small lists only Is much more efficient than bubble sort, and selection sort, if the list that needs to be sorted is nearly sorted Ver. 1.0 Session 4
  • 48. Data Structures and Algorithms Implementing Insertion Sort Algorithm To understand the implementation of insertion sort algorithm, consider an unsorted list of numbers stored in an array. 0 1 2 3 4 arr 70 80 30 10 20 Ver. 1.0 Session 4
  • 49. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) To sort this list by using the insertion sort algorithm: You need to divide the list into two sublists, sorted and unsorted. 0 1 2 3 4 arr 70 80 30 10 20 Ver. 1.0 Session 4
  • 50. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) To sort this list by using the insertion sort algorithm: You need to divide the list into two sublists, sorted and unsorted. Initially, the sorted list has the first element and the unsorted list has the remaining 4 elements. 0 1 2 3 4 70 80 30 10 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 51. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 1 Place the first element from the unsorted list at its correct position in the sorted list. 0 1 2 3 4 70 80 30 10 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 52. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 1 Place the first element from the unsorted list at its correct position in the sorted list. 0 1 2 3 4 70 80 30 10 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 53. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 2 Place the first element from the unsorted list at its correct position in the sorted list. 0 1 2 3 4 70 80 30 10 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 54. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 2 Place the first element from the unsorted list at its correct position in the sorted list. 30 70 80 10 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 55. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 3 Place the first element from the unsorted list at its correct position in the sorted list. 30 70 80 10 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 56. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 3 Place the first element from the unsorted list at its correct position in the sorted list. 0 1 2 3 4 10 30 70 80 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 57. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 4 Place the first element from the unsorted list at its correct position in the sorted list. 0 1 2 3 4 10 30 70 80 20 Sorted List Unsorted List Ver. 1.0 Session 4
  • 58. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Pass 4 Place the first element from the unsorted list at its correct position in the sorted list. 0 1 2 3 4 10 20 30 70 80 Sorted List Unsorted List Ver. 1.0 Session 4
  • 59. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) Let us now write an algorithm to 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 implement insertion sort algorithm. 3. Set temp = arr[i] 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 Ver. 1.0 Session 4
  • 60. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 Ver. 1.0 Session 4
  • 61. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 62. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 • Set temp = arr[i] temp = 80 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 63. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 • Set temp = arr[i] temp = 80 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 64. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 • Set temp = arr[i] temp = 80 • Set j = i – 1 arr[j] < temp • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 65. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 3. Set temp = arr[i] temp = 80 5. Set j = i – 1 arr[j] < temp 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 • Store temp at index j + 1 j i Value temp is stored at its correct position in the sorted list Ver. 1.0 Session 4
  • 66. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =1 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 Ver. 1.0 Session 4
  • 67. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 Ver. 1.0 Session 4
  • 68. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 69. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 • Set temp = arr[i] temp = 30 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 70. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 • Set temp = arr[i] temp = 30 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 71. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 • Set temp = arr[i] temp = 30 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 70 80 30 10 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 72. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 3. Set temp = arr[i] temp = 30 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 70 80 30 80 10 20 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 73. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 3. Set temp = arr[i] temp = 30 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 70 80 10 20 • Decrement j by 1 9. Store temp at index j + 1 j j i Ver. 1.0 Session 4
  • 74. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 3. Set temp = arr[i] temp = 30 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 70 70 80 10 20 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 75. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 3. Set temp = arr[i] temp = 30 5. Set j = i – 1 j = –1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 70 80 10 20 • Decrement j by 1 • Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 76. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 3. Set temp = arr[i] temp = 30 5. Set j = i – 1 j = –1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 30 70 80 10 20 b. Decrement j by 1 • Store temp at index j + 1 i Ver. 1.0 Session 4
  • 77. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =2 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 30 70 80 10 20 b. Decrement j by 1 9. Store temp at index j + 1 Ver. 1.0 Session 4
  • 78. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 30 70 80 10 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 79. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 • Set temp = arr[i] temp = 10 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 30 70 80 10 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 80. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 • Set temp = arr[i] temp = 10 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 30 70 80 10 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 81. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 • Set temp = arr[i] temp = 10 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 30 70 80 10 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 82. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 30 70 80 80 10 20 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 83. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 30 70 80 20 • Decrement j by 1 • Store temp at index j + 1 j j i Ver. 1.0 Session 4
  • 84. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 30 70 70 80 20 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 85. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 30 70 80 20 • Decrement j by 1 • Store temp at index j + 1 j j i Ver. 1.0 Session 4
  • 86. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 30 30 70 80 20 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 87. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 j = –1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 30 70 80 20 • Decrement j by 1 • Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 88. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 3. Set temp = arr[i] temp = 10 5. Set j = i – 1 j = –1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 30 70 80 20 b. Decrement j by 1 • Store temp at index j + 1 i Ver. 1.0 Session 4
  • 89. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =3 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 30 70 80 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 90. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 • Set temp = arr[i] • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 30 70 80 20 b. Decrement j by 1 9. Store temp at index j + 1 i i Ver. 1.0 Session 4
  • 91. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 • Set temp = arr[i] temp = 20 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 30 70 80 20 b. Decrement j by 1 9. Store temp at index j + 1 i Ver. 1.0 Session 4
  • 92. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 • Set temp = arr[i] temp = 20 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 30 70 80 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 93. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 • Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 • Set temp = arr[i] temp = 20 • Set j = i – 1 • Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 30 70 80 20 b. Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 94. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 10 30 70 80 80 20 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 95. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 10 30 70 80 • Decrement j by 1 • Store temp at index j + 1 j j i Ver. 1.0 Session 4
  • 96. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 10 30 70 70 80 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 97. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 10 30 70 80 • Decrement j by 1 • Store temp at index j + 1 j j i Ver. 1.0 Session 4
  • 98. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 10 30 30 70 80 • Decrement j by 1 9. Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 99. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 • Shift the value at index j to index j + 1 arr 10 30 70 80 • Decrement j by 1 • Store temp at index j + 1 j j i Ver. 1.0 Session 4
  • 100. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] temp = 20 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 20 30 70 80 b. Decrement j by 1 • Store temp at index j + 1 j i Ver. 1.0 Session 4
  • 101. Data Structures and Algorithms Implementing Insertion Sort Algorithm (Contd.) n=5 1. Repeat steps 2, 3, 4, and 5 varying i from 1 to n – 1 i =4 3. Set temp = arr[i] 5. Set j = i – 1 7. Repeat until j becomes less than 0 or arr[j] becomes less than or equal to temp: 0 1 2 3 4 a. Shift the value at index j to index j + 1 arr 10 20 30 70 80 b. Decrement j by 1 9. Store temp at index j + 1 j i The list is now sorted Ver. 1.0 Session 4
  • 102. Data Structures and Algorithms Determining the Efficiency of Insertion Sort Algorithm To sort a list of size n by using insertion sort, you need to perform (n – 1) passes. Best Case Efficiency: Best case occurs when the list is already sorted. In this case, you will have to make only one comparison in each pass. In n – 1 passes, you will need to make n – 1 comparisons. The best case efficiency of insertion sort is of the order O(n). Worst Case Efficiency: – Worst case occurs when the list is sorted in the reverse order. – In this case, you need to perform one comparison in the first pass, two comparisons in the second pass, three comparisons in the third pass, and n – 1 comparisons in the (n – 1)th pass. – The worst case efficiency of insertion sort is of the order O(n2). Ver. 1.0 Session 4
  • 103. Data Structures and Algorithms Just a minute A sales manager has to do a research on best seller cold drinks in the market for the year 2004-2006. David, the software developer, has a list of all the cold drink brands along with their sales figures stored in a file. David has to provide the sorted data to the sales manager. The data in the file is more or less sorted. Which sorting algorithm will be most efficient for sorting this data and why? Answer: Insertion sort provides better efficiency than bubble sort and selection sort when the list is partially sorted. Therefore, David should use the insertion sort algorithm. Ver. 1.0 Session 4
  • 104. Data Structures and Algorithms Sorting Data by Using Shell Sort Shell sort algorithm: Insertion sort is an efficient algorithm only if the list is already partially sorted and results in an inefficient solution in an average case. To overcome this limitation, a computer scientist, D.L. Shell proposed an improvement over the insertion sort algorithm. The new algorithm was called shell sort after the name of its proposer. Ver. 1.0 Session 4
  • 105. Data Structures and Algorithms Implementing Shell Sort Algorithm Shell sort algorithm: Improves insertion sort by comparing the elements separated by a distance of several positions to form multiple sublists Applies insertion sort on each sublist to move the elements towards their correct positions Helps an element to take a bigger step towards its correct position, thereby reducing the number of comparisons Ver. 1.0 Session 4
  • 106. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) To understand the implementation of shell sort algorithm, consider an unsorted list of numbers stored in an array. 0 1 2 3 4 5 6 7 8 9 10 arr 70 30 40 10 80 20 90 110 75 60 45 Ver. 1.0 Session 4
  • 107. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) To apply shell sort on this array, you need to: Select the distance by which the elements in a group will be separated to form multiple sublists. Apply insertion sort on each sublist to move the elements towards their correct positions. 0 1 2 3 4 5 6 7 8 9 10 arr 70 30 40 10 80 20 90 110 75 60 45 Ver. 1.0 Session 4
  • 108. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) Increment = 3 0 3 6 9 1 4 7 10 List 1 = 70 10 90 60 List 2 = 30 80 110 45 Pass = 1 2 5 8 List 3 = 40 20 75 0 1 2 3 4 5 6 7 8 9 10 arr 70 30 40 10 80 20 90 110 75 60 45 Ver. 1.0 Session 4
  • 109. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) 0 3 6 9 1 4 7 10 List 1 = 10 70 60 70 10 90 90 60 List 2 = 30 80 110 45 45 80 110 2 5 8 List 3 = 20 40 40 75 20 Apply insertion sort to sort the The lists are sorted three lists Ver. 1.0 Session 4
  • 110. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) 0 3 6 9 1 4 7 10 List 1 = 10 60 70 90 List 2 = 30 45 80 110 2 5 8 List 3 = 20 40 75 0 1 2 3 4 5 6 7 8 9 10 arr 10 30 20 60 45 40 70 80 75 90 110 Ver. 1.0 Session 4
  • 111. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) 0 2 4 6 8 10 Increment = 2 List 1 = 10 2 20 45 Pass = 70 75 110 1 3 5 7 9 List 2 = 30 60 40 80 90 0 1 2 3 4 5 6 7 8 9 10 arr 10 30 20 60 45 40 70 80 75 90 110 Ver. 1.0 Session 4
  • 112. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) 0 2 4 6 8 10 List 1 = 10 20 45 70 75 110 1 3 5 7 9 List 2 = 30 60 40 80 90 Apply insertion sort on each sublist Ver. 1.0 Session 4
  • 113. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) 0 2 4 6 8 10 List 1 = 10 20 45 70 75 110 1 3 5 7 9 List 2 = 30 40 60 80 90 The lists are now sorted Ver. 1.0 Session 4
  • 114. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) 0 2 4 6 8 10 List 1 = 10 20 45 70 75 110 1 3 5 7 9 List 2 = 30 40 60 80 90 0 1 2 3 4 5 6 7 8 9 10 arr 10 30 20 40 45 60 70 80 75 90 110 Ver. 1.0 Session 4
  • 115. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) Increment = 1 Pass = 3 0 1 2 3 4 5 6 7 8 9 10 arr 10 30 20 40 45 60 70 80 75 90 110 Apply insertion sort to sort the list Ver. 1.0 Session 4
  • 116. Data Structures and Algorithms Implementing Shell Sort Algorithm (Contd.) Increment = 1 Pass = 3 0 1 2 3 4 5 6 7 8 9 10 arr 10 20 30 40 45 60 70 75 80 90 110 The list is now sorted Ver. 1.0 Session 4
  • 117. Data Structures and Algorithms Just a minute Which of the following sorting algorithms compares the elements separated by a distance of several positions to sort the data? The options are: 1. Insertion sort 2. Selection sort 3. Bubble sort 4. Shell sort Answer: 4. Shell sort Ver. 1.0 Session 4
  • 118. Data Structures and Algorithms Summary In this session, you learned that: Sorting is a process of arranging data in some pre-defined order of a key value. The order can be either ascending or descending. There are various sorting algorithms that are used to sort data. Some of them are: Bubble sort Selection sort Insertion sort Shell sort Merge sort Quick sort Heap sort Ver. 1.0 Session 4
  • 119. Data Structures and Algorithms Summary (Contd.) To select an appropriate algorithm, you need to consider the following: – Execution time – Storage space – Programming effort – Bubble sort and selection algorithms have a quadratic order of growth, and are therefore suitable for sorting small lists only. – Insertion sort performs different number of comparisons depending on the initial ordering of elements. When the elements are already in the sorted order, insertion sort needs to make very few comparisons. – Insertion sort is an efficient algorithm than bubble sort and selection sort if the list that needs to be sorted is nearly sorted. Ver. 1.0 Session 4
  • 120. Data Structures and Algorithms Summary (Contd.) Shell sort improves insertion sort by comparing the elements separated by a distance of several positions. This helps an element to take a bigger step towards its correct position, thereby reducing the number of comparisons. Ver. 1.0 Session 4

Notes de l'éditeur

  1. Ask students to answer this question and then come to the given example.
  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 write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  23. 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.
  24. 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.
  25. 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.
  26. Specify path for solution
  27. Ask students to answer this question and then come to the given example.
  28. Ask students to answer this question and then come to the given example.
  29. Ask students to answer this question and then come to the given example.
  30. Ask students to answer this question and then come to the given example.
  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 write an algorithm to implement bubble sorting. Given them 5-10 minutes to write the algorithm. Then show them the answer.
  41. 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.
  42. 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.
  43. 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.
  44. Ask students to answer this question and then come to the given example.
  45. Ask students to answer this question and then come to the given example.
  46. Ask students to answer this question and then come to the given example.
  47. Ask students to answer this question and then come to the given example.
  48. Ask students to answer this question and then come to the given example.
  49. Ask students to answer this question and then come to the given example.
  50. Ask students to answer this question and then come to the given example.
  51. Ask students to answer this question and then come to the given example.
  52. Ask students to answer this question and then come to the given example.
  53. Ask students to answer this question and then come to the given example.
  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 answer this question and then come to the given example.
  61. Ask students to answer this question and then come to the given example.
  62. Ask students to answer this question and then come to the given example.
  63. Ask students to answer this question and then come to the given example.
  64. Ask students to answer this question and then come to the given example.
  65. Ask students to answer this question and then come to the given example.
  66. Ask students to answer this question and then come to the given example.
  67. Ask students to answer this question and then come to the given example.
  68. Ask students to answer this question and then come to the given example.
  69. Ask students to answer this question and then come to the given example.
  70. Ask students to answer this question and then come to the given example.
  71. Ask students to answer this question and then come to the given example.
  72. Ask students to answer this question and then come to the given example.
  73. Ask students to answer this question and then come to the given example.
  74. Ask students to answer this question and then come to the given example.
  75. Ask students to answer this question and then come to the given example.
  76. Ask students to answer this question and then come to the given example.
  77. Ask students to answer this question and then come to the given example.
  78. Ask students to answer this question and then come to the given example.
  79. Ask students to answer this question and then come to the given example.
  80. Ask students to answer this question and then come to the given example.
  81. Ask students to answer this question and then come to the given example.
  82. Ask students to answer this question and then come to the given example.
  83. Ask students to answer this question and then come to the given example.
  84. Ask students to answer this question and then come to the given example.
  85. Ask students to answer this question and then come to the given example.
  86. Ask students to answer this question and then come to the given example.
  87. Ask students to answer this question and then come to the given example.
  88. Ask students to answer this question and then come to the given example.
  89. Ask students to answer this question and then come to the given example.
  90. Ask students to answer this question and then come to the given example.
  91. Ask students to answer this question and then come to the given example.
  92. Ask students to answer this question and then come to the given example.
  93. Ask students to answer this question and then come to the given example.
  94. Ask students to answer this question and then come to the given example.
  95. Ask students to answer this question and then come to the given example.
  96. Ask students to answer this question and then come to the given example.
  97. Ask students to answer this question and then come to the given example.
  98. Ask students to answer this question and then come to the given example.
  99. 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.
  100. 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.
  101. Ask students to answer this question and then come to the given example.
  102. Ask students to answer this question and then come to the given example.
  103. Ask students to answer this question and then come to the given example.
  104. Ask students to answer this question and then come to the given example.
  105. Ask students to answer this question and then come to the given example.
  106. Ask students to answer this question and then come to the given example.
  107. Ask students to answer this question and then come to the given example.
  108. Ask students to answer this question and then come to the given example.
  109. Ask students to answer this question and then come to the given example.
  110. Ask students to answer this question and then come to the given example.
  111. Ask students to answer this question and then come to the given example.
  112. Ask students to answer this question and then come to the given example.
  113. Ask students to answer this question and then come to the given example.
  114. 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.