Sorting
Lesson 25 of 25 in Coddy's Coding Problems: Volume 2 course.
In modern programming, there are many different methods of sorting & searching. We have bubble sort, merge sort, etc. If you're able to switch only two adjacent elements from a sequence, or basically switch two neighbors, output the total amounts of adjacent switches you need to make to sort the sequence
Challenge
HardWrite a program that reads a natural number N from standard input. In the following line, input N numbers representing the sequence. Keep doing adjacent switches in the sequence until it's sorted from smallest to largest number. Output the total number of adjacent switches needed to sort the sequence
Input
5
4 2 1 5 3
Output
5
Explanation
[4 | 2] 2 4 1 5 3
[4 | 1] 2 1 4 5 3
[1 | 2] 1 2 4 5 3
[5 | 3] 1 2 4 3 5
[3 | 4] 1 2 3 4 5
Try it yourself
#include <stdio.h>
int main() {
// Write code here
return 0;
}All lessons in Coding Problems: Volume 2
1Course Introduction
Introduction4Hard Difficulty
Digit SumReverse MatrixVowel PairsMagical SquareHappy SubsequenceTriple DivisionSorting