• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 3 fuera de 19 páginas
Examen

Exam (elaborations) data structures dsa

Document preview thumbnail
Vista previa 3 fuera de 19 páginas

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

Vista previa del contenido

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];

Información del documento

Subido en
19 de diciembre de 2024
Número de páginas
19
Escrito en
2024/2025
Tipo
Examen
Contiene
Preguntas y respuestas
$10.09

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
1
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes