-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathArrayOperations.cs
More file actions
176 lines (162 loc) · 6.38 KB
/
ArrayOperations.cs
File metadata and controls
176 lines (162 loc) · 6.38 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
namespace BootCamp.Chapter1
{
public static class ArrayOperations
{
/// <summary>
/// Sort the array in ascending order.
/// If array empty or null- don't do anything.
/// </summary>
/// <param name="array">Input array in a random order.</param>
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>
/// Reverse the array elements, first being last and so on.
/// If array empty or null- don't do anything.
/// </summary>
/// <param name="array">Input array in a random order.</param>
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>
/// Remove last element in array.
/// </summary>
/// <param name="array">Input array.</param>
/// <returns>A new array with the last element removed. If an array is empty or null, returns input array.</returns>
public static int[] RemoveLast(int[] array)
{
// ToDo: implement.
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>
/// Remove first element in array.
/// </summary>
/// <returns>A new array with the first element removed. If an array is empty or null, returns input array.</returns>
public static int[] RemoveFirst(int[] array)
{
// ToDo: implement.
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>
/// Removes array element at given index.
/// </summary>
/// <param name="array">Input array.</param>
/// <param name="index">Index at which array element should be removed.</param>
/// <returns>A new array with element removed at a given index. If an array is empty or null, returns input array.</returns>
public static int[] RemoveAt(int[] array, int index)
{
// ToDo: implement.
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>
/// Inserts a new array element at the start.
/// </summary>
/// <param name="array">Input array.</param>
/// <param name="number">Number to be added.</param>
/// <returns>A new array with element added at a given index. If an array is empty or null, returns new array with number in it.</returns>
public static int[] InsertFirst(int[] array, int number)
{
// ToDo: implement.
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>
/// Inserts a new array element at the end.
/// </summary>
/// <param name="array">Input array.</param>
/// <param name="number">Number to be added.</param>
/// <returns>A new array with element added in the end of array. If an array is empty or null, returns new array with number in it.</returns>
public static int[] InsertLast(int[] array, int number)
{
// ToDo: implement.
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>
/// Inserts a new array element at the specified index.
/// </summary>
/// <param name="array">Input array.</param>
/// <param name="number">Number to be added.</param>
/// <param name="index">Index at which array element should be added.</param>
/// <returns>A new array with element inserted at a given index. If an array is empty or null, returns new array with number in it.</returns>
public static int[] InsertAt(int[] array, int number, int index)
{
// ToDo: implement.
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;
}
}
}