1. Ascending Order Using Various Sorting Methods
For the list:
[
\text{List} = { 25, 34, 45, 8, 7, 78, 51 }
]
We'll show the sorting process step by step for three different sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort.
a. Bubble Sort
Bubble Sort works by repeatedly swapping adjacent elements if they are in the wrong order.
Initial List: { 25, 34, 45, 8, 7, 78, 51 }
Pass 1:
- Compare 25 and 34, no swap.
- Compare 34 and 45, no swap.
- Compare 45 and 8, swap: { 25, 34, 8, 45, 7, 78, 51 }
- Compare 45 and 7, swap: { 25, 34, 8, 7, 45, 78, 51 }
- Compare 45 and 78, no swap.
- Compare 78 and 51, swap: { 25, 34, 8, 7, 45, 51, 78 }
Pass 2:
- Compare 25 and 34, no swap.
- Compare 34 and 8, swap: { 25, 8, 34, 7, 45, 51, 78 }
- Compare 34 and 7, swap: { 25, 8, 7, 34, 45, 51, 78 }
- Compare 34 and 45, no swap.
- Compare 45 and 51, no swap.
- Compare 51 and 78, no swap.
Pass 3:
- Compare 25 and 8, swap: { 8, 25, 7, 34, 45, 51, 78 }
- Compare 25 and 7, swap: { 8, 7, 25, 34, 45, 51, 78 }
- Compare 25 and 34, no swap.
- Compare 34 and 45, no swap.
- Compare 45 and 51, no swap.
- Compare 51 and 78, no swap.
Pass 4:
- Compare 8 and 7, swap: { 7, 8, 25, 34, 45, 51, 78 }
- Remaining comparisons won't change positions.
Final Sorted List: { 7, 8, 25, 34, 45, 51, 78 }
b. Insertion Sort
Insertion Sort works by dividing the array into a sorted and unsorted section and gradually growing the sorted section.
Initial List: { 25, 34, 45, 8, 7, 78, 51 }
- Start with the second element (34), it's already in order.
- Insert 45, it's already in order.
- Insert 8: { 8, 25, 34, 45, 7, 78, 51 }
- Insert 7: { 7, 8, 25, 34, 45, 78, 51 }
- Insert 78: { 7, 8, 25, 34, 45, 51, 78 }
- Insert 51: { 7, 8, 25, 34, 45, 51, 78 }
Final Sorted List: { 7, 8, 25, 34, 45, 51, 78 }
c. Selection Sort
Selection Sort works by repeatedly selecting the minimum element from the unsorted section and moving it to the sorted section.
Initial List: { 25, 34, 45, 8, 7, 78, 51 }
- Find the minimum (7), swap with 25: { 7, 34, 45, 8, 25, 78, 51 }
- Find minimum (8), swap with 34: { 7, 8, 45, 34, 25, 78, 51 }
- Find minimum (25), swap with 45: { 7, 8, 25, 34, 45, 78, 51 }
- Find minimum (34), no swap.
- Find minimum (45), no swap.
- Find minimum (51), no swap.
- Remaining elements (78) are in place.
Final Sorted List: { 7, 8, 25, 34, 45, 51, 78 }
2. Hashing Process with Collision Resolution
For the list { 25, 34, 45, 8, 7, 78, 51 }, we will hash these numbers using the following hash function:
Hash Function: ( \text{key} \mod 7 )
Hash Table Size: 7
Step-by-step Hashing:
| Key | Hash Value (key % 7) | Action | Hash Table After Insertion |
|---|
| 25 | 4 | Insert at index 4 | { , , , , 25, , } |
| 34 | 6 | Insert at index 6 | { , , , , 25, , 34 } |
| 45 | 3 | Insert at index 3 | { , , , 45, 25, , 34 } |
| 8 | 1 | Insert at index 1 | { , 8, , 45, 25, , 34 } |
| 7 | 0 | Insert at index 0 | { 7, 8, , 45, 25, , 34 } |
| 78 | 1 | Collision (linear probe) | { 7, 8, , 45, 25, 78, 34 } |
| 51 | 2 | Insert at index 2 | { 7, 8, 51, 45, 25, 78, 34 } |
i) Linear Probing
For linear probing, when a collision occurs, we check the next slot in the array until we find an empty slot. In this case, ( 78 ) collided at index 1 and moved to index 5.
The final hash table with linear probing will look like:
[
\text{Hash Table} = [7, 8, 51, 45, 25, 78, 34]
]
ii) Chaining
With chaining, we maintain a linked list at every index for collisions.
Step-by-step Chaining:
| Key | Hash Value (key % 7) | Action | Hash Table After Insertion |
|---|
| 25 | 4 | Insert at index 4 | { , , , , {25}, , } |
| 34 | 6 | Insert at index 6 | { , , , , {25}, , {34} } |
| 45 | 3 | Insert at index 3 | { , , , {45}, {25}, , {34} } |
| 8 | 1 | Insert at index 1 | { , {8}, , {45}, {25}, , {34} } |
| 7 | 0 | Insert at index 0 | { {7}, {8}, , {45}, {25}, , {34} } |
| 78 | 1 | Collision, adding to list | { {7}, {8, 78}, , {45}, {25}, , {34} } |
| 51 | 2 | Insert at index 2 | { {7}, {8, 78}, {51}, {45}, {25}, , {34} } |
The final hash table with chaining will look like:
[
\text{Hash Table} = [{7}, {8, 78}, {51}, {45}, {25}, , {34}]
]
3. Binary Search for 8 and 48
Using the sorted list from step 1:
[
\text{Sorted List} = { 7, 8, 25, 34, 45, 51, 78 }
]
a. Finding 8
- Set low to 0, high to 6 (the indices of the list).
- Calculate mid: ( \text{mid} = \frac{0 + 6}{2} = 3 ) (rounded down).
- Compare list[3] (34) with 8: 34 > 8.
- High becomes mid - 1 = 2.
- Calculate mid: ( \text{mid} = \frac{0 + 2}{2} = 1 ).
- Compare list[1] (8) with 8: 8 = 8.
- Found 8 at index 1.
b. Finding 48
- Set low to 0, high to 6.
- Calculate mid: ( \text{mid} = \frac{0 + 6}{2} = 3 ).
- Compare list[3] (34) with 48: 34 < 48.
- Low becomes mid + 1 = 4.
- Calculate mid: ( \text{mid} = \frac{4 + 6}{2} = 5 ).
- Compare list[5] (51) with 48: 51 > 48.
- High becomes mid - 1 = 4.
- Calculate mid: ( \text{mid} = \frac{4 + 4}{2} = 4 ).
- Compare list[4] (45) with 48: 45 < 48.
- Low becomes mid + 1 (5).
Now low (5) is greater than high (4), so 48 is not found in the list.
Summary:
- Sorted List: { 7, 8, 25, 34, 45, 51, 78 }
- Binary Search Results: Found 8, Not Found 48.
- Hashing: Linear Probing produced an array and Chaining produced a linked list for collision resolution.