Have you ever wondered how large amounts of data are organized so quickly and efficiently? Sorting algorithms play a crucial role in arranging data in a specific order, making it easier to search, analyze, and manage. Sorting is fundamental to computer science because it lays the groundwork for more complex algorithms that require data to be in a particular sequence. In this lesson, we’ll explore two fundamental sorting algorithms: Selection Sort and Bubble Sort. These algorithms provide a foundation for understanding more advanced sorting techniques.
The selection sort algorithm is a simple yet powerful way to sort elements in a list or array by repeatedly finding the smallest (or largest) element from the unsorted portion and moving it to the beginning of the list.
Steps in Selection Sort
void SelectionSort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[minIndex])
{
minIndex = j; // Update minimum element index
}
}
// Swap the minimum element with the first unsorted element
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
Demo
int[] numbers = { 64, 25, 12, 22, 11 };
SelectionSort(numbers);
foreach (int num in numbers)
{
Console.Write(num + " ");
}
11 12 22 25 64
Selection Sort Card Demo
The bubble sort algorithm is another straightforward way to sort elements in a list or array by repeatedly “bubbling” the largest unsorted element to the end of the list in each pass. Although not the most efficient, it’s valuable for understanding basic sorting principles.
Steps in Bubble Sort
void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
// Swap adjacent elements if they’re out of order
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
Demo
int[] numbers = { 64, 34, 25, 12, 22, 11, 90 };
BubbleSort(numbers);
foreach (int num in numbers)
{
Console.Write(num + " ");
}
11 12 22 25 34 64 90
Bubble Sort Card Demo