In: Computer Science
Explain: Analysis of algorithms with the example.
Explain with example: How computational time of an algorithm depends on
input size?
Write an algorithm (using whatever method you prefer) that takes an input of 10 integers, sorts the integers in ascending order and out puts the sorted list.
In relation to computational time of your algorithm for part 4, what arrangement of the input numbers will cause the best case computational time? What arrangement will cause the worst case time?
1)
Analysis of algorithms is the determination of the amount of time and space resources required to execute it. Usually, the efficiency or running time of an algorithm is stated as a function relating the input length to the number of steps, known as time complexity, or volume of memory, known as space complexity.
Example :-
for (int i = 0; i < a.length; i++) { System.out.println(a[i]); }
Here n=a.length (provided we know that all of the items in the array have a fixed size, which is often the case). We have
System.out.println
so we write T(n)=4n+1.
All operations are not created equal. The printing completely overwhelms the increments, compares and indexing operations. We might as well talk about having nn compare-index-print-increment operations. Then T(n)=n+1. While we’re at it, we might as well realize that initialization doesn’t mean much, so we’re safe to write T(n)=n.
2)
A “computational problem” is a function whose:
Let f(n) be some function (not to be confused with the function the algorithm is computing) of the input size n (and not the input itself).
For a given algorithm for any computational problem, following are the two important ways of classifying its (worst-case) time complexity.
3)
Algorithm Bubble sort
We assume list is an array of 10 elements. We further assume that swap function swaps the values of the given array elements.
begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort
If an array is already in sorted order, and bubble sort makes no swaps, the algorithm can terminate after one pass.O(n) is the best-case running time for bubble sort. Bubble sort has a worst-case and average complexity of О(n2), where n is the number of items being sorted.