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

The Merge Sort Algorithm

A random document I found

Uploaded by

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

The Merge Sort Algorithm

A random document I found

Uploaded by

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

GCSE 9-1: OCR Computer Science

Sort Algorithms

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Today we will look at…

The Merge Sort Algorithm

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Sorting Algorithms
Learning
Objectives: Sorting Algorithms
To understand…

Standard Sorting • Sorting algorithms are used to sort data in


Algorithms:
-merge sort
particular ways (for example sorting a set of
numbers from highest to lowest).

• There are some standard sorting algorithms


that have been developed and each have their
own merits.

• Over the next few slides we will take a look


another common sorting algorithms:

– Merge Sort Algorithm

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Sorting Algorithms
Learning Merge Sort Algorithm
Objectives:
How it works:
To understand…

Standard Sorting We start with a set of data


Algorithms: to be sorted. 6 2 1 8 5 3 7 4
-merge sort

The data set is split into


its individual items. 6 2 1 8 5 3 7 4

Each pair of individual


Source: items are compared and 6 2 1 8 5 3 7 4
https://en.wikipedia.org/wiki/Merge_sor sorted into sorted pairs.
t
2 6 1 8 3 5 4 7

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Sorting Algorithms
Learning Merge Sort Algorithm
Objectives:
How it works:
To understand…

Standard Sorting Continuing from the last


Algorithms: slide…each pair of sorted
-merge sort pairs are compared and
sorted into sorted sets of
2 6 1 8 3 5 4 7
4.
1
This is done by
continually comparing 2 6 8 3 5 4 7
the left most items in
each pair and adding the 1 2
smallest of these items
to a new list each time. 6 8 3 5 4 7
1 2 6
8 3 5 4 7
1 2 6 8

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Sorting Algorithms
Learning Merge Sort Algorithm
Objectives:
How it works:
To understand…

Standard Sorting
Algorithms: Continuing on from the
-merge sort previous slide… 1 2 6 8 3 5 4 7
…each pair of sorted
pairs are compared and 3
sorted into sorted sets of
4. 5 4 7
This is done by
continually comparing 3 4
the left most items in
each pair and adding the 5 7
smallest of these items
to a new list each time. 3 4 5
7
1 2 6 8 3 4 5 7

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Sorting Algorithms
Learning Merge Sort Algorithm 1 2 6 8 3 4 5 7
Objectives: How it works:
1
To understand…
2 6 8 3 4 5 7
Standard Sorting Next, the items in each
Algorithms: pair of sorted 4s are 1 2
-merge sort compared and sorted into
sorted sets of 8. 6 8 3 4 5 7

This is done by 1 2 3
continually comparing
the left most items in 6 8 4 5 7
each pair and adding the
smallest of these items 1 2 3 4
to a new list each time.
6 8 5 7
1 2 3 4 5
Because this example 6 8 7
contained 8 numbers,
the data is sorted at the 1 2 3 4 5 6
end of this phase. For
longer data sets, the 8 7
process would continue
in the same manner.
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Sorting Algorithms
Learning
Objectives: Merge Sort Algorithm
To understand… def mergeSort(dataSet):
result = []
Standard Sorting IF LENGTHOF(dataSet) < 2
Algorithms: RETURN x
-merge sort
mid = LENGTHOF(dataSet) DIV 2
y = mergeSort(x[:mid])
z = mergeSort(x[mid:])
i=0
j=0
WHILE i < LENGTHOF(y) and j < LENGTHOF(z):
IF y[i] > z[j]
result.APPEND(z[j])
j += 1
ELSE
result.APPEND(y[i])
i += 1
result += y[i:]
result += z[j:]
RETURN result

dataSet = [4,6,2,8,4,55,6,22,54,23,36,42]
results = mergeSort(dataSet)
DISPLAY results

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Question
• Demonstrate all stages of a merge
sort so that the following set of data
can be ordered.

7 3 2 9 6 4 6 5

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

Answer 2 3 7 9 4 6 5 8
4
7 3 2 9 6 4 8 5 6 5 8
4 5
7 3 2 9 6 4 8 5
6 8
3 7 2 9 4 6 5 8
4 5 6
3 7 2 9 4 6 5 8 8
2 2 3 7 9 4 5 6 8
3 7 9 4 6 5 8
2 3
7 9 4 6 5 8
2 3 7
9 4 6 5 8
2 3 7 9

www.computerscienceuk.com
GCSE 9-1: OCR Computer Science

2 3 7 9 4 5 6 8
2
3 7 9 4 5 6 8
2 3
7 9 4 5 6 8
2 3 4
7 9 5 6 8
2 3 4 5
7 9 6 8
2 3 4 5 6
7 9 8
2 3 4 5 6 7
9 8
2 3 4 5 6 7 8
2 3 4 5 6 7 8 9

www.computerscienceuk.com

You might also like