Lecture 2 Algorithms-1
Lecture 2 Algorithms-1
Quadratic sorts
Let's look at three quadratic sorting algorithms - insertion sort,
selection sort, and bubble sort
1. Insertion Sort
How it works:
Start with the first element of the array, assuming it is already sorted.
Take the next element and insert it into the correct place in the sorted
part of the array.
Repeat this process for all elements of the array.
C++ code with explanation:
**Explanation:**
- We start with the second element of the array, assuming that the first
element is already sorted.
- In the `while` loop, we shift the array elements to the right until we
find a position to insert the current element (key).
- Once the correct position is found, we insert the key at that location.
2. Selection Sort
How it works:
- We look for the minimum element in the array and move it to the
beginning.
- Then we look for the minimum element in the rest of the array and
move it to the next position.
- We repeat the process for all elements of the array.
**Explanation:**
3. Bubble Sort
How it works:
- We go through the array, comparing adjacent elements, and swap
them if they are in the wrong order.
- After each iteration, the largest element "floats" to the end of the
array.
- Repeat the process until the array is completely sorted.
Explanation:
Conclusion
These three examples show how quadratic sorting algorithms work in
C++ without using functions. Such algorithms are easy to understand
and implement, but their efficiency leaves much to be desired when
working with large arrays.