Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ-Tuhin committed Mar 31, 2018
1 parent 9731df9 commit 86231ae
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 30_Days-of_Code_Challenges/Day20Sorting/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day20Sorting
{
class Program
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp, Int32.Parse);

int numberOfSwaps = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
numberOfSwaps++;
}
}

if (numberOfSwaps == 0)
{
break;
}
}

Console.WriteLine("Array is sorted in {0} swaps.", numberOfSwaps);
Console.WriteLine("First Element: {0}", a[0]);
Console.WriteLine("Last Element: {0}", a[a.Length - 1]);
Console.ReadKey();
}
}
}

0 comments on commit 86231ae

Please sign in to comment.