I've been away from Programming and the Tech Industry for sometime. I thought I'd look at and try some of the older basic stuff. I bushed up to QuickSort and found most existing implementations of it, in Texts and on the web, to be a little convoluted. So I tried to simplify the implementation to be more in line with the spirit of the algorithm. Let me know your thoughts.
Design Philosophy: I interpreted/implemented Bubble Sort with the 'vibe' that the sort produces, and quick sort with its 'vibe' as well: the vibe that comes from a direct interpretation of the algorithm into code. As in, this is how i'd describe Quick Sort in implementation, without the 'high' or 'low', really distilling down to what i see as: a sorting algorithm that pivots an element into place and splits the rest of the elements into two lists based on whether the rest of the values are lesser or greater than the pivot, and recursively repeats the process. I want to avoid the normally available code that 'feels' disjoint from the essence of the algorithm.
package com.progint;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.SecureRandom;
public class QuickSortenDine {
private ArrayList<Integer> listToSort;
private ArrayList<Integer> sortedList;
private SecureRandom pivotPicker; /*We're going to pick a Random pivot*/
public QuickSortenDine() {
listToSort = new ArrayList<Integer>();
sortedList = new ArrayList<Integer>();
pivotPicker = new SecureRandom();
}
void quickSort() {
quickSort(listToSort);
}
void quickSort(ArrayList<Integer> currentList) {
int listSize = currentList.size();
if (listSize == 1) sortedList.add(currentList.get(0));
if (listSize <= 1) return;
int low = 0 , high = listSize;
int pivot = pivotPicker.nextInt(listSize);
ArrayList<Integer> lowerList = new ArrayList<Integer>();
ArrayList<Integer> higherList = new ArrayList<Integer>();
int pivotValue = currentList.get(pivot);
int currentIndex = low;
while (currentIndex < high) {
if (currentIndex == pivot) {currentIndex++; continue;}
int currentElement = currentList.get(currentIndex++);
if (currentElement < pivotValue) {lowerList.add(currentElement);}
else/************* > **********/ {higherList.add(currentElement);}
}
quickSort(lowerList); sortedList.add(pivotValue); quickSort(higherList);
}
public void readListToBeSorted() throws IOException {
int len;
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the List: ");
len = Integer.parseInt(consoleReader.readLine());
if (len<=0) {System.out.println("\nQuitting"); System.exit(0);}
System.out.println("Enter the numbers: ");
for (int i=0; i<len; i++) {
int number;
try {
number = Integer.parseInt(consoleReader.readLine());
}
catch (NumberFormatException ne) {
System.out.println("\nEnter Numbers Only\n");
i--;
continue;
}
listToSort.add(number);
System.out.println();
}
}
void printList() {
for (int i=0; i<sortedList.size(); i++) {
System.out.print(sortedList.get(i));
if (i==sortedList.size()-1) continue;
else System.out.print(",");
}
}
public static void main(String args[]) throws IOException {
QuickSortenDine quickEn = new QuickSortenDine();
quickEn.readListToBeSorted();
System.out.println("\nQuick Sorting...");
quickEn.quickSort();
quickEn.printList();
}
}
java.security.SecureRandom
instead ofjava.util.Random
? \$\endgroup\$==
<
>
, and didn't bother double-checking what was being compared. As for SecureRandom, I don't think you need it here, and it's probably much slower, but that's your decision. \$\endgroup\$