快速排序算法递归和非递归实现 [# 29]
2010-07-18 04:56
274 查看
Quick sort algorithm is quite like the merge sort, cause both of them use the idea of divide-and-conquer, but be aware, quick sort doesn't conquer it subproblems, cause when the problem is divided into small enough (only 1 element in the sub array), the
whole array have already been sorted.
The worst case running time of quick sort is O(n^2), this can happen when the array is already sorted, so the total running time is T(n) = T(n-1) + /theta(n), and we can have T(n) = O(n^2). To avoid such case, we can randomly pick one as the pivot and
swap it with the last element in the array, and the running time will be O(n lgn) because T(n) = 2 T(n/2) + /theta(n) .
The non-recursive quicksort stores the pair of left index and right index in the stack, and each time the array is divided, the new pair of left and right indices are stored. If the left index is larger than or equal to the right index, we will not store that
pair of indices because the number of element in that segment should be only one.
转载请注明出处:http://blog.csdn.net/beiyeqingteng
whole array have already been sorted.
The worst case running time of quick sort is O(n^2), this can happen when the array is already sorted, so the total running time is T(n) = T(n-1) + /theta(n), and we can have T(n) = O(n^2). To avoid such case, we can randomly pick one as the pivot and
swap it with the last element in the array, and the running time will be O(n lgn) because T(n) = 2 T(n/2) + /theta(n) .
public class QuickSortRecursive { public static void main(String[] args) { QuickSortRecursive qsr = new QuickSortRecursive(); int[] array = {0, -1}; qsr.quickSort(array, 0, array.length - 1); for (int i : array) { System.out.print(i + " "); } } public void quickSort(int[] array, int begin, int end) { if (begin < end) { //get the pivot position, and divide. int partitionPosition = partition(array, begin, end); // the left part of the array. quickSort(array, begin, partitionPosition - 1); // the right part of the array. quickSort(array, partitionPosition + 1, end); } } public int partition(int[] array, int begin, int end) { Random rd = new Random(); int tempPivot = rd.nextInt(end - begin + 1) + begin; swap(tempPivot, end, array); int startPoint = begin; for (int j = begin; j < end; j++) { if (array[j] < array[end]) { swap(startPoint, j, array); startPoint++; } } swap(end, startPoint, array); return startPoint; } private void swap(int p1, int p2, int[] array) { int temp = array[p1]; array[p1] = array[p2]; array[p2] = temp; } }
The non-recursive quicksort stores the pair of left index and right index in the stack, and each time the array is divided, the new pair of left and right indices are stored. If the left index is larger than or equal to the right index, we will not store that
pair of indices because the number of element in that segment should be only one.
public class QuickSortNonRecursive { public static void main(String[] args) { QuickSortNonRecursive qsnr = new QuickSortNonRecursive(); int[] array = {0, 2, -11, 2, 18, 99, 3, 5, 11, 22, 9, 100}; qsnr.quicksort(array); for (int i : array) { System.out.print(i + " "); } } public void quicksort(int[] array) { if (array == null || array.length == 1) return; //store the pair of start point and end point Stack<Integer> s = new Stack<Integer>(); s.push(0); s.push(array.length - 1); while (!s.empty()) { int right = s.pop(); int left = s.pop(); //if right <= left, the subarray only contains // one or null element if (right <= left) continue; int i = partition(array, left, right); //add the the pair of start point and end point //to stack if (left < i - 1) { s.push(left); s.push(i - 1); } if (i + 1 < right) { s.push(i+1); s.push(right); } } } public int partition (int[] array, int left, int right) { int position = left; int pointer = left; while (pointer < right) { if (array[pointer] < array[right]) { swap(array, pointer, position); position++; } pointer++; } swap(array, position, right); return position; } public void swap(int[] array, int index1, int index2) { int temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; } }
转载请注明出处:http://blog.csdn.net/beiyeqingteng
相关文章推荐
- 《c程序设计语言》读书笔记-递归实现快速排序算法
- C语言的快速排序算法代码-递归实现
- 快速排序算法递归与非递归实现(二)【转】
- 快速排序算法的递归与非递归实现(C/C++)
- 《c程序设计语言》读书笔记-递归实现快速排序算法
- [置顶] 排序算法:快速排序算法实现及分析(递归形式和非递归形式)
- 快速排序算法的递归实现
- 快速排序算法原理及java递归实现
- 【算法】快速排序算法(递归实现 从小到大排列) 排序范围(0~n-1) n为数组元素个数
- 快速排序算法的递归实现的个人理解
- 二分查找算法递归实现&&快速排序算法实现
- 一个C语言实现不含递归的高效快速排序算法
- 快速排序算法原理及java递归实现
- 快速排序算法原理及java递归实现
- 递归调用实现快速排序算法
- 非递归实现快速排序算法
- 快速排序算法原理及java递归实现
- 【模板小程序】快速排序算法实现(递归+非递归)
- 快速排序算法递归与非递归实现
- 快速排序算法原理及java递归实现