The algorithm for a bubble sort of n items is given below.

for i = 1 to n-1

for j = 1 to (n-i)

if numbers [j] > numbers[j+1]

# Swap the names in the array

temp = numbers[j]

numbers[count] = numbers[count+1]

numbers[count+1] = temp

endif

next j

next i

(a) Calculate the number of steps needed to sort a list of 5 items. Count the IF statement in the inner loop as one statement. [2]

I don't know how to do this. What exactly is a 'step'? Thank you

for i = 1 to n-1

for j = 1 to (n-i)

if numbers [j] > numbers[j+1]

# Swap the names in the array

temp = numbers[j]

numbers[count] = numbers[count+1]

numbers[count+1] = temp

endif

next j

next i

(a) Calculate the number of steps needed to sort a list of 5 items. Count the IF statement in the inner loop as one statement. [2]

I don't know how to do this. What exactly is a 'step'? Thank you

Original post by Sasuto

The algorithm for a bubble sort of n items is given below.

for i = 1 to n-1

for j = 1 to (n-i)

if numbers [j] > numbers[j+1]

# Swap the names in the array

temp = numbers[j]

numbers[count] = numbers[count+1]

numbers[count+1] = temp

endif

next j

next i

(a) Calculate the number of steps needed to sort a list of 5 items. Count the IF statement in the inner loop as one statement. [2]

I don't know how to do this. What exactly is a 'step'? Thank you

for i = 1 to n-1

for j = 1 to (n-i)

if numbers [j] > numbers[j+1]

# Swap the names in the array

temp = numbers[j]

numbers[count] = numbers[count+1]

numbers[count+1] = temp

endif

next j

next i

(a) Calculate the number of steps needed to sort a list of 5 items. Count the IF statement in the inner loop as one statement. [2]

I don't know how to do this. What exactly is a 'step'? Thank you

Its possibly a bit loosely worded, but for 2 marks, Id presume they mean that the code block associated with the conditional if is "1 step", irrespective of whether the body is executed or not, so really youre just counting the number of iterations in the nested for loops.

(edited 1 year ago)

Original post by mqb2766

Its possibly a bit loosely worded, but for 2 marks, Id presume they mean that the code block associated with the conditional if is "1 step", irrespective of whether the body is executed or not, so really youre just counting the number of iterations in the nested for loops.

Thank you. So it would be 5 steps?

(edited 1 year ago)

Original post by Sasuto

Thank you. So it would be 5 steps?

There are 2 loops, and how many times does each one execute its body? As a trivial case, how many times would a bubble sort execute if there were 2 elements in the list?

Original post by mqb2766

There are 2 loops, and how many times does each one execute its body? As a trivial case, how many times would a bubble sort execute if there were 2 elements in the list?

Would it be 10? If there were 2 elements, would it be 4?

Original post by Sasuto

Would it be 10? If there were 2 elements, would it be 4?

No for both. Do you understand how two nested loops operate and how many times each executes?

A bubble sort for 2 elements would be "trivial" if you understand the algorithm.

Original post by mqb2766

No for both. Do you understand how two nested loops operate and how many times each executes?

A bubble sort for 2 elements would be "trivial" if you understand the algorithm.

A bubble sort for 2 elements would be "trivial" if you understand the algorithm.

Okay. I'll try to break everything down.

Why is it to n-1 and not n in:

for i = 1 to n-1

And why is it to (n-i)?

for j = 1 to (n-i)

Original post by Sasuto

Okay. I'll try to break everything down.

Why is it to n-1 and not n in:

for i = 1 to n-1

And why is it to (n-i)?

for j = 1 to (n-i)

Why is it to n-1 and not n in:

for i = 1 to n-1

And why is it to (n-i)?

for j = 1 to (n-i)

My eyes are going, the inner loop goes to n-i, not n-1. So 10 is right (though 4 wasnt if there are 2 elements), do you know how you came up with that and how the inner/outer loops work?

(edited 1 year ago)

Original post by mqb2766

My eyes are going, the inner loop goes to n-i, not n-1. So 10 is right (though 4 wasnt if there are 2 elements), do you know how you came up with that and how the inner/outer loops work?

I'm not sure... but is it because the innor loop checks if the number is bigger than the next number and if it is, it swaps the number. And then the outer loop goes through the list?? This is confusing to me. And what is [count] for? Is it to count the number of swaps we make?

If you're wondering why I find this so confusing it's because I didn't take cs for my GCSEs.

Original post by Sasuto

I'm not sure... but is it because the innor loop checks if the number is bigger than the next number and if it is, it swaps the number. And then the outer loop goes through the list?? This is confusing to me. And what is [count] for? Is it to count the number of swaps we make?

If you're wondering why I find this so confusing it's because I didn't take cs for my GCSEs.

If you're wondering why I find this so confusing it's because I didn't take cs for my GCSEs.

You do cover bubble sort on your gcse, so you may need to google some vidoes/basic tutorials - its relatively straight forward. But even if you don't understand the algorithm, n is 5, so

* The outer loop operates 4 times so i=1..(5-1)

* The first time the inner loop iterates *** times as j=1..(5-1)

* The second time the inner loop iterates *** times as j=1..(5-2)

* ...

But you are expected to know how the bubble sort operates, so a quick google would be worthwhile.

Original post by mqb2766

You do cover bubble sort on your gcse, so you may need to google some vidoes/basic tutorials - its relatively straight forward. But even if you don't understand the algorithm, n is 5, so

* The outer loop operates 4 times so i=1..(5-1)

* The first time the inner loop iterates *** times as j=1..(5-1)

* The second time the inner loop iterates *** times as j=1..(5-2)

* ...

But you are expected to know how the bubble sort operates, so a quick google would be worthwhile.

* The outer loop operates 4 times so i=1..(5-1)

* The first time the inner loop iterates *** times as j=1..(5-1)

* The second time the inner loop iterates *** times as j=1..(5-2)

* ...

But you are expected to know how the bubble sort operates, so a quick google would be worthwhile.

Okay, thank you for helping me! Have a nice day!

- computer science alevel but physics makes me want to die?
- TSR Computer Science Society
- Self study A level Computer Science or A level Maths
- OCR A-level Computer Science Paper 2 (H446/01) - 18th June 2024 [Exam Chat]
- Computer science A-level
- A Level Computer Science (Written+ NEA) - Private Centres : AQA
- I have a feeling that my computer science grade will be very low.
- A-level Computer Science Study Group 2023-2024
- full A level computer science course
- About my a level choice answer and that’ll be very helpful
- A-level computer science
- is this a good revision method and also does anyone have any good resources?
- Gcse ocr computer science
- Recommended books for AQA chemistry and OCR computer science A level books
- Coding Artefact with Computer Science A-Level
- OCR A Level Computer Science Book Resource Help
- Giving away A* a level computer science anki flashcards
- Which Computer Science exam board is most similar to WJEC/Eduqas?
- Using PyQt5 Designer for OCR A Level Computer Science project
- PG online computer science test answers

Latest

Trending

Last reply 1 week ago

can someone please explain what principle domain is and why the answer is a not c?Maths

0

13