• 快速排序(QuickSort)用C# 实现的小例子


        class QuickSort
        
    {
            
    public void Sort(int[] data, int start, int end)
            
    {
                
    if (start >= end) return;
                
    if (start + 1 == end)
                
    {
                    
    if (data[start] > data[end])
                        Swap(data, start, end);

                    
    return;
                }


                
    int indexL = start + 1, indexR = end;
                
    while (indexL < indexR)
                
    {
                    
    // Get from left
                    while (indexL <= end && data[start] >= data[indexL])
                        indexL
    ++;

                    
    // Get from right
                    while (indexR > start && data[start] < data[indexR])
                        indexR
    --;

                    
    if (indexL < indexR)
                    
    {
                        Swap(data, indexR, indexL);
                    }

                }


                
    if(indexL-1 !=start)
                    Swap(data, start, indexL 
    - 1);


                Sort(data, start, indexL 
    - 2);
                Sort(data, indexL, end);
            }


            
    private void Swap(int[] data, int x, int y)
            
    {
                data[x] 
    = data[x] + data[y];
                data[y] 
    = data[x] - data[y];
                data[x] 
    = data[x] - data[y];
            }

        }

  • 相关阅读:
    ASP.NET中级学习3
    C#面向对象学习笔记
    Javascript学习笔记
    FormView控件使用
    ASP.NET初级学习
    ListView控件是使用
    Java NIO 学习笔记一
    堆栈和托管堆 c#
    安装php7.2并且整合nginx且分开部署
    Python 安装requests和MySQLdb
  • 原文地址:https://www.cnblogs.com/skywind/p/1131612.html
Copyright © 2020-2023  润新知