Epic Systems Interview Question
Software Engineer / Developersspace complexity:
Quick = O(1)
Bubble = O(1)
Insertion = O(1)
Merge = O(n)
Time complexities:
Quick sort: Best O(n), Average: O(nlogn), Worst case: O(n^2)
Insertion sort: Best O(n), Worst Case: O(n^2)
Bubble sort: O(n^2) (best/worst)
Merge Sort: O(nlogn)(best/worst)
So quick sort is the best algorithms, considering its time and space complexity.
space complexity of Quick sort on array is O(1).
Because at each recursive call one element position will be fixed (with only O(1) extra memory for swapping elements). there will be 2 recursive calls one on the left part and one on the right part. We can use the same original array for the recursive calls also.
So How can it take O(logn) Memory. Can you explain.
I second Anji. Immaterial of the number of recursive calls in a quicksort, all of them act on the same original array, fixing the position of one particular element {pivot/split value} per call.So its in place, even with a recursive implementation. In fact there is no need of a scratch area, at all. That's not the case with merge sort. It does not sort in place.
import java.io.*;
import java.util.*;
class replace
{
public static void main(String [] args)
{
String totest[];
int i;
String test="She is a Hot girl";
String newString="";
StringTokenizer st= new StringTokenizer(test);
while(st.hasMoreTokens())
{
String temp=st.nextToken();
if( temp.compareTo("a")==0)
newString=newString+"the ";
else
newString= newString+ temp + " ";
}
System.out.println(newString);
}
}
import java.io.*;
import java.util.*;
class replace
{
public static void main(String [] args)
{
String totest[];
int i;
String test="She is a Hot girl";
String newString="";
StringTokenizer st= new StringTokenizer(test);
while(st.hasMoreTokens())
{
String temp=st.nextToken();
if( temp.compareTo("a")==0)
newString=newString+"the ";
else
newString= newString+ temp + " ";
}
System.out.println(newString);
}
}
it depends on the context.... Merge is the best.... what is this binary search sorting algorithm??
Never heard of it..
Merge is definitely not the best for memory efficient.
Quick = O(logn)
Bubble = O(1)
Insertion = O(1)
Merge = O(n)
for Binary search, if counting the result storage, O(n)
So bubble and insertion are best.
I think bubble, insertion sort and quick sort are most **memory** efficient. And the storage complexity is not O(1). It's O(n). How can it be O(1)? Surely they are in place algorithms. But do you think a 5 element input array will occupy the same storage space as 50 element input array? If no then how the storage space is O(1). It depends on the input size. merge sort takes more space in addition to O(n) of input size.
Space Complexity:
- eastflag163 October 27, 2013QuickSort: O(logn)~O(n)
Bubble: O(1)
Insertion: O(1)
Merge: O(n)
BS: O(n)