Sorting & Searching Algorithms

advertisement
SORTING AND SEARCHING ALGORITHMS
Animated demonstration: http://www.sorting-algorithms.com/
SEARCHING ALGORITHMS
Sequential Search Algorithm
Use a loop to sequentially step through an array to find a specific item in a collection of data. A flag is
used to exit the loop one it is found.
1. Advantage: Simple
2. Disadvantage: Not efficient
3. Complexity: O(n)
The Binary Search Algorithm
Clever algorithm and more efficient than others but the values in the arrary must be sorted in ascending
order. Starts with the element in the middle and compares it to the value and then goes up or down thus
reducing half the array's elements. Returns -1 if value does not exist.
1. Advantage: Efficient and clever
2. Disadvantage: The array must be sorted first
3. Complexity: O(log2(n))
Example:
0 5 13 19 22 41 55 68 72 81 98
55 68 72 81 98
55 68
Binary Search Tree
Example of a binary tree search:
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/binarySearchTree.htm
1. Advantage: Very efficient
2. Disadvantage: algorithms can get complicated
3. Complexity: O(log n)
1
SORTING ALGORITHMS
The Selection Sort Algorithm
Step through example shown on Pgs. 443-446
Scans through an array and rearranges its contents in some specific order. The smallest value in the
array is located and moved to element 0. Then the next smallest value is located and moved to element
1, so forth and so on. Worst Case: If the array being searched has 20,000 elements, this algorithm will
have to look at all 20,000 elements to find a value stored in the last element. Typically, this search will
locate an item (if it exists in the array) in N/2 attempts. N=50,000 it will search 25,000.
1. Advantage: Simple
2. Disadvantage: Not efficient
3. Complexity: O( )
Example1:
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64
Ex2:
1
1
1
1
4
2
2
2
7
7
4
4
9
9
9
7
2
4
7
9
http://en.wikipedia.org/wiki/Bubble_sort For another example\
Bubble Sort
It works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and
swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are
needed, which indicates that the list is sorted. http://en.wikipedia.org/wiki/Bubble_sort
1. Advantage: Simple
2. Disadvantage: Not efficient for large lists
3. Complexity: O( )
First Pass:
(51428)
(15428)
(14528)
(14258)
( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not
2
swap them.
Second Pass:
(14258)
(14258)
(14258)
( 1 2 4 5 8 ), Swap since 4 > 2
(12458)
(12458)
(12458)
(12458)
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm
needs one whole pass without any swap to know it is sorted.
Third Pass:
(12458)
(12458)
(12458)
(12458)
(12458)
(12458)
(12458)
( 1 2 4 5 8 ) Finally, the array is sorted, and the algorithm can terminate
Quicksort
http://en.wikipedia.org/wiki/Quicksort
Break and array into two parts and then move elements around so that all the larger values are in one
end and all the smaller values are in the other. Each of the two parts is then subdivided in the same
manner, and so on, until the subparts contain only a single value, at which point the array is sorted.
1. Advantage: Efficient and fast sort
2. Disadvantage: Worst-case complexity rating is poor
3. Uses recursion
4. Complexity: O(n log n)
HeapSort
1. Advantage: Efficient and fast sort
2. Disadvantage: Usually slower than a quicksort
3. Complexity:
3
The BigO Notation is a method is used to determine the performance of the algorithm based on
the data being arrange in a worst case order. Performance is determined by efficiency: running
time and memory usage.
BigO
BigO
Advantages/Disadvantages
Searches
1. Sequential Search Algorithm
1. Advantage: Simple
2. Disadvantage: Not efficient
2. Binary Search Algorithm
1. Advantage: Efficient and clever
2. Disadvantage: The array must be sorted first
3. Binary Tree Search
1. Advantage: Very efficient
2. Disadvantage: algorithms can get complicated
Sorts
2. Selection Sort Algorithm
1. Advantage: Simple
2. Disadvantage: Not efficient
3. Bubble Sort
1. Advantage: Simple
2. Disadvantage: Not efficient for large lists
4. Quicksort
1. Advantage: Efficient and fast sort
2. Disadvantage: Worst-case complexity rating is poor
3. Uses recursion
5. HeapSort
1. Advantage: Efficient and fast sort
2. Disadvantage: Usually slower than a quicksort
TEXTBOOK EXERCISES
CHECKPOINT – PG 451
7.17
7.18
7.19
7.20
7.21
4
Download