Skip to content

Commit a2591e6

Browse files
ClémentClément
authored andcommitted
First notes on sorting algorithms.
1 parent 9ace7e6 commit a2591e6

4 files changed

Lines changed: 519 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
5+
static class Displaying<T> where T : IComparable<T>
6+
{
7+
public static void DisplayHeader(List<T> aList, int start, int stop)
8+
{
9+
for (int i = start; i < stop; ++i)
10+
Console.Write("[{0,2}]", i);
11+
Console.WriteLine();
12+
}
13+
14+
public static void Display2(List<T> aList, int start, int stop)
15+
{
16+
for (int i = start; i < stop; ++i)
17+
{
18+
if (i == start || i == (start - 1 + stop) / 2)
19+
{
20+
Console.BackgroundColor = ConsoleColor.Yellow;
21+
Console.ForegroundColor = ConsoleColor.Black;
22+
Console.Write("{0,4}", aList[i]);
23+
Console.ForegroundColor = ConsoleColor.White;
24+
Console.BackgroundColor = ConsoleColor.Black;
25+
}
26+
else if (i == (stop - 1))
27+
{
28+
Console.BackgroundColor = ConsoleColor.Yellow;
29+
Console.ForegroundColor = ConsoleColor.Black;
30+
Console.Write("{0,4}<==", aList[i]);
31+
Console.ForegroundColor = ConsoleColor.White;
32+
Console.BackgroundColor = ConsoleColor.Black;
33+
}
34+
else
35+
Console.Write("{0,4}", aList[i]);
36+
}
37+
38+
Console.WriteLine();
39+
}
40+
public static void Display3(List<T> aList, int start, int stop, int pivotPos)
41+
{
42+
for (int i = start; i < stop; ++i)
43+
{
44+
if (i < pivotPos)
45+
{
46+
Console.BackgroundColor = ConsoleColor.Cyan;
47+
Console.ForegroundColor = ConsoleColor.Black;
48+
Console.Write("{0,4}", aList[i]);
49+
Console.ForegroundColor = ConsoleColor.White;
50+
Console.BackgroundColor = ConsoleColor.Black;
51+
}
52+
else if (i == pivotPos)
53+
{
54+
Console.BackgroundColor = ConsoleColor.Yellow;
55+
Console.ForegroundColor = ConsoleColor.Black;
56+
Console.Write("{0,4}", aList[i]);
57+
Console.ForegroundColor = ConsoleColor.White;
58+
Console.BackgroundColor = ConsoleColor.Black;
59+
}
60+
else if (i > pivotPos)
61+
{
62+
Console.BackgroundColor = ConsoleColor.Green;
63+
Console.ForegroundColor = ConsoleColor.Black;
64+
Console.Write("{0,4}", aList[i]);
65+
Console.ForegroundColor = ConsoleColor.White;
66+
Console.BackgroundColor = ConsoleColor.Black;
67+
}
68+
}
69+
Console.WriteLine();
70+
}
71+
72+
/*
73+
* Black 0, DarkBlue 1, DarkGreen 2, DarkCyan 3, DarkRed 4, DarkMagenta 5, DarkYellow 6, Gray 7, DarkGray 8,
74+
* Blue 9, Green 10, Cyan 11, Red 12, Magenta 13, Yellow 14, White 15
75+
*/
76+
public static void DisplayEvery(List<T> aList, int start, int stop, int ith)
77+
{
78+
for (int i = start; i < stop; ++i)
79+
{
80+
Console.ForegroundColor = ConsoleColor.Black;
81+
int colorValue = 10 + (i % ith);
82+
if (colorValue == 9 || colorValue == 4 || colorValue == 5)
83+
Console.ForegroundColor = ConsoleColor.White;
84+
85+
Console.BackgroundColor = (ConsoleColor)colorValue;
86+
Console.Write("{0,4}", aList[i]);
87+
88+
Console.ForegroundColor = ConsoleColor.White;
89+
Console.BackgroundColor = ConsoleColor.Black;
90+
91+
}
92+
Console.WriteLine();
93+
}
94+
public static void Display(List<T> aList, int start = 0, int end = -1)
95+
{
96+
if (end == -1)
97+
end = aList.Count;
98+
99+
for (int i = start; i < end; i++)
100+
Console.Write("{0,4}", aList[i]);
101+
Console.WriteLine();
102+
}
103+
public static void DisplayOne(List<T> aList, int pos, int start = 0, int end = -1)
104+
{
105+
if (end == -1)
106+
end = aList.Count;
107+
108+
for (int i = start; i < end; i++)
109+
{
110+
if (pos != i)
111+
Console.Write("{0,4}", aList[i]);
112+
else
113+
{
114+
Console.BackgroundColor = ConsoleColor.Green;
115+
Console.ForegroundColor = ConsoleColor.Black;
116+
Console.Write("{0,4}", aList[i]);
117+
Console.ForegroundColor = ConsoleColor.White;
118+
Console.BackgroundColor = ConsoleColor.Black;
119+
}
120+
}
121+
Console.WriteLine();
122+
}
123+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
class Program
5+
{
6+
static void Main(string[] args)
7+
{
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>.Display(test);
15+
Sorting<int>.InsertionSort(test);
16+
}
17+
}

0 commit comments

Comments
 (0)