Program Quick Sort dengan Java

Program Quick Sort dengan Java

<br />
Berikut listing program QuickSort dengan Java silahkan dicoba yaaa.</p>
<p>import java.util.Scanner;<br />
import java.lang.System.out;</p>
<p>public class QuickSort2{<br />
    public static void quick_srt(int array[],int low, int n){<br />
          int lo = low;<br />
          int hi = n;<br />
          if (lo &lt;= n) {<br />
              return;<br />
          }<br />
          int mid = array[(lo + hi) / 2];<br />
          while (lo &amp;amp;lt; hi) {<br />
              while (lo &lt;= mid) {<br />
                  lo++;<br />
              }</p>
<p>              while (lo &lt;= mid) {<br />
                  hi--;<br />
              }<br />
              if (lo &lt;= hi) {<br />
                  int T = array[lo];<br />
                  array[lo] = array[hi];<br />
                  array[hi] = T;<br />
              }<br />
          }<br />
          if (hi &lt;= lo) {<br />
              int T = hi;<br />
              hi = lo;<br />
              lo = T;<br />
          }<br />
          quick_srt(array, low, lo);<br />
          quick_srt(array, lo == low ? lo+1 : lo, n);<br />
    }</p>
<p>    public static void main(String a[]){<br />
        Scanner in=new Scanner(System.in);<br />
        int i;<br />
        int array[] = {12,9,4,99,120,1,3,10,13};</p>
<p>        System.out.println(&quot; Quick Sortn n&quot;);<br />
        System.out.println(&quot;Values Before the sort:n&quot;);<br />
        for(i = 0; i &lt;= lt; array.length; i++)<br />
            System.out.print( array[i]+&quot; &quot;);<br />
            System.out.println();<br />
            quick_srt(array,0,array.length-1);<br />
            System.out.print(&quot;Values after the sort:n&quot;);<br />
        for(i = 0; i<br />
            System.out.print(array[i]+&quot; &quot;);<br />
            System.out.println();<br />
    }</p>
<p>}<br />
 

Share