Algorithms and Programming Concepts: Week 11
Sorting Algorithms
Road Map
• Introduction
• Selection sort
• Bubble sort
sorting
algorithms
• A Sorting Algorithm is used to rearrange a given set of
elements in a specific order
• Sorting is a fundamental operation in CS to reduce the
complexity of operations
• Essential for efficient data management
• It involves arranging data in a specific order, typically
ascending or descending. Sorted data is easier to search,
visualize, and process in various applications.
sorting
algorithms
• Importance of sorting algorithms:
⚬ Facilitates faster searching and retrieval.
⚬ Helps in organizing data for efficient storage and
display.
⚬ Aids in preprocessing data for algorithms like binary
search.
Basic Types of Sorting
Algorithms
• Comparison-based Sorting: Involves
comparing elements to decide their
order (e.g., Bubble Sort, Quick Sort,
Merge Sort).
• Non-comparison Sorting: Uses
properties of data to sort without direct
comparison (e.g., Counting Sort, Radix
Sort).
sorting
algorithms
• Purpose of sorting algorithms:
⚬ Optimized Searching: Many algorithms (like binary
search) require sorted input.
⚬ Data Analysis: Makes data patterns easier to observe.
⚬ Data Preparation: Essential for tasks like data
merging or statistical analysis.
⚬ Algorithm Foundation: Sorting is a building block for
many other algorithms.
Applications of Sorting
• Databases: To organize and retrieve data quickly.
• E-commerce: Arranging products by price, popularity, or
rating.
• Computer Graphics: Rendering objects in a particular
sequence.
• Networking: Managing packet sequences in
communication protocols.
Selection
sort
Selection sort
Concept: Repeatedly it sorts a collection of data by selecting the
smallest (or largest) element from the unsorted portion and
swapping it with the first unsorted element.
Steps:
• find the smallest element and swap it with the first element [of
the unsorted list]. (To get the smallest element at its correct
position)
• find the smallest among remaining elements (or second
smallest) and move it to its correct position by swapping
• repeat the above steps until the data set is sorted completely
Selection sort steps
Selection sort steps
Selection sort steps
Selection Sort
Algorithms
Selection
Sort
Complexity
Bubble sort
Bubble sort
Concept: Repeatedly swap adjacent elements if they are in the
wrong order, effectively "bubbling" larger elements to the end.
• Simplest form of sorting algorithm
Steps:
• Compare adjacent elements.
• Swap them if they are in the wrong order.
• Continue until the array is sorted.
After the first pass, the maximum element goes to end (its correct
position). Same way, after second pass, the second largest element
goes to second last position and so on
Bubble sort steps
Bubble sort steps
bubble Sort Algorithm
bubble
Sort
Complexity