# (N^2-N)/2

URL:: https://brilliant.org/courses/computer-science-algorithms/the-speed-of-algorithms-2/counting-operations/8/
Author:: brilliant.org
## Highlights
> For most sorting algorithms, the cost is described in terms of the **number of comparisons between array elements**. As you saw in these examples, the cost in terms of swaps is misleadingly low for selection sort.
> The number of comparisons, on the other hand, will *never* be misleadingly low because any well-designed sorting algorithm will only swap elements after comparing elements.
> Why? Because, without a comparison first, there's no way for an algorithm to know if a swap is even useful! There will always be more comparisons than swaps. ([View Highlight](https://read.readwise.io/read/01g9zt0cjktqnj8v2jvx3z55as))
---
Title: (N^2-N)/2
Author: brilliant.org
Tags: readwise, articles
date: 2024-01-30
---
# (N^2-N)/2

URL:: https://brilliant.org/courses/computer-science-algorithms/the-speed-of-algorithms-2/counting-operations/8/
Author:: brilliant.org
## AI-Generated Summary
Selection sort as we have presented it here is a very predictable algorithm: on an array of length n, it performs exactly (n−1) swap instructions and exactly (n2−n)/2 comparisons.
## Highlights
> For most sorting algorithms, the cost is described in terms of the **number of comparisons between array elements**. As you saw in these examples, the cost in terms of swaps is misleadingly low for selection sort.
> The number of comparisons, on the other hand, will *never* be misleadingly low because any well-designed sorting algorithm will only swap elements after comparing elements.
> Why? Because, without a comparison first, there's no way for an algorithm to know if a swap is even useful! There will always be more comparisons than swaps. ([View Highlight](https://read.readwise.io/read/01g9zt0cjktqnj8v2jvx3z55as))