Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each iteration of the sort is suppose to reduce the sortable range. Basically, it's a divide and conquer algorithm.</p> <p>The <code>pivot</code> finds the "mid" point of the range you've specified</p> <p>The problem is, you're randomising the <code>pivot</code>, so each time you run through the sort, it's comparing some random section, probably including some section you've already sorted...</p> <p>So, instead of using...</p> <pre><code>int pivot = r.nextInt(high-low+1)+low; </code></pre> <p>You should be using...</p> <pre><code>int pivot = low + (high - low) / 2; </code></pre> <p>For example...</p> <pre><code>import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class QuickSort { static String[] strings; public static void main(String[] args) { List&lt;String&gt; values = new ArrayList&lt;&gt;(26); for (int index = 0; index &lt; 26; index++) { values.add(new String(new char[]{(char)(65 + index)})); } Collections.shuffle(values); strings = values.toArray(new String[values.size()]); System.out.println("Before"); for (int i = 0; i &lt; strings.length; i++) { System.out.print(strings[i] + " "); } System.out.println(""); qsort(0, strings.length - 1); System.out.println("The array, quicksorted: "); for (int i = 0; i &lt; strings.length; i++) { System.out.print(strings[i] + " "); } System.out.println("\n"); } static void qsort(int low, int high) { int i = low, j = high; // Get the pivot element int pivot = low + (high - low) / 2; String value = strings[pivot]; // Divide into two lists while (i &lt;= j) { while (strings[i].compareTo(value) &lt; 0) { i++; } while (strings[j].compareTo(value) &gt; 0) { j--; } if (i &lt;= j) { exchange(i, j); i++; j--; } } // Recursion if (low &lt; j) { qsort(low, j); } if (i &lt; high) { qsort(i, high); } } static void exchange(int i, int j) { String temp = strings[i]; strings[i] = strings[j]; strings[j] = temp; } } </code></pre> <p>Which produces...</p> <pre><code>Before N S B A F Z X J Q K V C L R W E H Y M U G I D P T O The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before G U P Q D A W T R M E O X J S C I V Y F H L B N Z K The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before D Z C B Q O K W X F V G R S A U P T H Y I E N L M J The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before Q K H B W N J V C Y U O R P G I F D Z E L S A X M T The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before R V P G E S C A H W X I T D Z B K Q F M U Y L J N O The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before L O T E U D H N P J V I Q C X S Z W A R F K G Y B M The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before I E J F U X P K R Q L S C O Y W G A Z B V M D H N T The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before X L K T W E V J N Y G H O Q I M C P A R B F S U Z D The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before U X N T K Q S V P F W C G Y O L A B E H J R D M Z I The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Before A J Z C M Y O Q F L K D P S X W N T I B H E R U V G The array, quicksorted: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z </code></pre> <p>nb- Don't stress of the use of <code>List</code>, I just generated some random <code>String</code>s ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload