Skip to content

Computer Science · Ch 5 — Sorting

Summary

Summary

  • Sorting: The process of arranging a collection of elements in a particular order — ascending or descending for numbers, alphabetical for strings — making the elements easier to search and retrieve.

  • Bubble Sort: Repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. For a list of n elements, it makes n-1 passes, with the largest unsorted element settling into its correct position at the end of the list after each pass.

  • Selection Sort: Divides the list into a sorted part and an unsorted part. In each of n-1 passes, it finds the smallest element in the unsorted part and swaps it into the leftmost position of the unsorted part, growing the sorted part from left to right.

  • Insertion Sort: Takes each element from the unsorted part, one at a time, and inserts it into its correct position within the already-sorted part — similar to how a player picks up cards one by one and inserts each into its correct place in an already-sorted hand. …