首页 > 其他分享 >CSharp: Sorting Algorithms

CSharp: Sorting Algorithms

时间:2023-09-28 15:14:40浏览次数:31  
标签:Sorting int void arrry SortingAlgorithm Algorithms CSharp geovindu public

 

/*****************************************************************//**
 * \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

相关文章

  • cpp: Sorting Algorithms
     /*****************************************************************//***\fileSortingAlgorithms.h*\brief排序*\IDEvs2022C++20*\authorgeovindu*\dateSeptember282023********************************************************......
  • python: Sorting Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:PythonSortingAlgorithms#描述:*https://www.programiz.com/dsa/counting-sort#*https://www.geeksforgeeks.org/sorting-algorithms/#Author:geovindu,GeovinDu涂聚文.#IDE:PyC......
  • python: Essential Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:#描述:#Author:geovindu,GeovinDu涂聚文.#IDE:PyCharm2023.1python311#Datetime:2023/9/2121:28#User:geovindu#Product:PyCharm#Project:EssentialAlgor......
  • c: Sorting Algorithms
    SortAlgorithm.h /*****************************************************************//***\fileSortAlgorithm.h*\brief业务操作方法*VSCODEc11https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md*https://www.progra......
  • natsort.natsorted()-用于自然排序(natural sorting)字符串列表。
    参考:https://natsort.readthedocs.io/en/stable/api.html#natsort.natsorted语法格式natsort.natsorted(seq:Iterable[T],key:Optional[Callable[[T],Union[natsort.utils.SupportsDunderLT,natsort.utils.SupportsDunderGT,None]]]=None,reverse:bool=False,alg:......
  • Proj. CRR Paper Reading: Optimal Speedup of Las Vegas Algorithms, Adaptive resta
    TitleAdaptiverestartforstochasticsynthesisPLDI2021TaskDistributethepowerbetweenmultiplerunsinstochasticprogramsynthesistoaccelerateHere,astochasticprogramsynthesisprogramcanbesummarizedasfollows:Givenasetof<input,ou......
  • * Dytechlab Cup 2022 A. Ela Sorting Books
    \(n\)本书必须分成\(k\)部分在书架(\(k\midn\)),每本书用一个小写的拉丁字母\([a,y]\)表示。每部分必须有严格\(\frac{n}{k}\)本书。当所有书分配完成后,对于每个部分编号为\(1,2,\cdots,k\),每部分的有\(\frac{n}{k}\)本书,他们的\(MEX\)表示这个部分,作为代表字符......
  • D. Sorting By Multiplication
    D.SortingByMultiplicationYouaregivenanarray$a$oflength$n$,consistingofpositiveintegers.Youcanperformthefollowingoperationonthisarrayanynumberoftimes(possiblyzero):choosethreeintegers$l$,$r$and$x$suchthat$1\lel......
  • CF258D Little Elephant and Broken Sorting 题解
    题意给定一个长度为\(n\)的排列\(a\)和\(m\)个形如\(\left(x,y\right)\)的操作,每次操作有\(50\%\)的概率交换\(a_x,a_y\),求最终排列的期望逆序对数。(\(1\len,m\le5000\))。题解首先转化答案\[\text{Ans}=\sum\limits_{i=1}^{n}\sum\limits_{j=i+1}^{......
  • CSharp_exe执行文件点击运行无反应;
    问题:点击试图运行exe可执行文件,但无法运行!解决思路:首先,想到的就是C#项目出错;再者就是运行环境缺少支持,查看Microsoft.NETFramework2.0以及Microsoft.NETFramework3.5是否安装,没安装的应该就不能运行的,所以装上即可!......