Algorithm
Sorting Algorithm
In computer science, a sorting algorithm is an algorithm that puts
elements
of
list
in
certain
order.
The
is
important
for
optimizing
the
use
Example 1.
Selection sort
Selection sort performs sorting by repeatedly putting the largest
element in the unsorted portion of the array to the end of this unsorted
portion until the whole array is sorted.
2. While the unsorted portion of the array has more than one element:
Find its largest element.
Swap with last element (assuming their values are different).
Reduce the size of the unsorted portion of the array by 1.
Example 2
Bubble Sort
Bubble sort examines the array from start to finish, comparing
elements as it goes.
Any time it finds a larger element before a smaller element, it swaps
the two.
In this way, the larger elements are passed towards the end.
The largest element of the array therefore "bubbles" to the end of the
array.
Then it repeats the process for the unsorted portion of the array until
the whole array is sorted.
Bubble sort works on the same general principle as shaking a soft
drink bottle.
Right after shaking, the contents are a mixture of bubbles and soft
drink, distributed randomly.
Because bubbles are lighter than the soft drink, they rise to the surface,
displacing the soft drink downwards.
This is how bubble sort got its name, because the smaller elements
"float" to the top, while the larger elements "sink" to the bottom.
Algorithm
Define the entire array as the unsorted portion of the array.
While the unsorted portion of the array has more than one element:
1. For every element in the unsorted portion, swap with the next
neighbor if it is larger than the neighbor.
2. Reduce the size of the unsorted portion of the array by 1.
Quicksort
A devide and conquer approach that uses recursion.
1. If the list has 0 or 1 elements it is sorted
2. otherwise, pick any element p in the list.
value
3. Partition the list minus the pivot into two sub lists according to values
less than or greater than the pivot. (equal values go to either)
4. return the quicksort of the first list followed by the quicksort of the
second list
Quicksort in Action
39 23 17 90 33 72 46 79 11 52 64 5 71
Pick middle element as pivot: 46
Partition list
23 17 5 33 39 11 46 79 72 52 64 90 71
quick sort the less than list
Pick middle element as pivot: 33
23 17 5 11
33
39
middle
element
as
pivot:
and so on.
When implementing
one temporary array
is used instead of
multiple temporary
arrays.
Summerily
Language libraries often have sorting algorithms in them
Java Arrays and Collections classes
17
Searching Algorithm
Given a list of data find the location of a particular value or report that
value is not present
linear search
intuitive approach
start at first item
is it the one I am looking for?
if not go to next item
repeat until found or all items checked
If items not sorted or unsortable this approach is necessary
Searching in a Sorted List
If items are sorted then we can divide and conquer
dividing your work in half with each step
generally a good thing
The Binary Search on List in Ascending order
Start at middle of list