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 + "\t");
            }

            InsertionSort(arr);
            Console.WriteLine("\n\n\nAfter insertion sort:");
            foreach(var a in arr)
            {
                Console.Write(a + "\t");
            }
        }

        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 + "\t");
            }

            SelectionSort(arr);
            Console.WriteLine("\n\n\nAfter selection sort:");
            foreach(var a in arr)
            {
                Console.Write(a + "\t");
            }
        }

        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 + "\t");
            }

            int[] selectionArr = SelectionSort(arr);
            Console.WriteLine("\n\nSelection sort:");
            foreach(var a in selectionArr)
            {
                Console.Write(a + "\t");
            }
        }

        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 + "\t");
            }

            int[] ascArr = BubbleSort(arr, true);
            Console.WriteLine("\n\n\nAsc order:");
            foreach(var a in ascArr)
            {
                Console.Write(a + "\t");
            }

            int[] descArr = BubbleSort(arr, false);
            Console.WriteLine("\n\n\nDesc order:");
            foreach(var a in descArr)
            {
                Console.Write(a + "\t");
            }
        }

        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;
        }

 

posted @ 2020-02-12 16:31  FredGrit  阅读(236)  评论(0编辑  收藏  举报