-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortingAlgorithms.cs
202 lines (174 loc) · 6.26 KB
/
SortingAlgorithms.cs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Linq;
namespace dojo
{
public static class SortingAlgorithms
{
public static IList<int> SelectionSort(IList<int> source)
{
for (var currentIndex = 0; currentIndex < source.Count - 1; currentIndex++)
{
var leastValueIndex = currentIndex;
for (var innerLoopIndex = currentIndex; innerLoopIndex < source.Count; innerLoopIndex++)
{
if (source[innerLoopIndex] < source[leastValueIndex])
{
leastValueIndex = innerLoopIndex;
}
}
if (leastValueIndex != currentIndex)
{
SwapTwoElementsInAnArray(source, leastValueIndex, currentIndex);
}
}
return source;
}
public static IList<int> InsertSort(IList<int> source)
{
for (var currentIndex = 1; currentIndex < source.Count; currentIndex++)
{
var previousIndex = currentIndex - 1;
var currentValue = source[currentIndex];
while (previousIndex >= 0 && currentValue < source[previousIndex])
{
SwapTwoElementsInAnArray(source, previousIndex,
previousIndex + 1);
previousIndex--;
}
}
return source;
}
public static IList<int> QuickSort(IList<int> source, int startIndex, int endIndex)
{
if (startIndex > endIndex)
{
throw new ArgumentException("starting index out of range");
}
var midPoint = (int)Math.Floor((endIndex + startIndex) / 2.0);
var pivot = source[midPoint];
var leftPointer = startIndex;
var rightPointer = endIndex;
while (leftPointer <= rightPointer)
{
while (pivot > source[leftPointer])
{
leftPointer++;
}
while (pivot < source[rightPointer])
{
rightPointer--;
}
if (leftPointer <= rightPointer)
{
SwapTwoElementsInAnArray(source, leftPointer, rightPointer);
leftPointer++;
rightPointer--;
}
}
if (startIndex < rightPointer)
{
QuickSort(source, startIndex, rightPointer);
}
if (leftPointer < endIndex)
{
QuickSort(source, leftPointer, endIndex);
}
return source;
}
public static IList<int> BubbleSort(IList<int> source)
{
var arrLength = source.Count;
for (var i = 0; i < arrLength; i++)
{
for (var j = 0; j < arrLength - 1 - i; j++)
{
if (source[j] > source[j + 1])
{
SwapTwoElementsInAnArray(source, j, j + 1);
}
}
}
return source;
}
public static IList<int> MergeSortAlgorithm(int[] source)
{
if (source.Length == 1)
{
return source;
}
var midPoint = (int)Math.Floor(source.Length / 2.0);
return MergeTwoSortedArrays(MergeSortAlgorithm(source[0..midPoint]), MergeSortAlgorithm(source[midPoint..]));
}
private static void SwapTwoElementsInAnArray(IList<int> source, int firstIndex, int secondIndex)
{
if (firstIndex >= source.Count || secondIndex >= source.Count || firstIndex < 0 || secondIndex < 0)
{
throw new ArgumentException("indices can not be larger than the size of array or less than 0");
}
var temp = source[firstIndex];
source[firstIndex] = source[secondIndex];
source[secondIndex] = temp;
}
private static IEnumerable<int> MergeTwoArrays(IList<int> left, IList<int> right)
{
var result = new List<int>();
while (left.Any() && right.Any())
{
if (left[0] < right[0])
{
result.Add(left[0]);
left.RemoveAt(0);
}
else
{
result.Add(right[0]);
right.RemoveAt(0);
}
}
result = AddElementsToAnArray(result, left);
result = AddElementsToAnArray(result, right);
return result;
}
private static List<int> AddElementsToAnArray(List<int> source, IList<int> toTransfer)
{
if (!toTransfer.Any())
{
return source;
}
source.AddRange(toTransfer);
return source;
}
public static IList<int> MergeTwoSortedArrays(IList<int> first, IList<int> second)
{
var resultArray = new List<int>();
var firstArrayIndex = 0;
var secondArrayIndex = 0;
while (firstArrayIndex < first.Count && secondArrayIndex < second.Count)
{
if (first[firstArrayIndex] <= second[secondArrayIndex])
{
resultArray.Add(first[firstArrayIndex]);
firstArrayIndex++;
}
else
{
resultArray.Add(second[secondArrayIndex]);
secondArrayIndex++;
}
}
AddRemainingElementsToSortedArray(resultArray, first, firstArrayIndex);
AddRemainingElementsToSortedArray(resultArray, second, secondArrayIndex);
return resultArray;
}
private static void AddRemainingElementsToSortedArray(ICollection<int> destination, IList<int> source,
int indexToStartFrom)
{
while (indexToStartFrom < source.Count)
{
destination.Add(source[indexToStartFrom]);
indexToStartFrom++;
}
}
}
}