explain sorting principles with examples
Sigiloso
Bubble Sort: Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It has a worst-case and average-case time complexity of O(n^2). Selection Sort: Selection sort divides the array into a sorted and an unsorted region. It repeatedly selects the smallest (or largest) element from the unsorted region and moves it to the beginning of the sorted region. It has a worst-case and average-case time complexity of O(n^2). Insertion Sort: Insertion sort builds the final sorted array one element at a time. It iterates over each element and inserts it into its correct position in the already sorted part of the array. It has a worst-case time complexity of O(n^2) but performs well on small datasets and is efficient for nearly sorted arrays.