TAILIEUCHUNG - Lecture Algorithms and data structures: Chapter 5 - Insertion Sort
This topic will describe the storage of objects in containers, we will focus on linear orderings: Implicitly defined linear orderings (sorted lists), explicitly defined linear orderings. We will summarize this information and look briefly at: Hierarchical orderings, partial orderings, equivalence relations, adjacency relations. | Review 1 Bubble Sort Bubble Sort Algorithm Time Complexity Best case Average case Worst case Examples Insertion Sort 2 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples Insertion Sort Insertion sort algorithm sorts a set of values by inserting values into an existing file Compare the second element with first, if the first element is greater than second place it before the first one Otherwise place is just after the first one Compare the third value with second If the third value is greater than the second value then place it just after the second Otherwise place the second value to the third place 3 And compare third value with the first value If the third value is greater than the first value place the third value to second place Otherwise place the first value to second place And place the third value to first place and so on 4 Let A be a linear array of n numbers A [1], A [2], A [3], A[n] The algorithm scan the array A from A [1] to A [n] by inserting each element A[k], into the proper position of the previously sorted sub list A [1], A [2], A [3], A [k – 1] 5 Step 1: As the single element A [1] by itself is sorted array. Step 2: A [2] is inserted either before or after A [1] by comparing it so that A[1], A[2] is sorted array. Step 3: A [3] is inserted into the proper place in A [1], A [2], that is A [3] will be compared with A [1] and A [2] and placed before A [1], between A [1] and A[2], or after A [2] so that A [1], A [2], A [3] is a sorted array. Step 4: A [4] is inserted in to a proper place in A [1], A [2], A [3] by comparing it; so that A [1], A [2], A [3], A [4] is a sorted array. Step 5: Repeat the process by inserting the element in the proper place in array Step n : A [n] is inserted into its proper place in an array A [1], A [2], A [3], A [n –1] so that A [1], A [2], A [3], ,A [n] is a sorted array 6 Algorithm Let A be a linear array of n numbers A [1], A [2], A [3],
đang nạp các trang xem trước