0% found this document useful (0 votes)
4 views15 pages

Merge Sortppt 1

This document is a mini project report on Merge Sort submitted by students Ritu Raj and Tarun Kant under the guidance of Prof. Vidya.R at Visvesvaraya Technological University. It covers the introduction to C programming, the Merge Sort algorithm, its implementation, and applications, emphasizing its efficiency and stability in sorting large data sets. The report includes detailed explanations of the algorithm's steps, pseudocode, and the structure of a C program.

Uploaded by

rritu7568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Merge Sortppt 1

This document is a mini project report on Merge Sort submitted by students Ritu Raj and Tarun Kant under the guidance of Prof. Vidya.R at Visvesvaraya Technological University. It covers the introduction to C programming, the Merge Sort algorithm, its implementation, and applications, emphasizing its efficiency and stability in sorting large data sets. The report includes detailed explanations of the algorithm's steps, pseudocode, and the structure of a C program.

Uploaded by

rritu7568
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“Jnana Sangama”, Belagavi-590018, Karnataka

Principal of Programming in C (BPOPS103)


Mini Project Report
on
Merge Sort
Submitted by

ROLL.NO Name
20 Ritu Raj
60 Tarun Kant

Under the Guidance of


Prof [Vidya.R]
Assistant Professor
Department of Computer Science & Engineering, BIT
Bengaluru-560004
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
An Autonomous Intuition Under VTU
BANGALORE INSTITUTE OF TECHNOLOGY
K.R. Road, V.V. Pura, Bengaluru-560 004
2024-25

Abstract of the Project


Chapter 1 Introduction

1.1 Introduction of C Programming


1.2 Applications of C Programming
1.3 Introduction of project
1.4 Objective of the project

Chapter 2 Modules and Methods used

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.

Basic Structure of a C Program:

#include <stdio.h> // Preprocessor directive to include standard I/O library


int main() { // Main function where execution begins
printf("Hello, World!\n"); // Print statement to display "Hello, World!"
return 0; // Return statement to indicate successful execution
}

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Introduction to C 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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Introduction to Project

Introduction to Merge Sort

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.

Key Features of Merge Sort:

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.

Applications and Objectives of Merge Sort:

•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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Introduction to Project

•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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Module and Methods

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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Module and Methods

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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Implementation

Algorithm

Step 1: Divide the array into sub-arrays (two halves):

firstHalf: [53, 24, 35, 76], secondHalf: [12, 5, 3, 6, 23]

Step 2: Now recursively divide the sub-arrays:

firstHalf: [53, 24, 35, 76] becomes [53, 24] and [35, 76]

secondHalf: [12, 5, 3, 6, 23] becomes [12, 5] and [3, 6, 23]

Step 3: Continue dividing (splitting) the arrays until each sub-array is left only with a single
element:

[53, 24] becomes [53] and [24]


[35, 76] becomes [35] and [76]
[12, 5] becomes [12] and [5]
[3, 6, 23] becomes [3] and [6, 23]

[6, 23] becomes [6] and [23]

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Implementation

Step 4: Merge the sorted sub-arrays:

Merge [53] and [24] to get [24, 53]

Merge [35] and [76] to get [35, 76]

Merge [12] and [5] to get [5, 12]

Merge [6] and [23] to get [6, 23]

Step 5: Merge the final two sub-arrays to get the sorted array:
Merge [24, 53] and [35, 76]

Compare 24 and 35: Take 24


Compare 53 and 35: Take 35
Compare 53 and 76: Take 53
Remaining elements: 76, Output: [24, 35, 53, 76]

Merge [5, 12] and [3, 6, 23]

Compare 5 and 3: Take 3


Compare 5 and 6: Take 5
Compare 12 and 6: Take 6
Compare 12 and 23: Take 12

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Implementation

Remaining elements: 23, Output: [3, 5, 6, 12, 23]


Merge [24, 35, 53, 76] and [3, 5, 6, 12, 23]

Compare 24 and 3: Take 3


Compare 24 and 5: Take 5
Compare 24 and 6: Take 6
Compare 24 and 12: Take 12
Compare 24 and 23: Take 23

Remaining elements: 24, 35, 53, 76


The final array after sorting will be: [3, 5, 6, 12, 23, 24, 35, 53, 76]

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Implementation

Pseudocode
Let’s see the pseudocode for the implementation of merge sort using a recursive approach.

mergeSort(arr[], left, right)


If left < right
•Find the middle point to divide the array into two halves:
mid = (left + right) / 2
•Call MergeSort for the first half:
mergeSort(arr, left, mid)
•Call MergeSort for the second half:
mergeSort(arr, mid + 1, right)
•Merge the two halves sorted above:
mergeTwoHalves(arr, left, mid, right)

mergeTwoHalves(arr[], left, mid, right)


n1 = mid – left + 1
n2 = right – mid

Create temp arrays t1[n1] and t2[n2]


Copy data to temp arrays t1[] and t2[]

Merge the temp arrays back into arr[left..right]


Copy the remaining elements of t1[], if any
Copy the remaining elements of t2[], if any

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Implementation

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Flowchart

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY


Results

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING, BANGALORE INSTITUTE OF TECHNOLOGY

You might also like