Skip to content

Instantly share code, notes, and snippets.

@xrigau
Created June 7, 2014 19:24
Show Gist options
  • Save xrigau/13d3dfb12ad9820e5222 to your computer and use it in GitHub Desktop.
Save xrigau/13d3dfb12ad9820e5222 to your computer and use it in GitHub Desktop.
Sorting Algorithms
interface SortingAlgorithm {
int[] sort(int[] values);
}
class QuickSort implements SortingAlgorithm {
@Override
int[] sort(int[] values) {
return quickSort(values);
}
// ...
}
class MergeSort implements SortingAlgorithm {
@Override
int[] sort(int[] values) {
return mergeSort(values);
}
// ...
}
class SortingAlgorithmFactory() {
SortingAlgorithm newAlgorithm(Type type) {
switch (type) {
case QUICKSORT:
return new QuickSort();
case MERGESORT:
return new MergeSort();
default:
throw new IllegalArgumentException("Invalid type");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment