Download presentation
Presentation is loading. Please wait.
Published byOwen Thomas Modified over 9 years ago
1
Sorting and Searching
2
Selection Sort “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element. 2) Now find the smallest element in the subarray a[1]…a[n-1] and swap it with a[1], the second element in the array. 3) Continue this process until just the last two elements remain to be sorted, a[n-2] and a[n-1]. 4) The smaller of these two elements is placed in a[n-2]; the larger in a[n-1]; and the sort is complete.
3
Selection Sort Example * Data items stored at asterisk and above are sorted in order relative to each other Unsorted Array After 1 st Pass After 2 nd Pass After 3 rd Pass After 4 th Pass 41111 22222 55533 14444 33355
4
Selection Sort cont… Before writing the algorithm for this sorting method, note the following. 1) If the array is of length n, we need n-1 steps 2) We must be able to find the smallest number 3) We need to exchange appropriate array items.
5
Insertion Sort Think of the first element in the array, a[0], as being sorted with respect to itself. The array can now be thought of as consisting of two parts, a sorted list followed by an unsorted list. The idea of insertion is to move elements from the unsorted list to the sorted list one at a time; as each is moved, it is inserted into its correct position in the sorted list. In order to place the new item, some elements may need to be moved down to create a slot.
6
Insertion Sort Example Insertion Sort Example Data items stored at asterisk and above are sorted in order relative to each other Data items stored at asterisk and above are sorted in order relative to each other Current element being compared Current element being compared Unsorted Array After 1 st Pass After 2 nd Pass After 3 rd Pass After 4 th Pass 22111 55*222 11 43 444 4 3333
7
Bubble Sort Compares adjacent pairs of items. Whenever two items are out of order with respect to each other, they are swapped. After one pass, we are assured that the array will have the item that comes last in order in the final array position. That is, the last item will “sink” to the bottom of the array, and preceding items will gradually “float” to the top.
8
Bubble Sort Example Unsorted Array After 1 st Pass After 2 nd Pass After 3 rd Pass After 4 th Pass 54*444 45*2*22 225*1*1 1115*3* 33335* * Items just swapped
9
Merge Sort If there is more than one element in the array: 1) Break the array into two halves 2) Mergesort the left half. 3) Mergesort the right half. 4) Merge the two subarrays into a sorted array. The main disadvantage of mergesort is that it uses a temporary array.
10
Merge Sort Example 5 -3 2 4 0 6 2 -3 5 0 4 6 -3 2 5 -3 0 2 4 5 6 0 4 6 4 0 2-3 65 2 5 -3 4 0 6 4 0 6 5 -3 2
11
Sequential Search Assume that you are searching for a key in a list of n elements. A sequential search starts at the first element and compares the key to each element in turn until the key is found or there are no more elements to examine in the list. If the list is sorted, in ascending order, say, stop searching as soon as the key is less than the current list element.
12
Binary Search *Works only if the array is sorted. Compare key to middle number of list. If key is greater, compare key to middle number of greater sub-array. If key is less, compare key to middle number of smaller sub-array. Continue steps until key is found or there is not middle number.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.