Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Data structures quick Sorting

Rating
-
Sold
-
Pages
5
Uploaded on
04-06-2024
Written in
2023/2024

Best full explanation of quick Sorting

Institution
Course

Content preview

Merge Sorting notes By Shanth…

# Merge Sort: Detailed Explanation and Example Programs

## Overview
Merge sort is a classic divide-and-conquer sorting algorithm known for its efficiency and stability.
It works by recursively dividing the array into smaller subarrays until each subarray contains
only one element, then merging these subarrays back together in a sorted order.

### Steps of Merge Sort:
1. **Divide**: Recursively divide the array into two halves until each subarray contains only one
element.
2. **Conquer**: Sort each subarray.
3. **Merge**: Merge the sorted subarrays back together to form the final sorted array.

### Example:
Let's sort the array `[38, 27, 43, 10]` using merge sort:

1. **Divide**:
- `[38, 27, 43, 10]` is divided into `[38, 27]` and `[43, 10]`.
- Further division: `[38]` and `[27]`, `[43]` and `[10]`.

2. **Conquer**:
- All single-element subarrays are considered sorted.

3. **Merge**:
- Merge `[38]` and `[27]` to get `[27, 38]`.
- Merge `[43]` and `[10]` to get `[10, 43]`.
- Finally, merge `[27, 38]` and `[10, 43]` to obtain the sorted array `[10, 27, 38, 43]`.

Thus, the sorted array is `[10, 27, 38, 43]`.

---

## Merge Sort Implementation in C++

```cpp
#include <iostream>
using namespace std;

void merge(int array[], int const left, int const mid, int const right) {
int n1 = mid - left + 1;
int n2 = right - mid;

, int L[n1], R[n2];

for (int i = 0; i < n1; i++)
L[i] = array[left + i];
for (int j = 0; j < n2; j++)
R[j] = array[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;
}

while (i < n1) {
array[k] = L[i];
i++;
k++;
}

while (j < n2) {
array[k] = R[j];
j++;
k++;
}
}

void mergeSort(int array[], int const begin, int const end) {
if (begin >= end)
return;

int mid = begin + (end - begin) / 2;
mergeSort(array, begin, mid);
mergeSort(array, mid + 1, end);
merge(array, begin, mid, end);
}

int main() {
int arr[] = {38, 27, 43, 10};

Written for

Institution
Course

Document information

Uploaded on
June 4, 2024
Number of pages
5
Written in
2023/2024
Type
Class notes
Professor(s)
Ramakrishna
Contains
All classes

Subjects

$12.09
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sshanth

Get to know the seller

Seller avatar
sshanth National star
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions