Thread: interview prospective

Forum : Collections   09-12-2017 07:21:56 PM
User Image
ArifKhan

Newbie

Joined: Jun 25, 2017

Points: 175

Threads: 115

Posts: 93

Answer Icon interview prospective

| Quote Date : Dec 9, 2017    Views:568    

What is time complexity? If you are going to implement  sorting by your own which  sorting you prefer? and why??

Comments

User Image
Brijesh

Newbie

Joined : Jun 29, 2017

Points : 100

Threads: 0

Posts: 5

Re: interview prospective

Reply Date : Jan 14, 2018

What ever AMRIT  say is correct but time complexity even depends on the devices on with our application are running i.e  processer, latency.. etc. 

 And I will choose merge sort because it more stable, as we know quick sort  performance better then n log(n)  but it will not guarantee.  Where as merge sort give performance of n log(n),but it give guarantee, and more stable. 
Even our collection framework use merge sort  because of the same reason.  
I hope this will help you if any argument most welcome. 


User Image
Amritk

Newbie

Joined : May 8, 2017

Points : 220

Threads: 56

Posts: 209

Answer IconRe: interview prospective

Reply Date : Dec 27, 2017

Time complexity is discribe the total amount of time taken by an algorithm to run as a function of the length
of the input.

Sometimes, there are more than one way to solve a problem. We need to learn how to compare the
performance different algorithms and choose the best one to solve a particular problem. While analyzing an
algorithm, we mostly consider time complexity and space complexity. Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. Similarly, Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input.


Consider below example to understand time complexity..

Suppose we have an array A and an integer x and we have to find if x exists in array A.

for this we need to traverse the whole array A and check if the any element is equal to x. or not.

for i : 1 to length of A

    if A[i] is equal to x
        return TRUE
return FALSE

Here each iteration will take constant time to execute the statement. In worst case if element x is not found
then total time is N, where N is the total length of the array A.

So here time complexity depends on the number of iteration here is order of n means O(n).

Similarly while sorting, we will check first what is the requirement and for which purpose we are sorting. If huge elements are there then we will prefer merge or quick sorting. ?

Load More

No More Comments