Skip to content

Efficient Python implementation of Merge Sort, a stable, divide-and-conquer sorting algorithm with O(n log n) time complexity. This repository includes well-documented code for splitting arrays, recursively sorting subarrays, and merging them back. Ideal for understanding core sorting techniques and comparing with other algorithms.

Notifications You must be signed in to change notification settings

samandar-hamrayev/geeksforgeeks-merge-sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

geeksforgeeks-merge-sort

Here’s a simple README.md file for the geeksforgeeks-merge-sort GitHub repository:

Merge Sort Algorithm

This repository provides a Python implementation of the Merge Sort algorithm, a powerful, stable sorting technique with a time complexity of O(n log n). Merge Sort is ideal for sorting large datasets where consistent, efficient sorting is needed. The code is well-documented for easy understanding.

Table of Contents

Introduction

Merge Sort is a divide-and-conquer algorithm that splits an array into halves, recursively sorts each half, and then merges them back into a single sorted array. It consistently performs in O(n log n) time, making it one of the fastest general-purpose sorting algorithms.

How Merge Sort Works

  1. Divide: Split the array into two halves until each subarray contains a single element.
  2. Conquer: Recursively sort each half.
  3. Combine: Merge the sorted halves back into one sorted array.

Code

Here's the main Merge Sort function:

def merge_sort(arr):
    if len(arr) > 1:
        mid = len(arr) // 2
        left_half = arr[:mid]
        right_half = arr[mid:]

        merge_sort(left_half)
        merge_sort(right_half)

        i = j = k = 0
        while i < len(left_half) and j < len(right_half):
            if left_half[i] < right_half[j]:
                arr[k] = left_half[i]
                i += 1
            else:
                arr[k] = right_half[j]
                j += 1
            k += 1

        while i < len(left_half):
            arr[k] = left_half[i]
            i += 1
            k += 1

        while j < len(right_half):
            arr[k] = right_half[j]
            j += 1
            k += 1

    return arr

Usage

To use this code, simply import or paste the merge_sort function into your project, and call it with an unsorted array.

Example:

arr = [34, 7, 23, 32, 5, 62]
sorted_arr = merge_sort(arr)
print("Sorted array:", sorted_arr)

Advantages of Merge SortEfficiency: Consistently performs in O(n log n) time.
	•	Stability: Maintains the relative order of equal elements.
	•	Predictable Performance: Suitable for large datasets without degrading to O(n²).


Contributing

Feel free to fork this repository, submit issues, or make pull requests to improve the implementation or documentation.

About

Efficient Python implementation of Merge Sort, a stable, divide-and-conquer sorting algorithm with O(n log n) time complexity. This repository includes well-documented code for splitting arrays, recursively sorting subarrays, and merging them back. Ideal for understanding core sorting techniques and comparing with other algorithms.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages