Repository navigation
Expand file tree
/
Copy pathAlgorithms.cs
More file actions
74 lines (65 loc) · 2.32 KB
/
Copy pathAlgorithms.cs
File metadata and controls
74 lines (65 loc) · 2.32 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
using System;
using System.Collections.Generic;
using System.Text;
namespace SortingAlgorithms
{
class Algorithms
{/*This function does change the original array,
if you want it to not change the original array itself, you might consider modifying the function*/
public static List<int> InsertionSort(List<int> input)
{
//To hold temporary values while swapping in array
int tempNumb;
//The minimum element to be swapped and its position in the searching area
int min;
int pos = 0;
//To know if min variable has changed and needs to be swapped to left
bool isMinChanged = false;
for (int i = 0; i < input.Count; i++)
{
min = input[i];
for (int i2 = i + 1; i2 < input.Count; i2++)
{//Begin from after current position (i + 1) and search the rest of the array to find lowest value
if (input[i2] < min)
{
min = input[i2];
pos = i2;
isMinChanged = true;
}
}//If a value is found, swap its position with current position
if (isMinChanged)
{
tempNumb = input[i];
input[i] = input[pos];
input[pos] = tempNumb;
isMinChanged = false;
}
}
return input;
}
// **BUBBLE SORT**
public static List<int> BubbleSort(List<int> input)
{
List<int> result = new List<int>(input);
int totalSwaps = 0;
int tempNumb = 0;
for(int i = 0; i < result.Count; i++)
{
totalSwaps = 0;
for(int i2 = 0; i2 < input.Count - 1; i2++)
{
if(result[i2] > result[i2 + 1])
{
tempNumb = result[i2];
result[i2] = result[i2 + 1];
result[i2 + 1] = tempNumb;
totalSwaps++;
}
}
if (totalSwaps == 0)
break;
}
return result;
}
}
}