Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 88 additions & 7 deletions Src/BootCamp.Chapter1/ArrayOperations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace BootCamp.Chapter1
using System;

namespace BootCamp.Chapter1
{
public static class ArrayOperations
{
Expand All @@ -10,6 +12,35 @@ public static class ArrayOperations
public static void Sort(int[] array)
{
// ToDo: implement.
if (array is null || array.Length == 0)
return;

var isArraySorted = IsSorted(array);
if (isArraySorted is false)
SortArray(array);
}

private static bool IsSorted(int[] array)
{
for (int i = 0; i < array.Length - 1; i++)
{
if ((array[i] > array[i + 1]))
return false;
}
return true;
}

private static void SortArray(int[] array)
{
var length = array.Length;
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - i - 1; j++)
{
if (array[j] > array[j + 1])
(array[j + 1], array[j]) = (array[j], array[j + 1]);
}
}
}

/// <summary>
Expand All @@ -20,6 +51,12 @@ public static void Sort(int[] array)
public static void Reverse(int[] array)
{
// ToDo: implement.
if (array is null || array.Length == 0)
return;
for (int i = 0; i < array.Length / 2; i++)
{
(array[i], array[^(i + 1)]) = (array[^(i + 1)], array[i]);
}
}

/// <summary>
Expand All @@ -30,7 +67,11 @@ public static void Reverse(int[] array)
public static int[] RemoveLast(int[] array)
{
// ToDo: implement.
return array;
if (array is null || array.Length == 0)
return array;
var newArray = new int[array.Length - 1];
Array.Copy(array, newArray, newArray.Length);
return newArray;
}

/// <summary>
Expand All @@ -40,7 +81,11 @@ public static int[] RemoveLast(int[] array)
public static int[] RemoveFirst(int[] array)
{
// ToDo: implement.
return array;
if (array is null || array.Length == 0)
return array;
var newArray = new int[array.Length - 1];
Array.Copy(array, 1, newArray, 0, newArray.Length);
return newArray;
}

/// <summary>
Expand All @@ -52,7 +97,17 @@ public static int[] RemoveFirst(int[] array)
public static int[] RemoveAt(int[] array, int index)
{
// ToDo: implement.
return array;
if (array is null || array.Length == 0)
return array;
if (index > array.Length - 1 || index < 0)
return array;
var newArray = new int[array.Length - 1];
for (int i = 0, j = 0; i < array.Length; i++)
{
if (i == index) continue;
newArray[j++] = array[i];
}
return newArray;
}

/// <summary>
Expand All @@ -64,7 +119,15 @@ public static int[] RemoveAt(int[] array, int index)
public static int[] InsertFirst(int[] array, int number)
{
// ToDo: implement.
return array;
if (array is null || array.Length == 0)
return new int[] { number };

var newArray = new int[array.Length + 1];
newArray[0] = number;
for (int i = 1, j = 0; i < newArray.Length; i++)
newArray[i] = array[j++];

return newArray;
}

/// <summary>
Expand All @@ -76,7 +139,12 @@ public static int[] InsertFirst(int[] array, int number)
public static int[] InsertLast(int[] array, int number)
{
// ToDo: implement.
return array;
if (array is null || array.Length == 0)
return new int[] { number };
var newArray = new int[array.Length + 1];
array.CopyTo(newArray, 0);
newArray[^1] = number;
return newArray;
}

/// <summary>
Expand All @@ -89,7 +157,20 @@ public static int[] InsertLast(int[] array, int number)
public static int[] InsertAt(int[] array, int number, int index)
{
// ToDo: implement.
return array;
if (index < 0 || index > (array is null ? 0 : array.Length))
return array;
if (array is null || array.Length == 0)
return new int[] { number };

var newArray = new int[array.Length + 1];
newArray[index] = number;
for (int i = 0, j = 0; i < newArray.Length; i++)
{
if (i == index)
continue;
newArray[i] = array[j++];
}
return newArray;
}
}
}