Bootstrap

默写简单的排序算法


快速排序:
public void sort(int arr[],int low,int high)
 {
 int l=low;
 int h=high;
 int povit=arr[low];
 
 while(l<h)
 {
 while(l<h&&arr[h]>=povit)
 h--;
 if(l<h){
 int temp=arr[h];
 arr[h]=arr[l];
 arr[l]=temp;
 l++;
 }
 
 while(l<h&&arr[l]<=povit)
 l++;
 
 if(l<h){
 int temp=arr[h];
 arr[h]=arr[l];
 arr[l]=temp;
 h--;
 }
 }

 if(l>low)sort(arr,low,l-1);
 if(h<high)sort(arr,l+1,high);
 }

冒泡排序:

 public void sort(int[] a)
    {
        int temp = 0;
        for (int i = a.length - 1; i > 0; --i)
        {
            for (int j = 0; j < i; ++j)
            {
                if (a[j + 1] < a[j])
                {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }


;