方法1:选择、插入、冒泡排序
时间复杂度:O(n^2)
方法2:堆排序、快速排序
时间复杂度:O(n*logn)
方法3:快速排序+剪枝
时间复杂度:O(n)
public int QuickSort(int[] arr, int left, int right, int n)
{
if(left >= right)
return;
int low = left;
int high = right;
int value = arr[low];
while(low < high)
{
while(low<high && arr[high]>=value)
high--;
arr[low] = arr[high];
while(low<high && arr[low]<=value)
low++;
arr[high] = arr[low];
}
arr[low] = value;
if(low == n)
return arr[low];
if(low < n)
return QuickSort(arr, low+1, right, n);
else
return QuickSort(arr, left, low-1, n);
}
网友评论