sorting-algo (1)
sorting-algo (1)
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.
● 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.
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.
● 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]
3. Insertion Sort
Insertion Sort builds a sorted portion of the array by inserting elements into their correct
positions one by one.
4. Shell Sort
Shell Sort generalizes insertion sort by starting with large gaps between elements and
reducing the gap as sorting progresses.
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.