快速排序 qsort

快速排序(quick sorting 又称 划分排序),是对气泡排序的改进。原理: 从待排序区间(a[0]到a[n-1])选取第一个为基准元素。通过从两端向中间顺序进行比较和交换,使前面单元中只包含比 基准元素小的数。....
        static void QSort(int[] arr, int s, int e) {
           
            
int i = s, j = e + 1;
            
int t = arr[s];
            
do {
                
do i++while(arr[i]<t && i<e );
                
do j--while(arr[j]>t && j>s);
          if (i < j) {
                    Swap(ref arr[i],ref arr[j]);
                }
            } 
while (i < j);

            Swap(
ref arr[s], ref arr[j]);    //一次比较完成 (基准 与 j 位置的元素交换)

            
if(s<j-1) QSort(arr, s, j - 1);
            
if(j+1<e) QSort(arr, j+1,e );
        }

        
static void Swap(ref int x,ref int y)
        {
            
int t = x;
            x 
= y;
            y 
= t;
        }

  昨天去面试一家垃圾公司,出了道题,实现快速排序,升降序。居然忘了怎么做。fuck.

原文地址:https://www.cnblogs.com/wucg/p/1774829.html