How can I implement merge sort in different programming languages?
You can implement merge sort in languages like Python, Java, and C++. Would you like examples in a specific language?
Yes, please show me the implementation in Python.
Here's a simple implementation of merge sort in Python:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2 # Finding the mid of the array
L = arr[:mid] # Dividing the elements into 2 halves
R = arr[mid:]
merge_sort(L) # Sorting the first half
merge_sort(R) # Sorting the second half
i = j = k = 0
# Copy data to temp arrays L[] and R[]
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Checking if any element was left
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
# Example usage:
arr = [38, 27, 43, 3, 9, 82, 10]
merge_sort(arr)
print(arr) # Output: [3, 9, 10, 27, 38, 43, 82]
Can you provide the same implementation in Java?
Sure! Here's how you can implement merge sort in Java:
public class MergeSort {
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public static void main(String args[]) {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = arr.length;
MergeSort ob = new MergeSort();
ob.sort(arr, 0, n - 1);
System.out.println("Sorted array");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
Get this answer and more from ChatGPT in Telegram, try it for free!
Get Started For FreePowered by advanced language models trained on billions of online words and conversations, MagicBuddy delivers friendly and helpful chat on demand.
Ask questions and get answers. Crack jokes and get laughs. Or even get writing ideas and study help. MagicBuddy's conversational intelligence allows for a truly unique and useful chat experience.
Curious? Try these prompts:
MagicBuddy is incredibly easy to use and the responses are lightning fast! 🥇
Impressive! MagicBuddy is my go-to AI assistant. It’s quick, accurate, and user-friendly.
Sending voice messages makes it easier to use in the middle of the day and the answers are super fast.
Love it! A friend of mine told me about it and it has made my life so much easier. It’s like having a helpful friend in my pocket!
I use 2 months already, simply amazing! MagicBuddy delivers answers in a snap. It’s a must-have tool for staying informed!
Game-changer! MagicBuddy’s speed and accuracy are unmatched. It’s my secret weapon for productivity
Say Hello to Your New Telegram BFF - Try MagicBuddy Free!
Get Started For Free