From f5c7b20a47d3c19f2840fb2043090277d677c99c Mon Sep 17 00:00:00 2001 From: PeixiZ Date: Wed, 26 Jan 2022 13:14:05 +0800 Subject: [PATCH] add c# bubble and improve the algorithm --- basic/sorting/BubbleSort/BubbleSort.cs | 45 +++++++++++++++++++++++ basic/sorting/BubbleSort/README.md | 51 ++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 basic/sorting/BubbleSort/BubbleSort.cs diff --git a/basic/sorting/BubbleSort/BubbleSort.cs b/basic/sorting/BubbleSort/BubbleSort.cs new file mode 100644 index 0000000000000..62f7a487b0b7e --- /dev/null +++ b/basic/sorting/BubbleSort/BubbleSort.cs @@ -0,0 +1,45 @@ +using static System.Console; +namespace Pro; +public class Program +{ + public static void Main() + { + int[] test = new int[] {56,876,34,23,45,501,2,3,4,6,5,7,8,9,11,10,12,23,34}; + BubbleSortNums(test); + foreach (var item in test) + { + WriteLine(item); + } + ReadLine(); + } + public static void BubbleSortNums(int[] nums) + { + int numchange = 0; + for (int initial = 0; initial < nums.Length - numchange; initial++) + { + WriteLine($"{initial} start "); + //记录此值 用于迭代开始位置 + bool changelog = false; + for (int second_sortnum = initial; second_sortnum < nums.Length - 1; second_sortnum++) + { + if (nums[second_sortnum] > nums[second_sortnum + 1]) + { + swap(ref nums[second_sortnum], ref nums[second_sortnum+ 1]); + if (!changelog) + { + //记录转换的位置,让initial开始位置从转换位置前开始 + initial = ((second_sortnum - 2 )> 0) ? (second_sortnum - 2) : -1; + numchange += 1; + } + changelog = true; + } + } + } + } + private static void swap(ref int compare_left, ref int compare_right) + { + int temp = compare_left; + compare_left = compare_right; + compare_right = temp; + } +} diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index bcc24e574e3fb..d077db7021514 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -188,6 +188,57 @@ fn main() { } ``` +### **C#** +```cs +using static System.Console; +namespace Pro; +public class Program +{ + public static void Main() + { + int[] test = new int[] {56,876,34,23,45,501,2,3,4,6,5,7,8,9,11,10,12,23,34}; + BubbleSortNums(test); + foreach (var item in test) + { + WriteLine(item); + } + ReadLine(); + } + public static void BubbleSortNums(int[] nums) + { + int numchange = 0; + for (int initial = 0; initial < nums.Length - numchange; initial++) + { + WriteLine($"{initial} start "); + //记录此值 用于迭代开始位置 + bool changelog = false; + for (int second_sortnum = initial; second_sortnum < nums.Length - 1; second_sortnum++) + { + if (nums[second_sortnum] > nums[second_sortnum + 1]) + { + swap(ref nums[second_sortnum], ref nums[second_sortnum+ 1]); + if (!changelog) + { + //记录转换的位置,让initial开始位置从转换位置前开始 + initial = ((second_sortnum - 2 )> 0) ? (second_sortnum - 2) : -1; + numchange += 1; + } + changelog = true; + } + } + } + } + private static void swap(ref int compare_left, ref int compare_right) + { + int temp = compare_left; + compare_left = compare_right; + compare_right = temp; + } +} + + +``` + ## 算法分析