Lecture Four
Examples of O( )
T(N) = O (N log N)
2
Examples of O( )
3
Sorting
Sorting is a classic subject in computer science.
There are three reasons for studying sorting
algorithms.
1- Sorting algorithms illustrate many creative
approaches to problem solving and these approaches
can be applied to solve other problems.
2- Sorting algorithms are good for practicing
fundamental programming techniques using selection
statements, loops, functions, and arrays.
3- Sorting algorithms are excellent examples to
demonstrate algorithm performance.
4
Sorting Algorithms
Selection Sort
Bubble Sort
Merge Sort
Quick Sort
Insertion Sort
5
Selection Sort
The main idea of selection sort is to scan the entire
given list to find its smallest element and exchange it
with the first element, putting the smallest element in
its final position in the sorted list.
Then we scan the list, starting with the second element,
to find the smallest among the last n − 1 elements and
exchange it with the second element, putting the second
smallest element in its final position.
6
Selection Sort
Generally, on the ith pass through the list, which we
number from 0 to n − 2, the algorithm searches for the
smallest item among the last n − I elements and swaps it
with Ai : After n- 1 passes, the list is sorted.
The mechanism:
7
Selection Sort
8
Selection Sort
9
Selection Sort
10
Bubble Sort
The bubble sort algorithm makes several passes
through the array. On each pass, successive
neighboring pairs are compared. If a pair is in
decreasing order, its values are swapped; otherwise,
the values remain unchanged.
This is called a bubble sort or sinking sort because
the smaller values gradually "bubble" their way to
the top and the larger values sink to the bottom.
11
Bubble Sort
12
Bubble Sort
13
Bubble Sort
14
Merge Sort
Merge sort is a perfect example of a successful
application of the divide-and conquer technique.
The algorithm divides the array A[0 ... n - 1] into
two halves A[0 … n / 2 - 1] and A[ n / 2 …n -
1],
And applies merge sort on each half recursively.
After the two halves are sorted, merge them.
15
Merge Sort
16
Merge Sort
The merging of two sorted arrays can be done as follows . Two
pointers (array indices) are initialized to point to the first elements
of the arrays being merged.
The elements pointed to are compared, and the smaller of them is
added to a new array being constructed; after that, the index of
the smaller element is incremented to point to its immediate
successor in the array it was copied from. This operation is
repeated until one of the two given arrays is exhausted, and then
the remaining elements of the other array are copied to the end of
the new array.
17
Merge Sort
18
Merge Sort
Merge sort example
The following figure illustrates a merge sort of an array of
eight elements (8 3 2 9 7 1 5 4).
The original array is split into (8 3 2 9) and (7 1 5 4). Apply
merge sort on this two subarrays recursively to split (8 3 2
9) into (8 3) and (2 9) and (7 1 5 4) into (7 1) and (5 4).
This process continues until the subarray contains only one
element.
19
Merge Sort
20
Merge Sort Time
Let T(n) denote the time required for sorting an
array of n elements using merge sort.
The merge sort algorithm splits the array into two
subarrays, sorts the subarrays using the same
algorithm recursively, and then merges the
subarrays. So,
21
Merge Sort Time
The first T(n/2) is the time for sorting the first half of the
array, and the second T(n/2) is the time for sorting the
second half.
To merge two subarrays, it takes at most n -1 comparisons
to compare the elements from the two subarrays and n
moves to move elements to the temporary array.
So, the total time is 2n-1 of merge process.
22
Merge Sort Time
23
Merge Sort Time
O (n log n)
24
Thanks
25