This repository contains a Python implementation of the Merge Sort algorithm. Merge Sort is a divide-and-conquer sorting algorithm that divides an input array into two halves, recursively sorts each half, and then merges the sorted halves.
Merge Sort is a comparison-based sorting algorithm with a time complexity of O(n log n). It is known for its stable nature and can efficiently handle large datasets.
This implementation consists of several functions:
mergeFunction
: Merges two sorted arrays into a single sorted array.sortLeftArray
: Recursively sorts the left half of the input array.sortRightArray
: Recursively sorts the right half of the input array.mergeSort
: Merges two sorted arrays into a single sorted array.
To use the Merge Sort algorithm, follow these steps:
- Clone this repository:
git clone https://github.com/excel-asaph/Merge-Sort-Algorithm-Implementation.git
cd Merge-Sort-Algorithm-Implementation
- Open the Python interpreter or create a Python script in the same directory:
vi your-filename.py
- Then, use the following code:
from merge_sort import mergeSort
# Example input array
user_array = [your_array_of_values]
# Sort the array using Merge Sort
user_array = mergeSort(user_array)
# Print the sorted array
print("Sorted Array:", user_array)
- Run the Python script:
python3 your-filename.py
Merges two sorted arrays into a single sorted array.
-
Arguments:
array
(list): The input list to be sorted.
-
Returns:
list
: A sorted list.
Recursively sorts the left half of the input array.
-
Arguments:
array
(list): The input list to be sorted.
-
Returns:
list
: A sorted list.
Recursively sorts the right half of the input array.
-
Arguments:
array
(list): The input list to be sorted.
-
Returns:
list
: A sorted list.
Merges two sorted arrays into a single sorted array.
-
Arguments:
value1
(list): The first sorted list.value2
(list): The second sorted list.
-
Returns:
list
: A merged and sorted list.
The Merge Sort algorithm has a time complexity of O(n log n), making it efficient for large datasets. It performs well on both small and large arrays, and its stable nature makes it suitable for scenarios where maintaining the order of equal elements is essential.
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/new-feature
) - Make your changes and commit them (
git commit -am 'Add new feature'
) - Push to the branch (
git push origin feature/new-feature
) - Create a pull request
This project is licensed under the MIT License - see the LICENSE file for details.