Simplest sort algorithm
- In bubble sort you see if two things are the wrong way round and it they are then you swap them. This does that (sort of) the “wrong way round”.
- In Bubble Sort your indices are always i<j … here they aren’t.
- In Bubble Sort you only ever compare adjacent items. Here you don’t.
- This really, really isn’t Bubble Sort, unoptimised or other.
void sort() {
for( int i = 0; i < N; i++)
for( int j = 0; j < N; j++)
if( id[i] < id[j])
swap( i, j);
}
see also
- Quicksort Algorithm in Five Lines of Code! - but not in place
Written on October 14, 2022, Last update on March 15, 2025
sort