zoukankan      html  css  js  c++  java
  • Quick Sort

        快速排序是对冒泡排序的一种改进。它基于分治策略,对于要排序的一个序列,从中选一值进行排序,将其放入到正确的位置position。然后以position为界,对左右两部分再分别做排序。直到划分的长度为1。具体实现如下:

    代码
            /// <summary>
            
    /// 快速排序
            
    /// </summary>
            
    /// <param name="data"></param>
            public static void QuickSort(int[] data)
            {
                
    if (data == null || data.Length < 1)
                {
                    
    throw new ArgumentNullException("data");
                }

                Quick(data, 
    0, data.Length);
            }

            
    private static void Quick(int[] data, int low, int high)
            {
                
    if (data == null || data.Length < 1)
                {
                    
    throw new ArgumentNullException("data");
                }
                
    if (low < 0 || low >= data.Length || high < 0 || high > data.Length)
                {
                    
    return;
                }

                
    if (low < high)
                {
                    
    int i = low, j = high, temp;
                    
    while (true)
                    {
                        
    do
                        {
                            i
    ++;
                        } 
    while (i < high && data[i] < data[low]);
                        
    do
                        {
                            j
    --;
                        } 
    while (j > low && data[j] > data[low]);

                        
    if (i < j)
                        {
                            temp 
    = data[i];
                            data[i] 
    = data[j];
                            data[j] 
    = temp;
                        }
                        
    else
                        {
                            temp 
    = data[j];
                            data[j] 
    = data[low];
                            data[low] 
    = temp;

                            
    break;
                        }
                    }

                    Quick(data, low, j
    );
                    Quick(data, j 
    + 1, high);
                }
            }
  • 相关阅读:
    本地Springboot项目打包成docker镜像并上传到云仓库的全流程
    vue 组件开发到发布到npm全流程
    Python创建文件
    Ngrinder脚本开发各细节锦集(groovy)
    Ngrinder多接口的混合场景压测比例设定方案
    Pycahrm出现推送失败的处理方案,出现Push failed: Failed with error: Authentication failed for 'https://gitee.com/fxcity/Temporary_Test.git/'
    接口测试提取csrf_token和session
    Postman提取html返回值
    HttpRunnerManager自动化测试安装部署(CentOS)
    【转载】centos下搭建RabbitMQ
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1691646.html
Copyright © 2011-2022 走看看