/*****************************************************************//** * \file SortingAlgorithm.cs * \brief csharp Sorting Algorithms 算法 * IDE vs 2022 C# .net 6.0 * \author geovindu * \date September 28 2023 *********************************************************************/ namespace SortingAlgorithms { /// <summary> /// /// </summary> public class SortingAlgorithm { /// <summary> /// 1.Bubble Sort冒泡排序法 /// </summary> /// <param name="arr"></param> public static void BubbleSort(int[] arrry) { int n = arrry.Length; int i, j, temp; bool swapped; for (i = 0; i < n - 1; i++) { swapped = false; for (j = 0; j < n - i - 1; j++) { if (arrry[j] > arrry[j + 1]) { // Swap arr[j] and arr[j+1] temp = arrry[j]; arrry[j] = arrry[j + 1]; arrry[j + 1] = temp; swapped = true; } } // If no two elements were // swapped by inner loop, then break if (swapped == false) break; } } /// <summary> /// /// </summary> /// <param name="arrry"></param> public static void printArray(int[] arrry) { int i; int size = arrry.Length; for (i = 0; i < size; i++) Console.Write(arrry[i] + " "); Console.WriteLine(); } /// <summary> /// 2.Selection Sort 选择排序 /// /// </summary> /// <param name="arr"></param> public static void SelectionSort(int[] arrry) { int n = arrry.Length; // One by one move boundary of unsorted subarray for (int i = 0; i < n - 1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i + 1; j < n; j++) if (arrry[j] < arrry[min_idx]) min_idx = j; // Swap the found minimum element with the first // element int temp = arrry[min_idx]; arrry[min_idx] = arrry[i]; arrry[i] = temp; } } /// <summary> /// 3. 插入排序 Insertion Sort /// </summary> /// <param name="arrry"></param> public static void InsertionSort(int[] arrry) { int n = arrry.Length; for (int i = 1; i < n; ++i) { int key = arrry[i]; int j = i - 1; // Move elements of arr[0..i-1], // that are greater than key, // to one position ahead of // their current position while (j >= 0 && arrry[j] > key) { arrry[j + 1] = arrry[j]; j = j - 1; } arrry[j + 1] = key; } } } }
/*****************************************************************//** * \file SortExample.cs * \brief csharp Sorting Algorithms 算法 * IDE vs 2022 C# .net 6.0 * \author geovindu * \date September 28 2023 *********************************************************************/ using System; using SortingAlgorithms; namespace BLL { /// <summary> /// /// </summary> public class SortExample { /// <summary> /// 1.Bubble Sort冒泡排序法 /// </summary> public static void Bubble() { int[] geovindu = { 64, 34, 25, 12, 22, 11, 90 }; int n = geovindu.Length; SortingAlgorithm.BubbleSort(geovindu); Console.WriteLine("1.Bubble Sorted array:"); SortingAlgorithms.SortingAlgorithm.printArray(geovindu); } /// <summary> /// 2.Selection Sort 选择排序 /// </summary> public static void Selection() { int[] geovindu = { 64, 34, 25, 12, 22, 11, 90 }; int n = geovindu.Length; SortingAlgorithm.SelectionSort(geovindu); Console.WriteLine("2.Selection Sorted array:"); SortingAlgorithms.SortingAlgorithm.printArray(geovindu); } /// <summary> /// 3. 插入排序 Insertion Sort /// </summary> public static void Insertion() { int[] geovindu = { 64, 34, 25, 12, 22, 11, 90 }; int n = geovindu.Length; SortingAlgorithm.InsertionSort(geovindu); Console.WriteLine("3.Insertion Sorted array:"); SortingAlgorithms.SortingAlgorithm.printArray(geovindu); } } }
调用:
/*****************************************************************//** * \file Program.cs * \brief csharp Sorting Algorithms 算法 * IDE vs 2022 C# .net 6.0 * \author geovindu * \date September 28 2023 *********************************************************************/ // See https://aka.ms/new-console-template for more information using BLL; Console.WriteLine("Hello, World! 涂聚文 Geovin Du,geovindu, 学习CSharp"); //1. SortExample.Bubble(); //2. SortExample.Selection(); //3. SortExample.Insertion();
标签:Sorting,int,void,arrry,SortingAlgorithm,Algorithms,CSharp,geovindu,public From: https://www.cnblogs.com/geovindu/p/17735816.html