What is a sorting algorithm?
- Sorting algorithms are precise step-by-step instructions that a computer can follow to efficiently sort data in massive datasets
- Three common sorting algorithms are:
- Bubble sort
- Merge sort
- Insertion sort
Author
Robert HamptonExpertise
Computer Science Content Creator
Step | Instruction |
1 | Compare the first two values in the dataset |
2 |
IF they are in the wrong order...
|
3 | Compare the next two values |
4 | REPEAT step 2 & 3 until you reach the end of the dataset (pass 1) |
5 |
IF you have made any swaps...
|
6 |
ELSE you have not made any swaps...
|
5 | 2 | 4 | 1 | 6 | 3 |
Step | Instruction | ||||||||||||||||||||||||
1 |
Compare the first two values in the dataset
|
||||||||||||||||||||||||
2 |
IF they are in the wrong order...
|
||||||||||||||||||||||||
3 |
Compare the next two values
|
||||||||||||||||||||||||
4 |
REPEAT step 2 & 3 until you reach the end of the dataset
|
||||||||||||||||||||||||
5 |
IF you have made any swaps...
|
||||||||||||||||||||||||
6 |
ELSE you have not made any swaps...
|
In the exam you do not have to show every swap that takes place in a bubble sort. You can show the outcome of a bubble sort at the end of each pass. If you have the outcome of each pass correct then a bubble sort has been implemented correctly and all marks will be given!
A program uses a file to store a list of words.
A sample of this data is shown
Milk | Eggs | Bananas | Cheese | Potatoes | Grapes |
Show the stages of a bubble sort when applied to data shown [2]
How to answer this question
Answer
# unsorted dataset
nums=[66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39]
# count the length of the dataset
numlength = len(nums)
# sets a flag to initiate the loop
swaps = True
while swaps == True :
swaps = False
# loop through the dataset
for y in range(numlength-1) :
# if the first number is bigger than the second number
if nums[y] > nums[y+1] :
# swap the numbers using a temporary variable
temp = nums[y]
nums[y] = nums[y+1]
nums[y+1] = temp
# sets the flag to true
swaps = True
# prints the sorted list
print (nums)
Step | Instruction |
1 | Divide the dataset into individual datasets by repeatedly splitting the dataset in half (DIVIDE) |
2 | Merge pairs of sub-datasets together by comparing the first value in each dataset (CONQUER) |
3 | REPEAT step 2 until all sub-datasets are merged together into one dataset |
7 | 4 | 1 | 2 | 6 | 3 | 8 | 5 |
Step | Instruction | |||||||||||||||||||||||||||||||||||||||||||
1 |
Divide the dataset into individual datasets by repeatedly splitting the dataset in half (DIVIDE)
divide in half (2 datasets of 4 values)
divide in half again (4 datasets of 2 values)
divide in half again (8 datasets of 1 value)
|
|||||||||||||||||||||||||||||||||||||||||||
2 |
Merge pairs of sub-datasets together by comparing the first value in each dataset (CONQUER)
Merge into 4 datasets of 2 values
|
|||||||||||||||||||||||||||||||||||||||||||
3 |
REPEAT step 2 until all sub-datasets are merged together into one dataset Merge into 2 datasets of 4 values
Merge into 1 dataset of 8 values (in the correct order)
|
In the exam, the divide stage could already be done for you and you would only need to demonstrate the conquer stage!
Step | Instruction |
1 | Take the first value in the dataset, this is now the sorted list |
2 |
Look at the next value in the dataset and compare it to the first value IF it is smaller
|
3 |
ELSE
|
4 |
REPEAT steps 2 & 3 until all values in the dataset have been compared, the list should now be in order |
54 | 27 | 17 | 9 | 40 | 12 |
Step | Instruction | ||||||||||||||||||||||||||||||
1 |
Take the first value in the dataset, this is now the sorted list
|
||||||||||||||||||||||||||||||
2 |
Look at the next value in the dataset (27) and compare it to the first value (is 27 < 54?) IF it is smaller (YES!)
|
||||||||||||||||||||||||||||||
3 |
ELSE
|
||||||||||||||||||||||||||||||
4 |
REPEAT steps 2 & 3 until all values in the dataset have been compared, the list should now be in order
|
Remember that in any sorting algorithm question, you make be asked to demonstrate a sort using words instead of numbers. The process is the same, the order will be alphabetically from A-Z unless stated in the question.
You may also abbreviate words using just their first letter as long as two values don't share the same first letter!
to absolutely everything:
Did this page help you?
Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.