160 likes | 493 Views
Sorting and Searching. Cristian Hill. The Need for Searching and Sorting (2.1.7). Because computers are constructed to solve data- processing problems, updating databases is a common operation.
E N D
Sorting and Searching Cristian Hill
The Need for Searching and Sorting(2.1.7) • Because computers are constructed to solve data- processing problems, updating databases is a common operation. • For example, adding, deleting, and modifying require that the database be searched for a specific index • Sorting is needed because Searching is made easier when there is order to the database
What you need to know • Two types of sorting and searching. • Ability to compare for efficiency and applicability
Types of Searching Linear(Sequential) Binary • Each object is examined in turn • Ends when item is found or end is reached • Does not need to be sorted • Checks midpoint, if value is greater than midpoint, then midpoint equals first value, recalculate midpoint, repeat • Continues until item is found or found not to be present • Requires that array(or similar) is sorted • Comparable to looking through a dictionary or telephone book Examples are on next slide
Example of Process • Linear(Sequential) • Searching for “1” • Binary • Searching for 4 Not equal to one, move to next number Equal to one stop and return index Mid point is 6, 3 < 6 , so 6 becomes new max 4 is the new midpoint, 4=search parameter
Efficiency of Searching Algorithms(Time) All numbers are completely arbitrary, but is what is given by the IB approved book
Bubble Sorting • Starts with the first pair of numbers at index 0 and 1 • The values are then swapped if necessary, and the next 2 indexes are compared(1 and 2) • This process is repeated until all the end of the array is reached • After the first pass it is repeated over and over until no changes are made.
Selection Sort • Sorts by finding the largest element in array and moving it to the 1st index, then continues with the second largest and so on. • This is done by splitting the list into 2 parts(sorted and unsorted) • Unsorted elements are searched for the largest value and this is then moved to first element of the unsorted list. The boundary of the sorted list is then increased