Merge Sortppt 1
Merge Sortppt 1
ROLL.NO Name
20 Ritu Raj
60 Tarun Kant
2.1 Merge
2.2 Merge_Sort
2.3 Main
2.4 <stdio.h>
2.5 <stdlib.h>
Chapter 3 Implementation
Chapter 4 Flowchart
Chapter 5 Results
Introduction to C Programming
What is C?
C was developed in the early 1970s by Dennis Ritchie at Bell Labs. It’s known for its efficiency and control, which makes it a popular choice for system programming,
embedded systems, and high-performance applications.
Key Features of C:
1.Simplicity: C is straightforward and easy to understand, making it a great language for beginners.
2.Efficiency: It's known for its performance and efficiency, which is why it's often used in system programming.
3.Portability: C programs can run on various computer systems with little or no modification.
4.Flexibility: C offers a rich set of built-in functions and operators, allowing for a wide range of programming tasks.
5.Low-Level Access: C provides access to low-level memory using pointers, making it suitable for system-level programming.
Key Concepts in C:
•Variables and Data Types: Understand different data types like int, float, char, and how to declare variables.
•Control Structures: Learn about conditional statements (if, else, switch) and loops (for, while, do-while).
•Functions: Writing reusable code blocks with functions.
•Pointers: Understanding memory addresses and pointer manipulation.
•Structures: Creating custom data types with struct.
•Arrays and Strings: Working with collections of data and character sequences.
Applications of C Programming
1.System Programming: Used for developing operating systems and embedded systems due to its efficiency and low-level access.
2.Game Development: Essential for game engines and real-time systems.
3.Compilers and Interpreters: Many language compilers and interpreters are written in C.
4.Database Systems: Core components of database management systems, like MySQL, are written in C.
5.Network Programming: Implementing networking protocols and client-server applications.
Merge Sort is a popular and efficient sorting algorithm based on the divide-and-conquer paradigm. It was invented by John von Neumann in 1945 and is widely used
due to its stability and predictable performance. Merge Sort divides the input array into two halves, recursively sorts them, and then merges the two sorted halves to
produce the final sorted array.
1.Divide and Conquer: Merge Sort breaks down the array into smaller subarrays until each subarray contains a single element. It then merges these subarrays in a way
that results in a sorted array.
2.Stable Sorting: Merge Sort preserves the order of equal elements, making it a stable sorting algorithm.
3.Consistent Performance: Merge Sort has a time complexity of O(n log n) in the best, worst, and average cases, providing consistent performance regardless of the
initial order of elements.
4.Non-In-Place Sorting: Merge Sort requires additional space proportional to the size of the array, as it uses temporary arrays for merging.
•Large Data Sets: Merge Sort is ideal for sorting large data sets due to its consistent O(n log n) time complexity.
•External Sorting: It is often used in external sorting algorithms where data is too large to fit into memory, as it can efficiently sort data stored on disk.
•Stable Sorting Requirements: When stability is a crucial requirement, Merge Sort is preferred over other algorithms like Quick Sort.
•Understand Merge Sort Algorithm: Gain a thorough understanding of the Merge Sort algorithm, its principles, and its divide-and-conquer approach.
•Implement Merge Sort in C: Write and test the C code to implement the Merge Sort algorithm effectively.
•Analyze Time and Space Complexity: Analyze and understand the time and space complexity of Merge Sort and compare it with other sorting algorithms.
•Explore Applications: Investigate real-world applications of Merge Sort and understand its relevance in various domains.
•Ensure Stability: Demonstrate the stability of Merge Sort and understand its importance in sorting algorithms.
•Handle Large Data Sets: Learn how to handle and sort large data sets efficiently using Merge Sort.
•Develop Problem-Solving Skills: Enhance your problem-solving and coding skills by tackling common issues and edge cases in sorting
Functions (Methods):
1.Merge:
•Purpose: Merges two subarrays of arr[]. The first subarray is arr[left..mid] and the second subarray is arr[mid+1..right].
•Parameters:
•arr[]: The array to be sorted.
•left: The starting index of the first subarray.
•mid: The ending index of the first subarray and the starting index of the second subarray.
•right: The ending index of the second subarray.
Process:
•Create temporary arrays Left[] and Right[] to hold the data.
•Copy data to temporary arrays.
•Merge the temporary arrays back into arr[left..right].
•Copy the remaining elements of Left[], if any.
•Copy the remaining elements of Right[], if any.
Merge_Sort:
•Purpose: Recursively divides and sorts the array using the Merge Sort algorithm.
•Parameters:
•arr[]: The array to be sorted.
•left: The starting index of the subarray to be sorted.
•right: The ending index of the subarray to be sorted.
•Process:
•Find the midpoint mid.
•Recursively call Merge_Sort for the first half and second half of the array.
•Call Merge to merge the sorted halves.
main:
•Purpose: Entry point of the program.
•Process:
•Get the size of the array from the user.
•Initialize the array with the given size.
•Get the elements of the array from the user.
•Call Merge_Sort to sort the array.
•Print the sorted array.
Modules:
•#include <stdio.h>:
•Provides functionalities for input and output operations, such as printf and scanf.
•#include <stdlib.h>:
•Provides functionalities for memory allocation, process control, conversions, and others. In this code, it is included but not explicitly used.
Algorithm
firstHalf: [53, 24, 35, 76] becomes [53, 24] and [35, 76]
Step 3: Continue dividing (splitting) the arrays until each sub-array is left only with a single
element:
Step 5: Merge the final two sub-arrays to get the sorted array:
Merge [24, 53] and [35, 76]
Pseudocode
Let’s see the pseudocode for the implementation of merge sort using a recursive approach.