Sorting
Bubble Sort
It works by swapping the adjacent elements if they are in wrong order. It is the most inefficient sorting algorithm because of simple it is. It has the time complexity of O(n^2). Insertion sort is better than bubble sort because it has the same asymptotic complexity but only requires O(n) swaps whereas bubble sort require O(n2) swaps.
Psuedocode
let n be the length of the array
for i from 0 to n-1
for j from 0 to n-1-i //The last element is already sorted after each pass
if arr[j] > arr[j+1]
swap(arr[j], arr[j+1])
let the array be [3,2,4,1]
i=0
j=0 //[3,2,4,1]
3>2
swap(3,2) //[2,3,4,1]
j=1 //[2,3,4,1]
3>4
j=2 //[2,3,4,1]
4>1
swap(4,1) //[2,3,1,4]
i=1
j=0 //[2,3,1,4]
2>3
j=1 //[2,3,1,4]
3>1
swap(3,1) //[2,1,3,4]
i=2
j=0 //[2,1,3,4]
2>1
swap(2,1) //[1,2,3,4]
Diagram
Optimization
we use a flag to track if there is swapping taking place, if there is no swapping it means the array is already sorted so no need for more pass.
for i from 0 to n-1
swapped = 0
for j from 0 to n-1-i //The last element is already sorted after each pass
if arr[j] > arr[j+1]
swap(arr[j], arr[j+1])
swapped = 1
if swapped == 0 // No elements are swapped so stop the loop
break
Insertion Sort
Insertion sort algorithm sorts an array by inserting an element from the unsorted part to correct position at sorted part.
Selection Sort
Selection sort algorithm sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning.
Psuedocode
let n be the length of the array
for i from 0 to n-1
min = i
for j from i+1 to n-1
if arr[j] < arr[min]
min = j
temp = arr[i]
arr[i] = arr[min]
arr[min] = temp
let the array be [3,2,4,1]
i=0
min = 0
j=1 //[3,2,4,1]
2 < 3
min = 1
j=2 //[3,2,4,1]
4 < 2
j=3 //[3,2,4,1]
1 < 2
min = 3
swap(a[i], a[min]) //[1,2,4,3]
i=1
min = 1
j=2 //[1,2,4,3]
4 < 2
j=3 //[1,2,4,3]
3 < 2
i=2
min = 2
j = 3 //[1,2,4,3]
3 < 4
swap(a[i], a[min]) //[1,2,3,4]
```<style>
.scroll-to-top {
font-size: 2.5rem;
width: 3.2rem;
height: 3.2rem;
display: none;
align-items: center;
justify-content: center;
position: fixed;
padding: 0.75rem;
bottom: 4rem;
right: calc(1.25rem + 90px + var(--page-padding));
z-index: 999;
cursor: pointer;
border: none;
color: var(--bg);
background: var(--fg);
border-radius: 50%;
}
.scroll-to-top.hidden {
display: none;
}
.scroll-to-top i {
transform: translateY(-2px);
}
@media (min-width: 1080px) {
.scroll-to-top {
display: flex;
}
}
</style>
<button type="button" aria-label="scroll-to-top" class="scroll-to-top hidden" onclick="scrollToTop()">
<i class="fa fa-angle-up"></i>
</button>
<script>
const scrollToTop = () => window.scroll({ top: 0, behavior: "smooth" });
window.addEventListener("scroll", () => {
const button = document.querySelector(".scroll-to-top");
button.classList.toggle("hidden", window.scrollY < 200);
});
</script>
<style>
footer {
text-align: center;
text-wrap: balance;
margin-top: 5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer p {
margin: 0;
}
</style>
<footer><p>Copyright © 2024 • Created with ❤️ by <a href="https://ashish.me">Ashish Patel</a></p>
</footer>