快速排序算法多种语言实现
于此我们展示在数种语言下的几个快速排序实现。我们在此仅秀出最普遍或独特的一些;针对其他的实现,参见快速排序实现条目。
主条目:快速排序实现
[编辑]C
排序一个整数的数组
void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; } void quicksort(int arr[],int beg,int end) { if (end >= beg + 1) { int piv = arr[beg], k = beg + 1, r = end; while (k < r) { if (arr[k] < piv) k++; else swap(&arr[k], &arr[r--]); } if (arr[k] < piv){ r++; swap(&arr[k],&arr[beg]); quicksort(arr, beg, k); quicksort(arr, r, end); }else { if (end - beg == 1) return; swap(&arr[--k],&arr[beg]); quicksort(arr, beg, k); quicksort(arr, r, end); } } }
另一个版本
void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; } void qsort(int arr[],int l,int r) { int i = l; int j = r; int key = arr[(i+j)/2]; while(i < j) { for(;(i < r)&&(arr[i] < key);i++); for(;(j > l)&&(arr[j] > key);j--); if (i <= j) { if ( i != j) { swap(&arr[i],&arr[j]); } i++; j--; } } if (i < r) qsort(arr,i,r); if (j > l) qsort(arr,l,j); }
第三个版本
/************************************************************************/ /* 总觉得以上两个版本不对,附上一个版本,真正的快排没有swap的,longrenle*/ /************************************************************************/ void quickSort(int data[], int left, int right) { int temp = data[left]; int ptr = left; int i = left + 1, j = right; if(data[i] <= temp) { data[ptr] = data[i]; ptr = i; } while(i!=j) { if(data[j] >= temp) { j--; } else { data[ptr] = data[j]; ptr = j; while(data[i] <= temp && i != j) { i++; } data[ptr] = data[i]; ptr = i; } } data[ptr] = temp; if(left < ptr - 1) quickSort(data, left, ptr - 1); if(ptr + 1 < right) quickSort(data, ptr + 1, right); }
[编辑]C++
这是一个使用标准模版库(STL)的泛型式快速排序版本。
#include <functional> #include <algorithm> #include <iterator> template< typename BidirectionalIterator, typename Compare > void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) { if( first != last ) { BidirectionalIterator left = first; BidirectionalIterator right = last; BidirectionalIterator pivot = left++; while( left != right ) { if( cmp( *left, *pivot ) ) { ++left; } else { while( (left != right) && cmp( *pivot, *right ) ) right--; std::iter_swap( left, right ); } } if (cmp( *pivot, *left )) --left; std::iter_swap( first, left ); quick_sort( first, left, cmp ); quick_sort( right, last, cmp ); } } template< typename BidirectionalIterator > inline void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) { quick_sort( first, last, std::less_equal< typename std::iterator_traits< BidirectionalIterator >::value_type >() ); }
[编辑]Java
import java.util.Comparator; import java.util.Random; public class Quicksort { public static final Random RND = new Random(); private static void swap(Object[] array, int i, int j) { Object tmp = array[i]; array[i] = array[j]; array[j] = tmp; } private static <E> int partition(E[] array, int begin, int end, Comparator<? super E> cmp) { int index = begin + RND.nextInt(end - begin + 1); E pivot = array[index]; swap(array, index, end); for (int i = index = begin; i < end; ++ i) { if (cmp.compare(array[i], pivot) <= 0) { swap(array, index++, i); } } swap(array, index, end); return (index); } private static <E> void qsort(E[] array, int begin, int end, Comparator<? super E> cmp) { if (end > begin) { int index = partition(array, begin, end, cmp); qsort(array, begin, index - 1, cmp); qsort(array, index + 1, end, cmp); } } public static <E> void sort(E[] array, Comparator<? super E> cmp) { qsort(array, 0, array.length - 1, cmp); } }
[编辑]Perl
sub qsort { return () unless @_; (qsort(grep { $_ < $_[0] } @_[1..$#_]), $_[0], qsort(grep { $_ >= $_[0] } @_[1..$#_])); }
[编辑]Python
def qsort(L): if not L: return [] return qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + \ qsort([x for x in L[1:] if x>=L[0]])
[编辑]Joy
DEFINE sort == [small][] [uncons [>] split] [[swap] dip cons concat] binrec .
[编辑]PHP
function quicksort($seq) { if (count($seq) > 1) { $k = $seq[0]; $x = array(); $y = array(); for ($i=1; $i<count($seq); $i++) { if ($seq[$i] <= $k) { $x[] = $seq[$i]; } else { $y[] = $seq[$i]; } } $x = quicksort($x); $y = quicksort($y); return array_merge($x, array($k), $y); } else { return $seq; } }
[编辑]Haskell
sort :: (Ord a) => [a] -> [a] sort [] = [] sort (pivot:rest) = sort [y | y <- rest, y < pivot] ++ [pivot] ++ sort [y | y <- rest, y >=pivot]
[编辑]Prolog
split(H, [A|X], [A|Y], Z) :- order(A, H), split(H, X, Y, Z). split(H, [A|X], Y, [A|Z]) :- not(order(A, H)), split(H, X, Y, Z). split(_, [], [], []). quicksort([], X, X). quicksort([H|T], S, X) :- split(H, T, A, B), quicksort(A, S, [H|Y]), quicksort(B, Y, X).
[编辑]Ruby
def sort(array) # return [] if array.empty? return array if array.size < 2 left, right = array[1..-1].partition { |y| y <= array.first } sort(left) + [ array.first ] + sort(right) end
[编辑]SML
This example demonstrates the use of an arbitrary predicate in a functional language.
fun quicksort lt lst = let val rec sort = fn [] => [] | (x::xs) => let val (left,right) = List.partition (fn y => lt (y, x)) xs in sort left @ x :: sort right end in sort lst end
[编辑]Pascal
program QSort; const Max = 1000; var Data: array[1..Max] of integer; I: Integer; procedure Sort(l, r: Integer); var i, j, x, y: integer; begin i := l; j := r; x := Data[(l+r) DIV 2]; while i<=j do begin while Data[i] < x do inc(i); while x < Data[j] do dec(j); if i <= j then begin y := Data[i]; Data[i] := Data[j]; Data[j] := y; inc(i); dec(j); end; end; if l < j then Sort(l, j); if i < r then Sort(i, r); end; begin {QSort} Randomize; for i := 1 to Max do Data[i] := Random(30000); Sort(1, Max); Writeln; for i := 1 to Max do Write(Data[i]:8); end.
[编辑]C#
public static void Sort(int[] numbers) { Sort(numbers, 0, numbers.Length - 1); } private static void Sort(int[] numbers, int left, int right) { if (left < right) { int middle = numbers[(left + right) / 2]; int i = left - 1; int j = right + 1; while (true) { while (numbers[++i] < middle) ; while (numbers[--j] > middle) ; if (i >= j) break; Swap(numbers, i, j); } Sort(numbers, left, i - 1); Sort(numbers, j + 1, right); } } private static void Swap(int[] numbers, int i, int j) { int number = numbers[i]; numbers[i] = numbers[j]; numbers[j] = number; }
[编辑]VB.Net
Private m_i32ArrSort(10000) As Integer Private Sub main() m_vSorting(1, 10000) End Sub ' Sort Private Sub m_vSorting(ByVal i32Left As Integer, ByVal i32Right As Integer) If (i32Left >= i32Right) Then Return Dim i32I, i32J As Integer Dim i32Middle = m_i32ArrSort(i32Left) i32I = i32Left + 1 i32J = i32Right Do While (i32I <= i32Right) If (m_i32ArrSort(i32I) > i32Middle) Then Exit While i32I += 1 End While While (i32J > i32Left) If (m_i32ArrSort(i32J) < i32Middle) Then Exit While i32J -= 1 End While If (i32I > i32J) Then Exit Do m_vSwap(i32I, i32J) Loop m_vSwap(i32Left, i32J) m_vSorting(i32Left, i32J) m_vSorting(i32J + 1, i32Right) End Sub ' Swap Private Sub m_vSwap(ByVal i32I As Integer, ByVal i32J As Integer) Dim i32Tmp As Integer = m_i32ArrSort(i32I) m_i32ArrSort(i32I) = m_i32ArrSort(i32J) m_i32ArrSort(i32J) = i32Tmp End Sub