Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rmres committed Sep 8, 2020
1 parent eddb0c7 commit c8bb54e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 30daysofcode/day20.cs
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;
}
}
39 changes: 39 additions & 0 deletions 30daysofcode/day21.cs
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);
}
}

0 comments on commit c8bb54e

Please sign in to comment.