Medium Algorithms Sorting Algorithms
Implement merge sort and explain the merge operation.
Answer
Divide array into halves recursively until single elements. Merge combines two sorted halves: use two pointers, compare elements at each pointer, copy smaller to result array, advance that pointer. Continue until one half exhausted, then copy remaining. Key insight: merging two sorted arrays is O(n). Total complexity: T(n) = 2T(n/2) + O(n) = O(n log n) by Master theorem. Space O(n) for merge buffer.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Algorithm Developer