Anda di halaman 1dari 8

Course Title:Data structures and Algorithm

Course Code: FPS 109


Course Instructor: Mr Bessong Rawlings(IT Eng)

Algorithm
Sorting Algorithm
In computer science, a sorting algorithm is an algorithm that puts
elements

of

list

in

certain

order.

The

most-used orders are numerical order and lexicographical order. Efficient


sorting

is

important

for

optimizing

the

use

of other algorithms (such as search and merge algorithms) that require


sorted lists to work correctly
To arrange a set of items in sequence or in a particularly weldefined
order. It is estimated that 25~50% of all computing power is used for
sorting activities.
Possible reasons:
Many applications require sorting;
Many applications perform sorting when they don't have to;
Many applications use inefficient sorting algorithms.

Where Sorting Can Be Applied


To prepare a list of student ID, names, and scores in a table (sorted by
ID or name) for easy checking.

To prepare a list of scores before letter grade assignment.


To produce a list of horses after a race (sorted by the finishing times)
for payoff calculation.

To prepare an originally unsorted array for ordered binary searching.

Some Sorting Methods


Selection sort
Bubble sort
Shell sort (a simple but faster sorting method
Quick sort (a very efficient sorting method for most applications)

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.

It is similar to the way that many people do their sorting.


Algorithm
1. Define the entire array as the unsorted portion of the array

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.

This is called the pivot

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

quicksort the less than list, pivot now 5


{} 5 23 17 11

quicksort the less than list, base case


quicksort the greater than list
Pick

middle

element

as

pivot:

and so on.

Merge Sort Algorithm


If a list has 1 element or 0 elements it is sorted
If a list has more than 2 split into into 2 separate lists
Perform this algorithm on each of those smaller lists
Take the 2 sorted lists and merge them together

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

C++ Standard Template Library


Python sort and sorted functions
Hybrid sorts
when size of unsorted list or portion of array is small use insertion
sort,otherwise use O(N log N) sort like Quicksort of Mergesort

Many other sorting algorithms exist.

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

is that the item?


If not is it less than or greater than the item?
less than, move to second half of list
greater than, move to first half of list
repeat until found or sub list size = 0

Anda mungkin juga menyukai