• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 19 pages
Exam (elaborations)

Exam (elaborations) data structures dsa

Document preview thumbnail
Preview 3 out of 19 pages

Providing full concept related to Merge sort from Scratch to Master DataStructure Tutorial Notes This is third Main concept Explanatied fully from scratch to end of Merge Sort

Content preview

1.Merge Sort Algorithm Overview
Merge Sort is a Divide and Conquer algorithm for sorting
an array.

Divide and Conquer Technique in Merge
Sort
InMerge Sort, the array is divided into two halves, sorted
separately and then merged. This process is repeated for
each half until there are no more elements to divide.

Merge Function in Merge Sort
The merge function is a crucial part of Merge Sort. It takes
two sorted arrays as input, merges them and returns a
single array which is also sorted.

Merging Sorted Sublists in Merge Sort
The Merge function works on the principle of merging two
sorted sublists. It iterates through each sublist and
compares the smallest elements. The smallest element is
then added to the output array and the process is
continued until one sublist is empty.

Handling Remaining Elements in Sublists
Once one of the sublists is empty, the remaining elements
in the other sublist are added to the output array in the
same order.

Copying Sorted Elements to Original Array
After the two halves have been sorted and merged, the
sorted elements are copied back to the original array.

,Time Complexity of Merge Function
The time complexity of the Merge function is O(n), where
n is the number of elements in the input array.

Time Complexity of Merge Sort
The time complexity of Merge Sort is O(n log n) due to the
divide and conquer nature of the algorithm.

Merge Function Logic and Implementation
Here's a sample implementation of the Merge Function:

void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

// create temp arrays
int L[n1], R[n2];

// Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];




// Merge the temp arrays back into arr[l..r]

, i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

// Copy the remaining elements of L[], if
there are any
while (i < n1)
{




arr[k] = L[i];

Document information

Uploaded on
December 19, 2024
Number of pages
19
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$10.09

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

Sold
0
Followers
0
Items
1
Last sold
-



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