zoukankan      html  css  js  c++  java
  • C# bubble sort,selection sort,insertion sort

    static void Main(string[] args)
            {
                InsertionSortDemo();
                Console.ReadLine();
            }
    
            static void InsertionSortDemo()
            {
                Random rnd = new Random();
                int[] arr = new int[100];
                for (int i = 0; i < 100; i++)
                {
                    arr[i] = rnd.Next(0, 1000);
                }
    
                Console.WriteLine("Raw data:");
                foreach (var a in arr)
                {
                    Console.Write(a + "	");
                }
    
                InsertionSort(arr);
                Console.WriteLine("
    
    
    After insertion sort:");
                foreach(var a in arr)
                {
                    Console.Write(a + "	");
                }
            }
    
            static void InsertionSort(int[] arr)
            {
                int inner, temp;
                for (int outer = 0; outer < arr.Length; outer++)
                {
                    temp = arr[outer];
                    inner = outer;
                    while (inner > 0 && arr[inner - 1] >= temp)
                    {
                        arr[inner] = arr[inner - 1];
                        inner--;
                    }
                    arr[inner] = temp;
                }
            }
    
            static void SelectSortDemo()
            {
                Random rnd = new Random();
                int[] arr = new int[100];
                for (int i = 0; i < 100; i++)
                {
                    arr[i] = rnd.Next(0, 10000);
                }
    
                Console.WriteLine("Raw data:");
                foreach (var a in arr)
                {
                    Console.Write(a + "	");
                }
    
                SelectionSort(arr);
                Console.WriteLine("
    
    
    After selection sort:");
                foreach(var a in arr)
                {
                    Console.Write(a + "	");
                }
            }
    
            public void SelectSort(int[] arr)
            {
                int min;
                for(int outer=0;outer<=arr.Length;outer++)
                {
                    min = outer;
                    for(int inner=outer+1;inner<=arr.Length;inner++)
                    {
                        if(arr[inner]<arr[min])
                        {
                            min = inner;
                        }
                    }
                    if(min!=outer)
                    {
                        int temp = arr[min];
                        arr[min] = arr[outer];
                        arr[outer] = temp;
                    }
                }
            }
    
            static void SelectionSortDemo()
            {
                Random rnd = new Random();
                int[] arr = new int[100];
                for (int i = 0; i < 100; i++)
                {
                    arr[i] = rnd.Next(0, 1000);
                }
    
                Console.WriteLine("Raw data:");
                foreach (var a in arr)
                {
                    Console.Write(a + "	");
                }
    
                int[] selectionArr = SelectionSort(arr);
                Console.WriteLine("
    
    Selection sort:");
                foreach(var a in selectionArr)
                {
                    Console.Write(a + "	");
                }
            }
    
            static int[] SelectionSort(int[] arr)
            {             
                int min = 0;
                for(int i=0;i<arr.Length-1;i++)
                {
                    min = i;
                    for(int j=i+1;j<arr.Length;j++)
                    {
                        if(arr[j]<arr[min])
                        {
                            min = j;
                        }
                    }
    
                    if (i != min)
                    {
                        int temp = arr[i];
                        arr[i] = arr[min];
                        arr[min] = temp;
                    }
                }
                return arr;
            }
    
            static void BubbleDemo()
            {
                Random rnd = new Random();
                int[] arr = new int[100];
                for(int i=0;i<100;i++)
                {
                    arr[i] = rnd.Next(0, 1000);
                }
    
                Console.WriteLine("Raw data:");
                foreach(var a in arr)
                {
                    Console.Write(a + "	");
                }
    
                int[] ascArr = BubbleSort(arr, true);
                Console.WriteLine("
    
    
    Asc order:");
                foreach(var a in ascArr)
                {
                    Console.Write(a + "	");
                }
    
                int[] descArr = BubbleSort(arr, false);
                Console.WriteLine("
    
    
    Desc order:");
                foreach(var a in descArr)
                {
                    Console.Write(a + "	");
                }
            }
    
            static int[] BubbleSort(int[] arr,bool isAsc)
            {
                if(arr==null && arr.Length==0)
                {
                    return null;
                }
                 
                for(int i=0;i<arr.Length;i++)
                {
                    for(int j=i+1;j<arr.Length;j++)
                    {
                        //Ascending
                        if(isAsc)
                        {
                            if(arr[i]>arr[j])
                            {
                                int temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;
                            }
                        }
                        //Descending
                        else
                        {
                            if(arr[i]<arr[j])
                            {
                                int temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;
                            }
                        }
                    }
                }
                return arr;
            }
  • 相关阅读:
    基于微服务架构的RBAC权限管理系统
    用C#实现基于(OpenId Connect)的单点登录与RBAC权限验证(SSO,OIDC,RBAC)
    量化分析基础
    Ocelot 发现服务总是失败的解决办法
    windows 下安装 theano 及配置 gpu
    python scrapy 爬虫 初学
    layer弹出层框架alert与msg详解
    Workerman-文件监控-牛刀小试
    ECharts 初体验
    实验楼 linux 学习
  • 原文地址:https://www.cnblogs.com/Fred1987/p/12299546.html
Copyright © 2011-2022 走看看