From fc8635016cb649a151680ff97bae6de0971fd888 Mon Sep 17 00:00:00 2001 From: cinita Date: Thu, 10 Jan 2019 00:43:52 +0800 Subject: [PATCH] update quick sort --- java/12_sorts/QuickSort.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/java/12_sorts/QuickSort.java b/java/12_sorts/QuickSort.java index d2523c2..0813657 100644 --- a/java/12_sorts/QuickSort.java +++ b/java/12_sorts/QuickSort.java @@ -24,10 +24,13 @@ public class QuickSort { int i = p; for(int j = p; j < r; ++j) { if (a[j] < pivot) { - int tmp = a[i]; - a[i] = a[j]; - a[j] = tmp; - ++i; + if (i == j) { + ++i; + } else { + int tmp = a[i]; + a[i++] = a[j]; + a[j] = tmp; + } } }