sorting is the process of arranging a collection of items or
data elements in a specific order. Sorting is a fundamental
operation in computer science and is used in a wide range of
applications
1.BUBBLE SORT
Bubble sort is a simple comparison-based sorting algorithm.
It repeatedly compares adjacent elements and swaps them
if they are in the wrong order, hence "bubbling" the largest
element towards the end of the array with each iteration. If
the goal is to sort the array in ascending order, the
algorithm starts by comparing the first and second
elements, swapping them if necessary. Then it moves on to
compare the second and third elements, and so on, until the
entire array is sorted.
Bubble sort has an average and worst-case time complexity
of O(n^2), where n is the number of items in the array. This
means that the time it takes to sort the array grows
quadratically with the number of elements, making it
inefficient for large data sets. However, bubble sort has the
advantage of being simple to understand and implement,
making it useful for educational purposes or small data sets
where efficiency is not a primary concern.
The working of the bubble sort algorithm can be
summarized in the following steps:
1. Start with an unsorted array of elements.
2. Compare each pair of adjacent elements from the
beginning of the array.
3. If the elements are in the wrong order (e.g., the current
element is greater than the next element in case of
ascending order), swap them.
4. Repeat steps 2 and 3 until you reach the end of the array.
This ensures that the largest element "bubbles" to the end
of the array.
5. After the first pass, the largest element will be in its
correct position at the end of the array. Repeat steps 2-4 for
the remaining unsorted elements (excluding the last
element which is already in its correct position).
6. Continue the passes until the entire array is sorted. Each
pass will place the next largest element in its correct
position.
7. The array is now sorted in ascending order.
, Here is a visualization of the bubble sort process:
Initial array: [5, 3, 8, 2, 1]
Pass 1:
- Compare 5 and 3. Swap them: [3, 5, 8, 2, 1]
- Compare 5 and 8. No swap.
- Compare 8 and 2. Swap them: [3, 5, 2, 8, 1]
- Compare 8 and 1. Swap them: [3, 5, 2, 1, 8]
Pass 2:
- Compare 3 and 5. No swap.
- Compare 5 and 2. Swap them: [3, 2, 5, 1, 8]
- Compare 5 and 1. Swap them: [3, 2, 1, 5, 8]
Pass 3:
- Compare 3 and 2. Swap them: [2, 3, 1, 5, 8]
- Compare 3 and 1. Swap them: [2, 1, 3, 5, 8]
Pass 4:
- Compare 2 and 1. Swap them: [1, 2, 3, 5, 8]
The array is now sorted: [1, 2, 3, 5, 8].
Note that bubble sort is an inefficient sorting algorithm,
especially for large datasets, due to its time complexity of
O(n^2). However, it is relatively simple to understand and
implement, making it useful for educational purposes or for
small datasets where efficiency is not a major concern.
2.SELECTION SORT
Selection sort is a simple comparison-based sorting
algorithm. It works by dividing the input array into two
portions: the sorted portion and the unsorted portion. In
each iteration, the algorithm finds the minimum (or
maximum) element from the unsorted portion and swaps it
with the first element of the unsorted portion, thus
expanding the sorted portion. This process continues until
the entire array is sorted. Here's how selection sort works:
1. Start with an unsorted array of elements.
2. Iterate through the array from the beginning to the
second-to-last element:
a. Assume the current element is the minimum (or
maximum) element.
b. Compare the assumed minimum (or maximum) element
with each subsequent element in the unsorted portion.
c. If a smaller (or larger) element is found, update the
assumed minimum (or maximum) element accordingly.