-
Notifications
You must be signed in to change notification settings - Fork 183
/
21 - Day 20 - Sorting.cs
46 lines (36 loc) · 1.28 KB
/
21 - Day 20 - Sorting.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/30-sorting/problem
// Difficulty: Easy
// Max Score: 30
// Language: C#
// ========================
// Solution
// ========================
using System;
class Solution {
static void Main(String[] args) {
int numberInput = Convert.ToInt32(Console.ReadLine());
string[] arrayInput = Console.ReadLine().Split(' ');
int[] numbersArray = Array.ConvertAll(arrayInput, Int32.Parse);
// Write Your Code Here
int numberOfSwaps = 0;
for (int i = 0; i < numberInput; i++) {
for (int j = 0; j < numberInput - 1; j++) {
if (numbersArray[j] > numbersArray[j + 1]) {
Array.Reverse(numbersArray, j, 2);
numberOfSwaps++;
}
}
if (numberOfSwaps == 0) {
break;
}
}
int firstPosition = numbersArray[0];
int lastPosition = numbersArray[numbersArray.Length - 1];
Console.WriteLine($"Array is sorted in {numberOfSwaps} swaps.");
Console.WriteLine($"First Element: {firstPosition}");
Console.WriteLine($"Last Element: {lastPosition}");
}
}