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

Daa Miniproject 2 This Is The Mini Project For Daa

The document is a mini project report on implementing single-threaded and multi-threaded merge sort algorithms, submitted by Vaibhav Ugale for the Bachelor of Engineering degree at Savitribai Phule Pune University. It includes a problem statement, objectives, theoretical background on merge sort and multithreading, and a conclusion that single-threaded merge sort is faster than its multi-threaded counterpart. The report also contains acknowledgments, project code, and performance analysis of both sorting methods.

Uploaded by

vaidehi.zurvs21
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 views12 pages

Daa Miniproject 2 This Is The Mini Project For Daa

The document is a mini project report on implementing single-threaded and multi-threaded merge sort algorithms, submitted by Vaibhav Ugale for the Bachelor of Engineering degree at Savitribai Phule Pune University. It includes a problem statement, objectives, theoretical background on merge sort and multithreading, and a conclusion that single-threaded merge sort is faster than its multi-threaded counterpart. The report also contains acknowledgments, project code, and performance analysis of both sorting methods.

Uploaded by

vaidehi.zurvs21
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/ 12

lOMoARcPSD|47139209

Daa miniproject 2 - This is the mini project for daa

Computer Engineering (Savitribai Phule Pune University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by vaidehi dharmadhikari ([email protected])
lOMoARcPSD|47139209

A PRELIMENERY REPORT ON

Implement single threaded and multi threaded merge sort

SUBMITTED TO THE SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE


IN THE PARTIAL FULFILLMENT OF THE REQUIREMENTS
FOR THE AWARD OF THE DEGREE

OF

BACHELOR OF ENGINEERING (COMPUTER ENGINEERING)

SUBMITTED BY,

Vaibhav Ugale

DEPARTMENT OF COMPUTER ENGINEERING

STES’S SMT. KASHIBAINAVALECOLLEGE OF ENGINEERING

VADGAON BK, OFF SINHGAD ROAD, PUNE 411041

SAVITRIBAI PHULE PUNE UNIVERSITY 2023-2024

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

CERTIFICATE

This is to certify that the project report entitles

Implement single threaded and multi threaded merge sort


Submitted by

Vaibhav Ugale

Are bonafide students of this institute and the work has been carried out by them under the
supervision of Prof. Sanjay.P.Pingat and it is approved for the partial fulfillment of the
requirement of Savitribai Phule Pune University, for the award of the degree of Bachelor of
Engineering (Computer Engineering)

(Prof.Sanjay.P.Pingat) (Prof. R.H.Borhade )


Guide Head,
Department of Computer Engineering Department of Computer Engineering

(Dr. A. V. Deshpande) Principal,


Smt.Kashibai Navale College of Engineering Pune – 41

Place : Pune
Date:

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

ACKNOWLEDGEMENT

The present world of competition there is a race of existence in which those are having will to come
forward succeed. Project is like a bridge between theoretical and practical working. With
thiswilling we joined this particular project. First of all, we would like to thank the supreme
powerthe Almighty God who is obviously the one has always guided us to work on the right path
of life.

We sincerely thank Prof. R.H.Borhade sir Head of the Department of Computer Science of Smt
Kashibai Navale college of engineering, for all the facilities provided to us in the pursuitof this
project.

We are indebted to our project guide Prof.Sanjay.P.Pingat, Department of Computer Science of Smt.
Kashibai Navale college of engineering. We feel it’s a pleasure to be indebted to our guide for his
valuable support, advice and encouragement and we think him for his superb and constant guidance
towards this project.

We are deeply grateful to all the staff members of CS department, for supporting us in all aspects.

We acknowledge our deep sense of gratitude to our loving parents for being a constant sourceof
inspiration and motivation.

NAME OF THE STUDENT


Vaibhav Ugale

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

Mini Project
Title: Implement single-threaded merge sort and multithreaded merge sort.

Problem Statement: Implement merge sort and multithreaded merge sort. Compare time
required by both the algorithms. Also analyse the performance of each algorithm for the best case
and the worst case.

Prerequisites: Multithreading concepts, sorting concepts, Merge sort algorithm.

Objectives: To analyse the performance of Single-threaded merge sort and Multithreaded merge
sort algorithm for the best case and the worst case.

Outcome: To analyse and conclude which algorithm works best and takes less time for sorting.

Theory:

• Merge Sort: Merge sort is the sorting technique that follows the divide and conquer
approach. 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. We have to define the merge() function to perform the merging. 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

• Multithreading: Multithreading is the ability of a program or an operating system to


enable more than one user at a time without requiring multiple copies of the program
running on the computer. Multithreading can also handle multiple requests from the same
user. Each user request for a program or system service is tracked as a thread with a separate
identity. As programs work on behalf of the initial thread request and are interrupted by
other requests, the work status of the initial request is tracked until the work is completed.
In this context, a user can also be another program.

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

Fast central processing unit (CPU) speed and large memory capacities are needed for
multithreading. The single processor executes pieces, or threads, of various programs so
fast, it appears the computer is handling multiple requests simultaneously.

• Merge Sort using Multithreading: Merge Sort is a popular sorting technique which
divides an array or list into two halves and then start merging them when sufficient depth
is reached. Time complexity of merge sort is O(nlogn).

Threads are lightweight processes and threads shares with other threads their code section,
data section and OS resources like open files and signals. But, like process, a thread has its
own program counter (PC), a register set, and a stack space.

Multi-threading is way to improve parallelism by running the threads simultaneously in


different cores of your processor.

Conclusion: We can conclude that Single threaded recursive Merge sort is more faster in each
and every case than Multithreaded recursive Merge sort.

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

DAA MINI PROJECT CODE AND


OUTPUT

Package DAA_Project

import java.lang.System; import


java.u琀椀l.ArrayList; import
java.u琀椀l.Arrays; import
java.u琀椀l.Random; class
MergeSort{

private sta琀椀c 昀椀nal int MAX_THREADS = 4;

private sta琀椀c class SortThreads extends Thread{ SortThreads(Integer[] array, int

begin, int end){

super(()->{

MergeSort.mergeSort(array, begin, end);

}); this.start();

}
// Perform Threaded merge sort public sta琀椀c void

threadedSort(Integer[] array){

// For performance - get current 琀椀me in millis before star琀椀ng long


琀椀me = System.currentTimeMillis(); 昀椀nal int length = array.length;

boolean exact = length%MAX_THREADS == 0;

int maxlim = exact? length/MAX_THREADS: length/(MAX_THREADS-1);

maxlim = maxlim < MAX_THREADS? MAX_THREADS : maxlim;

// To keep track of threads 昀椀nal ArrayList<SortThreads>

threads = new ArrayList<>(); for(int i=0;

i < length; i+=maxlim){ int beg = i; int remain = (length)-i;

7
Downloaded by vaidehi dharmadhikari ([email protected])
lOMoARcPSD|47139209

int end = remain < maxlim? i+(remain-1): i+(maxlim-1); 昀椀nal SortThreads


t = new SortThreads(array, beg, end); // Add the thread references to join them later
threads.add(t);

for(Thread t: threads){

try{

t.join();

} catch(InterruptedExcep琀椀on ignored){}

for(int i=0; i < length; i+=maxlim){ int mid = i == 0? 0 : i-1; int


remain = (length)-i; int end

= remain < maxlim? i+(remain-1): i+(maxlim-1);

merge(array, 0, mid, end);

}
琀椀me = System.currentTimeMillis() - 琀椀me;

System.out.println("Time spent for custom mul琀椀-threaded recursive merge_sort(): "+ 琀椀me"ms");

// Typical recursive merge sort public sta琀椀c void

mergeSort(Integer[] array, int begin, int end){

if (begin<end){ int mid =

(begin+end)/2;

mergeSort(array, begin, mid);

mergeSort(array, mid+1, end);

merge(array, begin, mid, end);

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

//Typical 2-way merge public sta琀椀c void merge(Integer[] array, int begin, int

mid, int end){ Integer[]

temp = new Integer[(end-begin)+1];

int i = begin, j = mid+1; int k

= 0;

while(i<=mid && j<=end){

if (array[i] <= array[j]){


temp[k] = array[i]; i+=1;

}else{
temp[k] = array[j];

j+=1;
}

k+=1;

// Add remaining elements to temp array from 昀椀rst half that are le昀琀 over while(i<=mid){

temp[k] = array[i]; i+=1; k+=1;

// Add remaining elements to temp array from second half that are le昀琀 over

while(j<=end){ temp[k] =

array[j]; j+=1; k+=1;

for(i=begin, k=0; i<=end; i++,k++){ array[i]

= temp[k];

9
Downloaded by vaidehi dharmadhikari ([email protected])
lOMoARcPSD|47139209

class Driver{

// Array Size private sta琀椀c Random random = new


Random(); private sta琀椀c 昀椀nal int size =
random.nextInt(100); private

sta琀椀c 昀椀nal Integer list[] = new Integer[size]; // Fill the ini琀椀al

array with random elements within range sta琀椀c {

for(int i=0; i<size; i++){

list[i] = random.nextInt(size+(size-1))-(size-1);

// Test the sor琀椀ng methods performance

public sta琀椀c void main(String[] args){

System.out.print("Input = ["); for (Integer

each: list)

System.out.print(each+", ");

System.out.print("] \n" +"Input.length = " + list.length + '\n');

// Test standard Arrays.sort() method Integer[]

arr1 = Arrays.copyOf(list, list.length); long t =

System.currentTimeMillis(); Arrays.sort(arr1,

(a,b)->a>b? 1: a==b? 0: -1); t =

System.currentTimeMillis() - t;

System.out.println("Time spent for system based Arrays.sort(): " + t + "ms");

// Test custom single-threaded merge sort (recursive merge) implementa琀椀on

Integer[] arr2 = Arrays.copyOf(list, list.length); t = System.currentTimeMillis();

MergeSort.mergeSort(arr2, 0, arr2.length-1); t

= System.currentTimeMillis() - t;

System.out.println("Time spent for custom single threaded recursive

merge_sort(): " + t +

10

Downloaded by vaidehi dharmadhikari ([email protected])


lOMoARcPSD|47139209

"ms");

// Test custom (mul琀椀-threaded) merge sort (recursive merge) implementa琀椀on

Integer[] arr = Arrays.copyOf(list, list.length);

MergeSort.threadedSort(arr); System.out.print("Output

= ["); for (Integer

each: arr)

System.out.print(each+", ");

System.out.print("]\n");

11
Downloaded by vaidehi dharmadhikari ([email protected])

You might also like