## Sorting and SearchingSorting and Searching are two very important tasks used in many different computer applications. There are several technique for each that we will talk about in CS 171. ## SearchingThe searching problem involves finding if an element (called the key) exists in a list and the location of the element. This problem is simplified if the list is sorted. ## Sequential SearchSequential search starts at the beginning of the sorted list and compares each element for the key. The search ends when the key is located or the element in the list is larger than the key. ## Binary SearchBinary search starts at the middle of a sorted list, comparing the element in the middle (say m) with the key. Comparing the key and the middle element will results in one of three possibilities: - key = m: The element was found!
- key < m: The element was not found. If the list on the left of the middle exists, binary search the left sublist. Otherwise the key is not in the list.
- key > m: The element was not found. If the list on the right of the middle exists, binary search the right sublist. Otherwise the key is not in the list.
## SortingThe sorting problem involves taking a list and sorting the elements, typically in nondecreasing order. ## Insertion SortProceed by inserting the elements, one at a time, into an increasingly larger sublist. For the ith element, find its location in the beginning of the list; shuffle all larger elements, creating a hole where the ith element can be placed. ## Selection SortProceed by selecting the smallest element in the unsorted list. Swap it with the smallest element in the unsorted list. Proceed until the entire list is sorted. ## Merge-SortMerge-Sort uses a divide and conquer strategy. Divide list into two equal (+1 or -1) sublists. Repeat division until lists are of size 1, which is a sorted list! Now merge smaller lists back into larger lists by comparing leading elements. Merge lists into larger lists in the reverse order in whihc they were divided. The last merge results in a sorted list. ## Other Sorting AlgorithmsExplore the other sorting methods on you own. These are discussed in other courses such as CS 173 and CS 356. ## References- Sequential search from wikipedia.
- Binary search from wikipedia.
- Selection Sort from wikipedia.
- Insertion Sort from wikipedia.
- Merge sort from wikipedia.
- Dancing Sorts.
- Sorting Algorithm Animations.
Copyright © 2017, David A. Reimann. All rights reserved. |