CMPT128

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Merge Sort Algorithm

In this Week
Merge Sort Algorithm
Additional Material (C++ file Input/Output)
Review of the course

CMPT 128 - FIC 2015-02 - Yonas T.


Weldeselassie

Merge Sort
In merge sort algorithm, the sorting procedure
goes as follows:
Algorithm Merge Sort
Sort the left half of the array
Sort the right half of the array
Combine (or merge) the left half and the right half
of the array

Details follow
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort
We can use any sorting algorithm to sort the
left half and the right half of the array
However, it makes sense by just looking at the
algorithm that we can make a recursive call of
the same algorithm
The stopping criteria will be when there is no
element to sort or there is only one element
to sort which is already sorted anyways
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort
The C++ code for merge sort algorithm will be

Now the question is how to merge the sorted left


half and right half sub-arrays efficiently
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort
The merge function takes an array A such that
the elements
A[startIndex], A[startIndex+1],....,A[middleIndex]

are sorted; and


A[middleIndex+1], A[middleIndex+2],....,A[lastIndex]

are also sorted.


However each half sub-array is completely
independent of the other half.
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort
In combining (merging) the left half and right
half sub-arrays therefore, we should proceed
as follows
Make a temporary array of the same size
While both sub-arrays have elements, copy
smallest element of the two sub-arrays to the
temporary array. Delete the smallest element
from its sub-array
Copy any remaining elements from whichever left
half that still has elements left
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort
We notice that we don't have to search for the
smallest element of the two sub-arrays
Since the sub-arrays are sorted, the smallest
element must be either A[startIndex] or
A[middleIndex+1]
This is the heart of the merge sort algorithm.
We can merge the sorted sub-arrays efficiently
Why merge on temporary array?
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort
We should merge the sub-arrays to temporary
array for otherwise we will destroy the array
being merged
Of course one could merge in the same array
with additional tasks. Can you think how?
The C++ function for the merging algorithm
follows:

CMPT 128 - FIC 2015-02 - Yonas T.


Weldeselassie

Merge Sort

Continued Next Page....


CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

Merge Sort

CMPT 128 - FIC 2015-02 - Yonas T.


Weldeselassie

10

C++ file input/output


We have seen how to print output to the
console (mostly monitor) using the cout
object from C++ iostream library
We have also seen how to read input from the
console (mostly keyboard) using the cin object
from C++ iostream library
We now look at how to print output to file
and read input from file using C++ fstream
library
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

11

C++ file input/output


C++ has the following two classes to handle
file input/output
The class ifstream has objects that are input file
streams
The class ofstream has objects that are output file
streams

Definitions of these file streams are located in


the header file fstream and so the following
directive must be included:
#include <fstream>
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

12

C++ file input/output


Before a file stream can be used for file
input/output, it must be declared
The type for input file stream variables is ifstream
The type for output file stream variables is ofstream

For example we declare inputFileStream variable to


be an input file stream as follows
ifstream inputFileStream;
Similarly, we declare outputFileStream variable to
be an output file stream as follows:
ofstream outputFileStream;
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

13

Writing to output file


Once we have declared an ofstream object, we
open a new file and connect it to the stream as
follows:
outputFileStream.open("ExampleFile.txt", ios :: out);

In this case, we are creating a new text file named


ExampleFile.txt for output operation (ios::out)
Now, every output stream made using the
variable outputFileStream will be written to this
file; as long as the file has been created
successfully
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

14

Writing to output file


Before writing to an output file, it is important
to check if the opening of the output file
stream has been successful. This is achieved
using the is_open member function as
follows:
bool flag = outputFileStream.is_open();
The is_open member function returns true if
the file opening operation was successful;
returns false otherwise
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

15

Writing to output file


Once we have asserted the opening operation,
we can use the outputFileStream object just like
cout object and use the << operator to write
output as follows:
outputFileStream << DATA << endl;
The output written will automatically go to the
file connected to the stream
Once we finish, the writing of the output to the
file, we must close the the output file stream as
follows:
outputFileStream.close();
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

16

Writing to output file


The new file created will be found in the same
folder where the cpp file of our project is located
It is important to note that if a file with the same
file name already existed in the same folder; then
it will be automatically deleted and a new file
created
The following C++ program demonstrates writing
to output file in C++. It creates a new file named
RandomNumbers.txt and writes in it 50 random
numbers in the range [0, 99]
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

17

Writing to output file: Example

CMPT 128 - FIC 2015-02 - Yonas T.


Weldeselassie

18

Reading from input file


In order to read input from existing file, follow the following
steps:
Declare an ifstream object
ifstream inputFileStream;
Connect the ifstream object to existing file for input
operations (use ios::in)
inputFileStream.open("RandomNumbers.txt", ios::in);
Check if the opening of the input stream was successful
bool flag = inputFileStream.is_open();
If successful use the ifstream object just like you would
for cin object
inputFileStream >> SOME VARIABLE;
Close the input file stream
inputFileStream.close();
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

19

Reading from input file


As in the case of the output stream, if the file is
not found in the same folder where the cpp file of
our project is found, then we will have file not
found error; that is is_open() member function
will return false
We should know how much data we have in the
input file so that we don't exceed the end of the
input file; in which case there will be an error
The following program demonstrates C++ file
input operation
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

20

Reading from input file: Example

CMPT 128 - FIC 2015-02 - Yonas T.


Weldeselassie

21

Lab Work
Write a program that creates a new output file named
RandomIntegers.txt and will fill it with 20 random
integers in the range [-50, 50]
Write a program that will create a dynamic array of size
20 and populates the array with the random integers
contained in the file RandomIntegers.txt
Write a program that will create a dynamic array of size
20 and populates the array with the random integers
contained in the file RandomIntegers.txt; sorts the array
using merge sort algorithm and then writes the sorted
integers to an output file named SortedIntegers.txt
Write a program that will read each integer contained in
the file SortedIntegers.txt and prints it to the screen
CMPT 128 - FIC 2015-02 - Yonas T.
Weldeselassie

22

You might also like