From ce2693dbdd2e59963a5dad59e605c86a80ecc011 Mon Sep 17 00:00:00 2001 From: xinyi Date: Sat, 30 Dec 2017 21:09:46 -0800 Subject: [PATCH] 2 modifications in the function partition of quicksort with choosing pivot from median. --- .../Exercise_23_04/Exercise_23_04.class | Bin 1465 -> 1694 bytes .../Exercise_23_04/Exercise_23_04.java | 12 +++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Exercise_23/Exercise_23_04/Exercise_23_04.class b/Exercise_23/Exercise_23_04/Exercise_23_04.class index 9c811b22aa487dd483c30f5b9b037e96d381d01e..917bbe06bbdf83fc01ca0c8f9cb1fd27f634ce6b 100644 GIT binary patch literal 1694 zcmaJ>O>Yxd6g?BqhdrIxZo(uXwgV{v9EUnIA*SFEC?+W;4n{HsMQH^SXG8|uGp;>B zWYJYwcGpcSRY)z7*sv)J8lfut_Mddo{(wp)J#XxiL_v*}``)|n-1E-8_s#qL&FdWi zGbrik#W5WQjtes?%m>1ph+qsS`v@A>k->y8XLNjstT2-r&T9Bbf;#KCjz1?Mlpen$ zK`wY}wuD&8aqXKMja9pK+gh#D6Zvw(sn#l9%a@>}SBe6xaV!S2-imKkYfD!1KXlV- z`Ht^+?pst*!xZ@&mP6k_dZqNxdSp%3E%(7>#cw(8gS?% z+~==swtc(7f_fX=H9L7a-sCMN=`&T^YUFiXMS-ExaS4MBt7aEGw`%!!9eeyl8cIm8rEYC*FZG``y}sLRpm!*$DD ztK03dPGJ79?tPvEfwwoC_B-JFFIA^K`TYg#QWUIuz2f+GUc;hFXyU&25m2Glr%9Uxnt5B zf|MjTzo8DdG)CqyH=bsM9HpKmaW<7nX15{d$LEO_iyTNQC^@4f)HIg<}i- znS_`St(tP#R9_@sP+=#zJ3$&Ib2!S`Fn`Bb{!zYzC-@GG;UZ4s2GY2N3EagQe8Gp% zMiw|>ADd^IsUt(dX~W%aJY~f~ z)bn{<|5aZODdlBZEiWq^)o^)Pi*pf{%gb>!{)SfLG9HdcX(Mr?T$*d5?ER=O8NDxuwwnvB+~uyex}crGnE($PVa4&7;WE Oj^Y&8VXjZ}Km9K-pIzGk delta 899 zcmaKqOHWf#6otQYX}Rs?Qc7ug-eSQ5r9gr5P(+^EA_;Lqq9Gx{Dh-y9N)U}s=){3D zgN=!CV4OM7i4F>C!VmB__)AO__qjB2K(tNPK99BcT4&$=w*E`q`uE>oPk?cjEkeXC z8tKu{tD#Rr!cRX*lK~4;6HEqu4C$ECFl;fxsD`w~7~>iyOeRe(9WE8s<-^MDQWIgD z7TRn&aBO09>20gYl+6{U6)oGj$GPF1Tybl7b$_o=+**88*xAhQ*<58tp<;S5Yct2Z z$%4%y*KDp6Hp$pzS+cpok|L0KlHa>mc$nWvk8O;MD}rv_@_||?DQa`OyZPd#!bvRm zulz$%%nT*YT~7ax@7sTHW?B(WoF_`frtREI<4%b#X=g~f4YkNAVZn&(q7s!vZ}JHB zNfwIcDoIoQx>;3fFCLd_C2Nu=p3P&7zkCZrvV6kVf9ZEM=ceR%pd9qXv$}3GUO`*1 zUN9gw8gRr~yPVdb97&e%be61QlIoL-H%YDM-AT=MM4j@1r(DvdZn^#=u3V#-Y!=Zr z$&SgJM(AWx-khbI6?#~umo@sxkzh+^YMVixGDJz3&l%+fXkw`S$Dr%iv2v%SBK@8Riy-fhAC!QkSSZ2c$azZ%T8OdF@71z$ diff --git a/Exercise_23/Exercise_23_04/Exercise_23_04.java b/Exercise_23/Exercise_23_04/Exercise_23_04.java index 2553d9f3..af9f7373 100644 --- a/Exercise_23/Exercise_23_04/Exercise_23_04.java +++ b/Exercise_23/Exercise_23_04/Exercise_23_04.java @@ -20,9 +20,15 @@ public static void quickSort(int[] list, int first, int last) { public static int partition(int[] list, int first, int last) { int middle = list[(list.length - 1) / 2]; // choose the median element as the pivot - int pivot = median(first, middle, last); + // here should parse the element in the list instead of + // index; + int pivot = median(list[first], middle, list[last]); int low = first + 1; // Index for forward search - int high = last; // Index for backward search + // here should not forward the index + // the pivot is choose from median() + // instead of the first element in + // the list + int high = last; // Index for backward search while (high > low) { // Search forward from left @@ -69,4 +75,4 @@ public static int median(int first, int middle, int last) { return Math.max(Math.min(first, middle), Math.min(Math.max(first, middle), last)); } -} \ No newline at end of file +}