zoukankan      html  css  js  c++  java
  • 常见的排序算法

    以前时候,写排序按我的想法,能排出来个结果.前后二个数据比较,交换.

    using System;
    using System.Collections;
    using System.Text;

    namespace Sort
    {
        
    class Program
        {
            
    //初始化
            public static  int[] GetRandomArr(int num)
            {
                
    if (num < 1)
                    
    return null;
                Random ran 
    = new Random();
                
    int[] arr = new int[num];
                
    for (int i = 0; i < num; i++)
                {
                    arr[i] 
    = ran.Next(1100);
                }
                
    return arr;
            }
            
    //冒泡
            public static  int[] BubbleSort(int[] array)
            {
                
    int[] arr = array;
                
    for (int i = 0; i < arr.Length; i++)
                {
                    
    int temp = 0;
                    
    for (int j = i + 1; j < arr.Length; j++)
                    {
                        
    if (arr[i] > arr[j])
                        {
                            temp 
    = arr[i];
                            arr[i] 
    = arr[j];
                            arr[j] 
    = temp;
                        }
                    }
                }
                
    return arr;
            }
            
    //输出
            public static void OutPutArray(int[] arr)
            {
                
    for (int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i].ToString() 
    + "  ");
                }
                Console.WriteLine();
            }

            
    static void Main(string[] args)
            {
                
    int[] arr = GetRandomArr(10);

                Console.WriteLine(
    "排序前:");
                OutPutArray(arr);
                
                Console.WriteLine(
    "排序后:");
                OutPutArray(BubbleSort(arr));

                Console.ReadKey();
            }
        }
    }

    现在在看以前写的,好像怪怪的,因些找了一些常见的排序算法,弥补自己理解的偏差。

    大学里面讲排序时,应该是这个:

            //原始排序算法
            public static void Sort(int[] array)
            {
                
    for (int i = 0; i < array.Length; i++)
                {
                    
    //经过第i次排序后数组的最后i位已经是有序的,所以需要排除
                    for (int j = 0; j < array.Length - i - 1; j++)
                    {
                        
    if (array[j] > array[j + 1])
                        {
                            
    int tmp = array[j];
                            array[j] 
    = array[j + 1];
                            array[j 
    + 1= tmp;
                        }
                    }
                }
            }

    改进后的是:

         public static void Sort(int[] array)
            {
                
    for (int i = 0; i < array.Length; i++)
                {
                    
    bool isChanged = false;
                    
    for (int j = 0; j < array.Length - i - 1; j++)
                    {
                        
    if (array[j] > array[j + 1])
                        {
                            
    int tmp = array[j];
                            array[j] 
    = array[j + 1];
                            array[j 
    + 1= tmp;
                            isChanged 
    = true;
                        }
                    }
                    
    //判断本轮测试是否有交换,没有交换说明数组已经是有序的了
                    if (!isChanged)
                        
    return;
                }
            }

    此外还有快速排序,堆排序,直接插入排序,有时间补上。

  • 相关阅读:
    笔记0510
    笔记0514
    笔记0521
    GridView专题
    笔记0418
    笔记0516
    笔记0515
    笔记0507
    Python 安装与环境变量配置
    ffmpeg 下载安装和简单应用
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1706003.html
Copyright © 2011-2022 走看看