0
0
Intro to Computingfundamentals~10 mins

Sorting algorithms (bubble, selection) in Intro to Computing - Flowchart & Logic Diagram

Choose your learning style9 modes available
Process Overview

Sorting algorithms arrange items in order. Bubble sort swaps neighbors to push the largest to the end. Selection sort finds the smallest and places it at the start. Both repeat until sorted.

Flowchart
Set i = 0
|Yes
Set j = 0
|Yes
j
Swap arr[j
|No
Increment j
(Loop back to j < n-1-i?)
Increment i
(Loop back to i < n-1?)
Set i = 0
|Yes
Set min_index = i
Set j = i+1
|Yes
j
Set min_index = j
|No
Increment j
(Loop back to j < n?)
Swap arr[i
|No
Increment i
(Loop back to j < n-1-i?)
This flowchart shows the step-by-step process of bubble sort and selection sort algorithms for sorting an array.
Step-by-Step Trace - 33 Steps
Step 1: Start bubble sort with array [4, 2, 5, 1]
Step 2: Compare arr[0]=4 and arr[1]=2
Step 3: Swap 4 and 2 because 4 > 2
Step 4: Compare arr[1]=4 and arr[2]=5
Step 5: Compare arr[2]=5 and arr[3]=1
Step 6: Swap 5 and 1
Step 7: Increment i to 1, start next pass
Step 8: Compare arr[0]=2 and arr[1]=4
Step 9: Compare arr[1]=4 and arr[2]=1
Step 10: Swap 4 and 1
Step 11: Increment i to 2, start next pass
Step 12: Compare arr[0]=2 and arr[1]=1
Step 13: Swap 2 and 1
Step 14: End bubble sort
Step 15: Start selection sort with array [4, 2, 5, 1]
Step 16: Set min_index = 0 (value 4)
Step 17: Compare arr[1]=2 with arr[min_index]=4
Step 18: Set min_index = 1
Step 19: Compare arr[2]=5 with arr[min_index]=2
Step 20: Compare arr[3]=1 with arr[min_index]=2
Step 21: Set min_index = 3
Step 22: Swap arr[0]=4 and arr[3]=1
Step 23: Increment i to 1
Step 24: Set min_index = 1 (value 2)
Step 25: Compare arr[2]=5 with arr[min_index]=2
Step 26: Compare arr[3]=4 with arr[min_index]=2
Step 27: min_index = i, no swap needed
Step 28: Increment i to 2
Step 29: Set min_index = 2 (value 5)
Step 30: Compare arr[3]=4 with arr[min_index]=5
Step 31: Swap arr[2]=5 and arr[3]=4
Step 32: Increment i to 3
Step 33: End selection sort
Diagram
Array Memory Layout:

Index:  0    1    2    3
       +----+----+----+----+
Value: | 4  | 2  | 5  | 1  |
       +----+----+----+----+

During sorting, values swap places in these boxes.

Bubble Sort pushes largest values to the right step by step.
Selection Sort picks smallest values and places them at the left step by step.
This diagram shows the array as boxes in memory with indexes and values. It helps visualize how sorting swaps values in place.
Flowchart Quiz - 3 Questions
Test your understanding
In bubble sort, what happens when two adjacent elements are compared and the left one is larger?
AThe smaller element moves left
BThey are left as is
CThey are swapped to move the larger element right
DThe algorithm ends
Key Result
Both bubble and selection sort repeatedly compare and swap elements to gradually arrange the array in order, but bubble sort swaps neighbors while selection sort swaps the smallest found element with the current position.