Skip to content

Commit a208d7e

Browse files
ClémentClément
authored andcommitted
Adding actions, working on sorting.
1 parent 9ae003d commit a208d7e

4 files changed

Lines changed: 81 additions & 148 deletions

File tree

source/code/projects/Actions/Actions/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static void Main()
3535
Action<int[]> display_array_variable = ExampleActions<int>.DisplayArray;
3636
display_array_variable(arrayT);
3737

38-
//
38+
// Passing an Action as an argument:
3939
CallingAction.Demo(ExampleActions.Test);
40-
}
40+
}
41+
4142
}

source/code/projects/Sorting/Sorting/Displaying.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
using System;
22
using System.Collections.Generic;
33

4-
54
static class Displaying<T> where T : IComparable<T>
65
{
6+
public static void List(List<T> listP)
7+
{
8+
Console.Write("Index ");
9+
for (int i = 0; i < listP.Count; i++)
10+
Console.Write("|{0,3}", i);
11+
Console.Write("|\n ");
12+
for (int i = 0; i < listP.Count; i++)
13+
Console.Write("|---", i);
14+
Console.WriteLine("|");
15+
Console.Write("Before");
16+
for (int i = 0; i < listP.Count; i++)
17+
Console.Write("|{0,3}", listP[i]);
18+
Console.WriteLine("|");
19+
}
20+
21+
public static void After(List<T> listP)
22+
{
23+
Console.Write("After ");
24+
for (int i = 0; i < listP.Count; i++)
25+
Console.Write("|{0,3}", listP[i]);
26+
Console.Write("|");
27+
if (Sorting<T>.IsSorted(listP))
28+
{
29+
Console.Write(" Is sorted: ✓\n");
30+
}
31+
else
32+
{
33+
throw new Exception("Sorting method is not correct!");
34+
}
35+
}
36+
737
public static void DisplayHeader(List<T> aList, int start, int stop)
838
{
939
for (int i = start; i < stop; ++i)

source/code/projects/Sorting/Sorting/Program.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ class Program
55
{
66
static void Main(string[] args)
77
{
8-
List<int> test = new List<int>();
9-
Random gen = new Random();
10-
for (int i = 0; i < 20; i++)
11-
{
12-
test.Add(gen.Next(50));
13-
}
14-
Displaying<int>.DisplayHeader(test, 0, test.Count);
15-
Displaying<int>.Display(test);
16-
Sorting<int>.InsertionSort(test);
8+
Demo.Run(Sorting<int>.InsertionSort);
9+
Demo.Run(Sorting<char>.InsertionSort);
10+
11+
Demo.Run(Sorting<int>.Heapsort);
12+
Demo.Run(Sorting<char>.Heapsort);
13+
14+
15+
Demo.Run(Sorting<int>.BubbleSort);
16+
Demo.Run(Sorting<char>.BubbleSort);
17+
18+
Demo.Run(Sorting<int>.ShellSort);
19+
Demo.Run(Sorting<char>.ShellSort);
1720

18-
test.Clear();
19-
for (int i = 0; i < 20; i++)
20-
{
21-
test.Add(gen.Next(50));
22-
}
23-
Sorting<int>.Heapsort(test);
2421
}
2522
}

source/code/projects/Sorting/Sorting/Sorting.cs

Lines changed: 35 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,47 @@
22
using System.Collections.Generic;
33

44
static class Sorting<T> where T : IComparable<T>
5+
{
6+
7+
// Helper method
8+
private static void Swap(List<T> listP, int lhs, int rhs)
59
{
6-
7-
// Insertion Algorithm
8-
public static void InsertionSort(List<T> listP)
10+
T temp = listP[lhs];
11+
listP[lhs] = listP[rhs];
12+
listP[rhs] = temp;
13+
}
14+
public static bool IsSorted(List<T> listP)
15+
{
16+
bool isSorted = true;
17+
for (int i = 0; i < listP.Count-1; i++)
918
{
10-
int swapOperations = 0;
11-
// Can be ignored, is simply here
12-
// to count number of time we
13-
// swap values.
14-
Console.WriteLine("----------- Insertion Sort -------");
15-
Displaying<T>.DisplayHeader(listP, 0, listP.Count);
19+
if (listP[i].CompareTo(listP[i + 1]) > 0){
20+
isSorted = false;
21+
}
22+
}
23+
return isSorted;
24+
}
25+
26+
// Done with helper method.
1627

28+
// Insertion Algorithm
29+
public static void InsertionSort(List<T> listP)
30+
{
1731
T current;
1832
int slot;
1933
for (int bar = 1; bar < listP.Count; bar++)
2034
{
2135
current = listP[bar];
2236
for (slot = bar; slot > 0 && current.CompareTo(listP[slot - 1]) < 0; slot--)
2337
{
24-
swapOperations++;
2538
listP[slot] = listP[slot - 1];
2639
}
2740
listP[slot] = current;
2841
}
29-
30-
Displaying<T>.Display(listP);
31-
Console.WriteLine("Count = {0}", swapOperations);
3242
}
3343
// Done with insertion Algorithm
3444

3545
// Helper methods for Heapsort
36-
private static void Swap(List<T> listP, int lhs, int rhs)
37-
{
38-
T temp = listP[lhs];
39-
listP[lhs] = listP[rhs];
40-
listP[rhs] = temp;
41-
}
42-
4346
private static int LeftChild(int i)
4447
{
4548
return 2 * i + 1;
@@ -49,32 +52,24 @@ private static int LeftChild(int i)
4952
// Heapsort Algorithm
5053
public static void Heapsort(List<T> listP)
5154
{
52-
Console.WriteLine(" --- Starting HeapSort ----");
5355
Heapsort(listP, listP.Count);
5456
}
5557

5658
private static void Heapsort(List<T> listP, int N)
5759
{
58-
Displaying<T>.DisplayHeader(listP, 0, listP.Count);
59-
Displaying<T>.Display(listP);
60-
6160
for (int i = N / 2; i >= 0; i--) /* BuildHeap */
6261
PercDown(listP, i, N);
63-
Console.WriteLine("-- Max Heap is built --");
64-
Displaying<T>.Display(listP);
6562
for (int i = N - 1; i > 0; i--)
6663
{
6764
Swap(listP, 0, i); /* DeleteMax */
6865
PercDown(listP, 0, i);
69-
Displaying<T>.Display(listP);
7066
}
7167
}
7268

7369
private static void PercDown(List<T> listP, int i, int N)
7470
{
7571
int Child;
7672
T current;
77-
7873
for (current = listP[i]; LeftChild(i) < N; i = Child)
7974
{
8075
Child = LeftChild(i);
@@ -92,132 +87,42 @@ private static void PercDown(List<T> listP, int i, int N)
9287

9388
// Done with heapsort Algorithm
9489

95-
96-
97-
98-
99-
// ================================================================================
100-
//
101-
// Bubble Algorithm
102-
//
103-
// ================================================================================
90+
// Bubble Algorithm
10491
public static void BubbleSort(List<T> listP)
10592
{
106-
int count = 0;
107-
Console.WriteLine("----------- Bubble Sort --- Green - bubbled to correct spot starting at end");
108-
Displaying<T>.DisplayHeader(listP, 0, listP.Count);
109-
Displaying<T>.Display(listP);
110-
111-
for (int i = listP.Count - 1; i >= 0; i--)
93+
for (int i = listP.Count - 1; i >= 0; i--)
11294
{
11395
for (int j = 0; j < listP.Count - 1; j++)
11496
{
11597
if (listP[j].CompareTo(listP[j + 1]) > 0)
11698
Swap(listP, j, j + 1);
117-
count++;
11899
}
119-
Displaying<T>.DisplayOne(listP, i, 0, i + 1);
120100
}
121-
122-
Displaying<T>.Display(listP);
123-
Console.WriteLine("Count = {0}", count);
124101
}
102+
// Done with bubble algorithm.
103+
104+
// ShellSort Algorithm
125105

126-
// ================================================================================
127-
//
128-
// ShellSort Algorithm
129-
//
130-
// ================================================================================ public static void ShellSort(List<T> listP)
131106
public static void ShellSort(List<T> listP)
132107
{
133-
int swapNum = 0;
134-
Console.WriteLine();
135-
Console.Write(" ");
136-
Displaying<T>.DisplayHeader(listP, 0, listP.Count);
137-
138108
int slot;
139-
T tmp;
140-
for (int gap = listP.Count / 3 + 1; gap > 0; gap /= 2) // determines increment sequence
109+
T current;
110+
for (int gap = listP.Count / 3 + 1; gap > 0; gap /= 2)
141111
{
142-
Console.Write("{0} ", gap);
143-
Displaying<T>.Display2(listP, 0, listP.Count);
144-
for (int next = gap; next < listP.Count; next++)
145-
{ // goes thru array by steps
146-
tmp = listP[next];
147-
for (slot = next; slot >= gap && tmp.CompareTo(listP[slot - gap]) < 0; slot -= gap) // slides tmp until in place
148-
{
149-
++swapNum;
150-
listP[slot] = listP[slot - gap];
151-
152-
}
153-
listP[slot] = tmp;
154-
}
155-
}
156-
Console.WriteLine("Count = {0}", swapNum);
157-
}
158112

159-
// gaps must be sorted larger to smaller
160-
public static void ShellSort(List<T> listP, int[] gaps)
161-
{
162-
Console.Write("---- ShellSort -----");
163-
T tmp;
164-
int slot;
165-
int count = 0;
166-
Console.WriteLine();
167-
Console.Write(" ");
168-
Displaying<T>.DisplayHeader(listP, 0, listP.Count);
169-
Console.Write(" ");
170-
Displaying<T>.Display(listP);
171-
foreach (int gap in gaps) // determines increment sequence
172-
{
173-
Console.Write("{0} ", gap);
174113
for (int next = gap; next < listP.Count; next++)
175-
{ // goes thru array by steps
176-
tmp = listP[next];
177-
for (slot = next; slot >= gap && tmp.CompareTo(listP[slot - gap]) < 0; slot -= gap) // slides tmp until in place
114+
{
115+
// goes thru array by steps
116+
current = listP[next];
117+
for (slot = next; slot >= gap && current.CompareTo(listP[slot - gap]) < 0; slot -= gap) // slides current until in place
178118
{
179-
++count;
180119
listP[slot] = listP[slot - gap];
181-
182120
}
183-
listP[slot] = tmp;
121+
listP[slot] = current;
184122
}
185-
Displaying<T>.DisplayEvery(listP, 0, listP.Count, gap);
186123
}
187-
Console.WriteLine("Number of shifts = {0}", count);
188124
}
189125

190-
public static void ShellSort(List<T> listP, List<int> IncrementList)
191-
{
192-
T tmp;
193-
int slot;
194-
int count = 0;
195-
196-
Console.Write(" ");
197-
Displaying<T>.DisplayHeader(listP, 0, listP.Count);
198-
Console.Write(" ");
199-
Displaying<T>.Display2(listP, 0, listP.Count);
200-
201-
for (int incNumber = IncrementList.Count - 1; incNumber >= 0; incNumber--)
202-
{
203-
int gap = IncrementList[incNumber];
204-
205-
for (int next = gap; next < listP.Count; next++)
206-
{ // goes thru array by steps
207-
tmp = listP[next];
208-
for (slot = next; slot >= gap && tmp.CompareTo(listP[slot - gap]) < 0; slot -= gap) // slides tmp until in place
209-
{
210-
++count;
211-
listP[slot] = listP[slot - gap];
212-
}
213-
listP[slot] = tmp;
214-
// Console.WriteLine("Count = {0}", count);
215-
}
216-
Console.Write("{0,3} ", gap);
217-
Displaying<T>.Display2(listP, 0, listP.Count);
218-
}
219-
Console.WriteLine("Count = {0}", count);
220-
}
221126

222127

223128

0 commit comments

Comments
 (0)