Like this presentation? Why not share!

No Downloads

Total Views

1,532

On Slideshare

0

From Embeds

0

Number of Embeds

2

Shares

0

Downloads

0

Comments

0

Likes

4

No embeds

No notes for slide

- 1. Data Structures and AlgorithmsObjectives 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 AlgorithmsSorting 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 AlgorithmsSorting 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 AlgorithmsSorting 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 AlgorithmsSelecting 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 AlgorithmsSorting 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsDetermining 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 AlgorithmsJust 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 AlgorithmsJust 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 AlgorithmsActivity: 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 AlgorithmsSorting 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsDetermining 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 AlgorithmsJust 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 AlgorithmsJust 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 AlgorithmsSorting 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsDetermining 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 AlgorithmsJust 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 AlgorithmsSorting 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsImplementing 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 AlgorithmsJust 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 AlgorithmsSummary 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 AlgorithmsSummary (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 AlgorithmsSummary (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

No public clipboards found for this slide

Be the first to comment