Razon,Ray Christian C.
BSIT305
1. Bubble Sort
Bubble Sort repeatedly steps through the array, compares adjacent elements, and swaps
them if they are in the wrong order. This process is repeated until the entire array is sorted.
Array: [21, 4, 25, 16]
Steps:
● Pass 1:
○ Compare 21 and 4 ➔ Swap ➔ [4, 21, 25, 16]
○ Compare 21 and 25 ➔ No swap
○ Compare 25 and 16 ➔ Swap ➔ [4, 21, 16, 25]
● Pass 2:
○ Compare 4 and 21 ➔ No swap
○ Compare 21 and 16 ➔ Swap ➔ [4, 16, 21, 25]
● Pass 3:
○ No swaps needed; array is sorted.
Sorted Result: [4, 16, 21, 25]
2. Selection Sort
Selection Sort divides the array into a sorted and unsorted portion. It repeatedly selects the
smallest element from the unsorted part and moves it to the sorted part.
Array: [21, 4, 25, 16, 12, 17]
Steps:
● Pass 1: Find minimum (4), swap with 21 ➔ [4, 21, 25, 16, 12, 17]
● Pass 2: Find minimum (12), swap with 21 ➔ [4, 12, 25, 16, 21, 17]
● Pass 3: Find minimum (16), swap with 25 ➔ [4, 12, 16, 25, 21, 17]
● Pass 4: Find minimum (17), swap with 25 ➔ [4, 12, 16, 17, 21, 25]
Sorted Result: [4, 12, 16, 17, 21, 25]
3. Insertion Sort
Insertion Sort builds a sorted portion of the array by inserting elements into their correct
positions one by one.
Array: [21, 4, 25, 16, 12]
Steps:
● Pass 1: Insert 4 before 21 ➔ [4, 21, 25, 16, 12]
● Pass 2: 25 is in the correct position ➔ [4, 21, 25, 16, 12]
● Pass 3: Insert 16 before 21 ➔ [4, 16, 21, 25, 12]
● Pass 4: Insert 12 before 16 ➔ [4, 12, 16, 21, 25]
Sorted Result: [4, 12, 16, 21, 25]
4. Shell Sort
Shell Sort generalizes insertion sort by starting with large gaps between elements and
reducing the gap as sorting progresses.
Array: [21, 4, 25, 16, 12, 17]
Steps:
● Gap = 3: Compare elements 3 positions apart.
○ Compare 21 and 16 ➔ Swap ➔ [16, 4, 25, 21, 12, 17]
● Gap = 1: Perform insertion sort on the array.
○ Insert 4 before 16 ➔ [4, 16, 25, 21, 12, 17]
○ Insert 12 before 16 ➔ [4, 12, 16, 21, 17, 25]
Sorted Result: [4, 12, 16, 17, 21, 25]
5. Merge Sort
Merge Sort uses a divide-and-conquer approach to divide the array into smaller parts, sort
them, and merge them back together.
Array: [21, 4, 25, 16, 12, 17, 3, 30]
Steps:
1. Split the array into halves:
○ [21, 4, 25, 16] and [12, 17, 3, 30]
2. Split again into smaller parts:
○ [21, 4], [25, 16], [12, 17], [3, 30]
3. Sort and merge:
○ [4, 21], [16, 25], [12, 17], [3, 30]
4. Merge pairs:
○ [4, 16, 21, 25], [3, 12, 17, 30]
5. Final merge:
○ [3, 4, 12, 16, 17, 21, 25, 30]
Sorted Result: [3, 4, 12, 16, 17, 21, 25, 30]