# Hackerrank “30 Days of Code” Çözümleri – Day 20: Sorting

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 20: Sorting” sorusunun açıklaması ve çözümü. Bu soruda bubble sort kavramına göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 20: Sorting: https://www.hackerrank.com/challenges/30-sorting/problem

► Problem açıklaması:

Objective

Today, we’re discussing a simple sorting algorithm called Bubble Sort. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given an array, a, of size n distinct elements, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following 3 lines:

Array is sorted in numSwaps swaps.

where numSways is the number of swaps that took place.

First Element: firstElement

where firstElement is the first element in the sorted array.

Last Element: lastElement

where lastElement is the last element in the sorted array.

Hint: To complete this challenge, you will need to add a variable that keeps a running tally of all swaps that occur during execution.

Example a = [4, 3, 1, 2]

original a: 4 3 1 2

round 1 a: 3 1 2 4 swaps this round: 3

round 2 a: 1 2 3 4 swaps this round: 2

round 3 a: 1 2 3 4 swaps this round: 0

In the first round, the is swapped at each of the comparisons, ending in the last position. In the second round, the is swapped at of the comparisons. Finally, in the third round, no swaps are made so the iterations stop. The output is the following:

Array is sorted in 5 swaps.

First Element: 1

Last Element: 4

Input Format

The first line contains an integer, , the number of elements in array . The second line contains space-separated integers that describe .

Output Format

Print the following three lines of output:

Array is sorted in numSwaps swaps.

where is the number of swaps that took place.

First Element: firstElement

where is the first element in the sorted array.

Last Element: lastElement

where is the last element in the sorted array.

Sample Input 0

3

1 2 3

Sample Output 0

Array is sorted in 0 swaps.

First Element: 1

Last Element: 3

Explanation 0

The array is already sorted, so swaps take place and we print the necessary lines of output shown above.

Sample Input 1

3

3 2 1

Sample Output 1

Array is sorted in 3 swaps.

First Element: 1

Last Element: 3

# Rekabetçi Programlama El Kitabı içerisinde Sorting kısmına giriş yaptık!

Bu Twitch yayınında rekatbeçi programlama el kitabı olarak bilinen “Competitive Programmer’s Handbook” kitabındaki “Sorting” kısmına giriş yaptık.

3. Kısım: Sorting

➡️ Sorting theory

➡️ O ( n^2 ) algorithms

➡️ Inversions

➡️ O ( n log n ) algorithms

➡️ Sorting lower bound

➡️ Counting sort