Reformat the C# codes.

Disable creating new line before open brace.
This commit is contained in:
krahets
2023-04-23 03:03:12 +08:00
parent ac6eece4f3
commit 73dcb4cea9
49 changed files with 561 additions and 1135 deletions

View File

@ -9,74 +9,62 @@ using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity;
public class space_complexity
{
public class space_complexity {
/* 函数 */
static int function()
{
static int function() {
// do something
return 0;
}
/* 常数阶 */
static void constant(int n)
{
static void constant(int n) {
// 常量、变量、对象占用 O(1) 空间
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
int c = 0;
}
// 循环中的函数占用 O(1) 空间
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
function();
}
}
/* 线性阶 */
static void linear(int n)
{
static void linear(int n) {
// 长度为 n 的数组占用 O(n) 空间
int[] nums = new int[n];
// 长度为 n 的列表占用 O(n) 空间
List<ListNode> nodes = new();
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
nodes.Add(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, string> map = new();
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
map.Add(i, i.ToString());
}
}
/* 线性阶(递归实现) */
static void linearRecur(int n)
{
static void linearRecur(int n) {
Console.WriteLine("递归 n = " + n);
if (n == 1) return;
linearRecur(n - 1);
}
/* 平方阶 */
static void quadratic(int n)
{
static void quadratic(int n) {
// 矩阵占用 O(n^2) 空间
int[,] numMatrix = new int[n, n];
// 二维列表占用 O(n^2) 空间
List<List<int>> numList = new();
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
List<int> tmp = new();
for (int j = 0; j < n; j++)
{
for (int j = 0; j < n; j++) {
tmp.Add(0);
}
numList.Add(tmp);
@ -84,8 +72,7 @@ public class space_complexity
}
/* 平方阶(递归实现) */
static int quadraticRecur(int n)
{
static int quadraticRecur(int n) {
if (n <= 0) return 0;
int[] nums = new int[n];
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
@ -93,8 +80,7 @@ public class space_complexity
}
/* 指数阶(建立满二叉树) */
static TreeNode? buildTree(int n)
{
static TreeNode? buildTree(int n) {
if (n == 0) return null;
TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1);
@ -103,8 +89,7 @@ public class space_complexity
}
[Test]
public void Test()
{
public void Test() {
int n = 5;
// 常数阶
constant(n);

View File

@ -8,52 +8,41 @@ using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity;
public class time_complexity
{
void algorithm(int n)
{
public class time_complexity {
void algorithm(int n) {
int a = 1; // +0技巧 1
a = a + n; // +0技巧 1
// +n技巧 2
for (int i = 0; i < 5 * n + 1; i++)
{
for (int i = 0; i < 5 * n + 1; i++) {
Console.WriteLine(0);
}
// +n*n技巧 3
for (int i = 0; i < 2 * n; i++)
{
for (int j = 0; j < n + 1; j++)
{
for (int i = 0; i < 2 * n; i++) {
for (int j = 0; j < n + 1; j++) {
Console.WriteLine(0);
}
}
}
// 算法 A 时间复杂度:常数阶
void algorithm_A(int n)
{
void algorithm_A(int n) {
Console.WriteLine(0);
}
// 算法 B 时间复杂度:线性阶
void algorithm_B(int n)
{
for (int i = 0; i < n; i++)
{
void algorithm_B(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(0);
}
}
// 算法 C 时间复杂度:常数阶
void algorithm_C(int n)
{
for (int i = 0; i < 1000000; i++)
{
void algorithm_C(int n) {
for (int i = 0; i < 1000000; i++) {
Console.WriteLine(0);
}
}
/* 常数阶 */
static int constant(int n)
{
static int constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
@ -62,8 +51,7 @@ public class time_complexity
}
/* 线性阶 */
static int linear(int n)
{
static int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
@ -71,26 +59,21 @@ public class time_complexity
}
/* 线性阶(遍历数组) */
static int arrayTraversal(int[] nums)
{
static int arrayTraversal(int[] nums) {
int count = 0;
// 循环次数与数组长度成正比
foreach (int num in nums)
{
foreach (int num in nums) {
count++;
}
return count;
}
/* 平方阶 */
static int quadratic(int n)
{
static int quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
@ -98,17 +81,13 @@ public class time_complexity
}
/* 平方阶(冒泡排序) */
static int bubbleSort(int[] nums)
{
static int bubbleSort(int[] nums) {
int count = 0; // 计数器
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.Length - 1; i > 0; i--)
{
for (int i = nums.Length - 1; i > 0; i--) {
// 内循环:冒泡操作
for (int j = 0; j < i; j++)
{
if (nums[j] > nums[j + 1])
{
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
@ -121,14 +100,11 @@ public class time_complexity
}
/* 指数阶(循环实现) */
static int exponential(int n)
{
static int exponential(int n) {
int count = 0, bas = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < bas; j++)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;
}
bas *= 2;
@ -138,18 +114,15 @@ public class time_complexity
}
/* 指数阶(递归实现) */
static int expRecur(int n)
{
static int expRecur(int n) {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* 对数阶(循环实现) */
static int logarithmic(float n)
{
static int logarithmic(float n) {
int count = 0;
while (n > 1)
{
while (n > 1) {
n = n / 2;
count++;
}
@ -157,41 +130,35 @@ public class time_complexity
}
/* 对数阶(递归实现) */
static int logRecur(float n)
{
static int logRecur(float n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
/* 线性对数阶 */
static int linearLogRecur(float n)
{
static int linearLogRecur(float n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2);
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
/* 阶乘阶(递归实现) */
static int factorialRecur(int n)
{
static int factorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// 从 1 个分裂出 n 个
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
[Test]
public void Test()
{
public void Test() {
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
int n = 8;
Console.WriteLine("输入数据大小 n = " + n);

View File

@ -8,21 +8,17 @@ using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity
{
public class worst_best_time_complexity {
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
static int[] randomNumbers(int n)
{
static int[] randomNumbers(int n) {
int[] nums = new int[n];
// 生成数组 nums = { 1, 2, 3, ..., n }
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// 随机打乱数组元素
for (int i = 0; i < nums.Length; i++)
{
for (int i = 0; i < nums.Length; i++) {
var index = new Random().Next(i, nums.Length);
var tmp = nums[i];
var ran = nums[index];
@ -33,10 +29,8 @@ public class worst_best_time_complexity
}
/* 查找数组 nums 中数字 1 所在索引 */
static int findOne(int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
static int findOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1)
@ -48,10 +42,8 @@ public class worst_best_time_complexity
/* Driver Code */
[Test]
public void Test()
{
for (int i = 0; i < 10; i++)
{
public void Test() {
for (int i = 0; i < 10; i++) {
int n = 100;
int[] nums = randomNumbers(n);
int index = findOne(nums);