Skip to content

Latest commit

 

History

History
46 lines (32 loc) · 1.7 KB

README.md

File metadata and controls

46 lines (32 loc) · 1.7 KB

Merge Sort

en pt-br

Merge sort is a popular and efficient sorting algorithm that follows the divide-and-conquer paradigm. It is a comparison-based algorithm that uses recursion to sort a list of elements.

The algorithm divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge operation is the key process that assumes that the two halves are sorted and merges them to produce a single sorted sub-list.

Algorithm Steps

  1. Divide the unsorted array into two halves, and call the merge sort function for each half, until each half contains only one element.
  2. Merge the two sorted halves. 2.1. Create a temporary array to store the sorted elements. 2.2. Compare the first element of each half. 2.3. Add the smallest element to the temporary array. 2.4. Repeat the process until all elements are sorted. 2.5. Copy the sorted elements from the temporary array to the original array.
  3. Repeat the process until the entire array is sorted.

Source code

Time Complexity

Case Complexity
Worst O(n log n)
Average O(n log n)
Best O(n log n)

Space Complexity

Case Complexity
Worst O(n)
Average O(n)
Best O(n)

Stability

The algorithm is stable, as it preserves the order of equal elements.

Previous section (Insertion Sort)
Next section (Quick Sort)

Return to main page