-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
class Solution { | ||
|
||
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); | ||
// Write Your Code Here | ||
|
||
int numberOfSwaps = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
// Track number of elements swapped during a single array traversal | ||
|
||
|
||
for (int j = 0; j < n - 1; j++) { | ||
// Swap adjacent elements if they are in decreasing order | ||
if (a[j] > a[j + 1]) { | ||
swap(a, j, j + 1); | ||
numberOfSwaps++; | ||
} | ||
} | ||
|
||
// If no elements were swapped during a traversal, array is sorted | ||
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]); | ||
|
||
} | ||
public static void swap(int[] a, int a1, int a2) { | ||
int doswap = a[a1]; | ||
a[a1] = a[a2]; | ||
a[a2] = doswap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
|
||
class Printer | ||
{ | ||
|
||
/** | ||
* Name: PrintArray | ||
* Print each element of the generic array on a new line. Do not return anything. | ||
* @param A generic array | ||
**/ | ||
// Write your code here | ||
|
||
static void PrintArray<T>(T[] arr) { | ||
for (int i = 0; i < arr.Length; i++) { | ||
Console.WriteLine(arr[i]); | ||
} | ||
} | ||
|
||
|
||
static void Main(string[] args) | ||
{ | ||
int n = Convert.ToInt32(Console.ReadLine()); | ||
int[] intArray = new int[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
intArray[i] = Convert.ToInt32(Console.ReadLine()); | ||
} | ||
|
||
n = Convert.ToInt32(Console.ReadLine()); | ||
string[] stringArray = new string[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
stringArray[i] = Console.ReadLine(); | ||
} | ||
|
||
PrintArray<Int32>(intArray); | ||
PrintArray<String>(stringArray); | ||
} | ||
} |