CS301-Lec44 handout

CS301-Lec44 handout - CS301 Data Structures Lecture No 44...

This preview shows pages 1–3. Sign up to view the full content.

CS301 – Data Structures Lecture No. 44 ___________________________________________________________________ Data Structures Lecture No. 44 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 7 7.1, 7.2 Summary Selection Sort o Selection Sort analysis Insertion Sort o Insertion Sort Analysis Bubble Sort o Bubble Sort analysis Summary N log 2 (N) Algorithms This is the sequel of the previous lecture in which we talked about the sort algorithms. There are three elementary sorting methods being discussed in this hand out. These are- selection sort, insertion sort and bubble sort. To begin with, we will talk about the selection sort algorithm. Selection Sort Suppose we have an array with different numbers. For sorting it in an ascending order, selection sorting will be applied on it in the following manner. We find the smallest number in the array and bring it to the position 1. We do the same process with the remaining part of the array and bring the smallest number among the remaining array to the next position. This process continues till the time all the positions of the array are filled with the elements. Thus the main idea of selection sort is that find the smallest element put it in the first position find the next smallest element in the remaining elements put it in the second position And so on, until we get to the end of the array Let’s understand this algorithm by considering an example with the help of figures. Consider an array that has four numbers i.e. 19, 5, 7 and 12 respectively. Now we want to sort this array in ascending order. To sort the array, selection algorithm will Page 1 of 11

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS301 – Data Structures Lecture No. 44 ___________________________________________________________________ be applied on it. The above pictorial representation explains the selection sort. It describes that at the start, we begin the search for the smallest number from the first position of the array i.e. from the index zero. We find that 5 is the smallest number and bring it to the first position i.e. index 0. Later, number 19 is put at the position that was occupied by number 5. Thus in a sense, we swap these numbers. Now 5, the smallest number in the array, has come at its final position i.e. index 0. As index 0 has the proper number, so we start our search from position 2 i.e. index 1. Now we look at the remaining elements 19, 7, 12 and find the smallest number among them. Here 7 is the smallest so we change its position with 19 to bring 7 at its position. Thus 5 and 7 have come at their final positions. Now there are two elements are left behind i.e. 12 and 19. We search the smallest among these and find that 12 is the smallest. So we swap it with 19 to bring it at index 2 i.e. its final position. Now there is last element remaining and obviously it is at its position as there is no element to compare with it. The array is now in the sorted form with ascending numbers.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

CS301-Lec44 handout - CS301 Data Structures Lecture No 44...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online