AlgoFrames

How many swaps?

Standard · Write code · about 10 min

Return how many swaps bubble sort makes while sorting nums in ascending order. Equal values never swap.

Example 1

Input:

[[3,5,2,4,1]]

Output:

7

Example 2

Input:

[[1,2,3]]

Output:

0

Sorted input needs no swaps.

Constraints

  • 0 ≤ nums.length ≤ 1,000
  • -1,000 ≤ nums[i] ≤ 1,000

Learn the idea first: Bubble sort, a visual lesson.

More Bubble sort problems