1
78kviews
Write an algorithm to sort set of numbers in ascending order. For above problem in which cases time complexity is calculated.
2
3.1kviews

Algorithm:

Consider we are sorting three numbers in ascending order.

Steps:

• Start.

• Accept three numbers from user (a, b, c).

• If a < b then goto step 4 else goto step 8.

• If a < c then goto step 5 else goto step 7.

• If b < c then goto step 9 else goto step 6.

• Interchange b and c and goto step 9.

• Interchange a and c and goto step 3.

• Interchange a and b and goto step 3.

• Display “Ascending order”.

• Display a, b, c.

• Stop.

Time Complexity:

Time Complexity helps in determining the amount of time needed for algorithm to generate the solution for any problem instance.

Worst Case Condition:

It is the maximum number of steps that an algorithm can take for any collection of data values. Example: If there are some numbers to be arranged in ascending order and they are given as input in exactly descending order, then the time required for implementing the algorithm for this case of input is called as the worst case condition.

Average Case Condition:

The efficiency averaged on all possible inputs. Example: For sorting the numbers in ascending order, in this case some of the input data is already sorted, while some requires to be sorting.

Best Case Condition: It is the minimum number of steps that an algorithm can take any collection of data values. Example: In this case if all the input data is already sorted then the time required to execute the implementation of the algorithm will be minimum and hence is considered as the best case condition