0% found this document useful (0 votes)
20 views

Array 8

This document discusses arrays and merge sort algorithms. It provides an overview of arrays, merging two arrays, and the merge sort algorithm. It explains how merge sort works by dividing the array in half and recursively sorting and merging the sub-arrays. The time complexity of merge sort is analyzed as O(nlogn) in the best, average, and worst cases. Space complexity is O(n). References for further reading on data structures and algorithms are also provided.

Uploaded by

manudev8924
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Array 8

This document discusses arrays and merge sort algorithms. It provides an overview of arrays, merging two arrays, and the merge sort algorithm. It explains how merge sort works by dividing the array in half and recursively sorting and merging the sub-arrays. The time complexity of merge sort is analyzed as O(nlogn) in the best, average, and worst cases. Space complexity is O(n). References for further reading on data structures and algorithms are also provided.

Uploaded by

manudev8924
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: CSH-231
Chapter: Array
Subject Coordinator:
Mr. Vishal Kumar
(E12820)

Array DISCOVER . LEARN .


Lecture No. 1.8 EMPOWER
Course Objective

• To develop understanding among the students


about the concept of the data structures
• To demonstrate methods to perform operations on
different data structures
• To analyze the various operations for complexity

Apex Institute of Technology 2


COURSE OUTCOMES

• CO1: Identify how arrays, linked lists, stacks,


queues, trees, and graphs are represented in
memory and used by algorithms
• CO4: Explain the computational efficiency of the
principal algorithms for sorting, searching
Index
• Merging arrays
• Merge Sort
• Complexity Analysis
Merging Arrays

Merging two arrays means combining two separate


arrays into one single array. For instance, if the first
array consists of 3 elements and the second array
consists of 5 elements then the resulting array
consists of 8 elements. This resulting array is known
as a merged array.
Below is the pictorial representation of the merged
array.
Continue..
Algorithm
1. Start
2. Declare two arrays.
3. Initialize these two arrays.
4. Declare another array that will store the merged arrays.
5. The size of the merged array should be equal to the sum of the other two arrays.
6. For loop will help to iterate every element present in the first array.
7. Condition inside the for loop (i < Size) will ensure the compiler, not exceed the
array limit.
8. Inside the second for loop assign each and every array element to the Merged
array.
9. Now, print the merged array.
10. Stop
Merge Sort

• Merge sort is similar to the quick sort algorithm as it


uses the divide and conquer approach to sort the
elements. It is one of the most popular and efficient
sorting algorithm. It divides the given list into two equal
halves, calls itself for the two halves and then merges
the two sorted halves.
• The sub-lists are divided again and again into halves
until the list cannot be divided further. Then we
combine the pair of one element lists into two-element
lists, sorting them in the process. The sorted two-
element pairs is merged into the four-element lists, and
so on until we get the sorted list.
Continue..

Note: The merge() function is used for merging two halves


Continue..
Algorithm for mergsort function:
mergesort(array, left, right)
if left < right
set mid = (left + right)/2
mergesort(array, left, right)
mergesort(arr, mid + 1, right)
MERGE (array, left, mid, right)
end of if
END mergesort
• Example
Continue..
Complexity Analysis

• Now, let's see the time complexity of merge sort in


best case, average case, and in worst case. We will
also see the space complexity of the merge sort.
1. Time Complexity

Case Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n*logn)
Continue..
• Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of merge sort
is O(n*logn).
• Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of merge sort is O(n*logn).
• Worst Case Complexity - It occurs when the array elements are required
to be sorted in reverse order. That means suppose you have to sort the
array elements in ascending order, but its elements are in descending
order. The worst-case time complexity of merge sort is O(n*logn).
2. Space Complexity
The space complexity of merge sort is O(n). It is because, in merge sort,
an extra variable is required for swapping.
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

For queries
Email: [email protected]

15

You might also like