Download presentation
Presentation is loading. Please wait.
1
Sort Algorithms
2
The Merge Sort Algorithm
Today we will look at… The Merge Sort Algorithm
3
Sorting Algorithms Sorting Algorithms
Learning Objectives: To understand… Standard Sorting Algorithms: -merge sort Sorting Algorithms Sorting algorithms are used to sort data in 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
4
Sorting Algorithms Merge Sort Algorithm How it works: 6 2 1 8 5 3 7 4
Learning Objectives: To understand… Standard Sorting Algorithms: -merge sort Merge Sort Algorithm How it works: We start with a set of data to be sorted. 6 2 1 8 5 3 7 4 The data set is split into its individual items. 6 2 1 8 5 3 7 4 Each pair of individual items are compared and sorted into sorted pairs. 6 2 1 8 5 3 7 4 Source: 2 6 1 8 3 5 4 7
5
Sorting Algorithms Merge Sort Algorithm How it works: 2 6 1 8 3 5 4 7
Learning Objectives: To understand… Standard Sorting Algorithms: -merge sort Merge Sort Algorithm How it works: Continuing from the last slide…each pair of sorted pairs are compared and sorted into sorted sets of 4. 2 6 1 8 3 5 4 7 1 This is done by continually comparing the left most items in each pair and adding the smallest of these items to a new list each time. 2 6 8 3 5 4 7 1 2 6 8 3 5 4 7 1 2 6 8 3 5 4 7 1 2 6 8
6
Sorting Algorithms Merge Sort Algorithm How it works: 1 2 6 8 3 5 4 7
Learning Objectives: To understand… Standard Sorting Algorithms: -merge sort Merge Sort Algorithm How it works: Continuing on from the previous slide… …each pair of sorted pairs are compared and sorted into sorted sets of 4. 1 2 6 8 3 5 4 7 3 5 4 7 This is done by continually comparing the left most items in each pair and adding the smallest of these items to a new list each time. 3 4 5 7 3 4 5 7 1 2 6 8 3 4 5 7
7
Sorting Algorithms Learning Objectives: To understand… Standard Sorting Algorithms: -merge sort Merge Sort Algorithm How it works: 1 2 6 8 3 4 5 7 1 2 6 8 3 4 5 7 Next, the items in each pair of sorted 4s are compared and sorted into sorted sets of 8. 1 2 6 8 3 4 5 7 This is done by continually comparing the left most items in each pair and adding the smallest of these items to a new list each time. 1 2 3 6 8 4 5 7 1 2 3 4 6 8 5 7 1 2 3 4 5 6 8 7 Because this example contained 8 numbers, the data is sorted at the end of this phase. For longer data sets, the process would continue in the same manner. 1 2 3 4 5 6 8 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8
8
Sorting Algorithms Merge Sort Algorithm Learning Objectives:
To understand… Standard Sorting Algorithms: -merge sort Merge Sort Algorithm def mergeSort(dataSet): result = [] IF LENGTHOF(dataSet) < 2 RETURN x 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
9
Question Demonstrate all stages of a merge sort so that the following set of data can be ordered. 7 3 2 9 6 4 5
10
Answer 2 3 7 9 4 6 5 8 4 7 3 2 9 6 4 8 5 6 5 8 7 3 2 9 6 4 8 5 4 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
11
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.