In this article I’m going to touch on the sorting algorithm called Quicksort. Its worst case time complexity is O(n^2) and its best case time complexity is O(nlogn). Firstly the method we are going to make is going to take a generic array and those elements should implement the Comparable interface so they can be sorted. Take this Circle class for example. When you implement the Comparable interface you specify in the compareTomethod what makes a circle greater than, less than, or equal to another circle. In this example we return whether the radius of the circle is bigger or smaller than the circle it is being compared to.
Circle.java
/**
* author: copypasteearth
* date: 7/17/2019
*/
public class Circle implements Comparable<Circle> {
public int xValue;
public int yValue;
public int radius;
@Override
public int compareTo(Circle o) {
return (this.radius - o.radius);
}
@Override
public String toString() {
return "x: " + xValue + " ---y: " + yValue + " ---radius: " + radius;
}
}
Next we create the Quicksort class that has the method quicksort and a main method to test out the quicksort method. The quicksort method is a recursive method and the base case is if(a < b). The method goes through the generic array and sorts the elements based on what the compareTo method tells it. Here is the Quicksort class.
Quicksort.java
/**
* author: copypasteearth
* date: 7/17/2019
*/
import java.util.Random;
public class QuickSort<T extends Comparable<T>> {
public static <T extends Comparable<T>> void quicksort(T[] array, int a, int b) {
if (a < b) {
int i = a, j = b;
T x = array[(i + j) / 2];
do {
while (array[i].compareTo(x) < 0) i++;
while (x.compareTo(array[j]) < 0) j--;
if ( i <= j) {
T tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
} while (i <= j);
quicksort(array, a, j);
quicksort(array, i, b);
}
}
public static void main(String[] args) {
Integer[] integerarray = {50, 55, 11, 89, 90, 1, 20, 11};
QuickSort<Integer> qsinteger = new QuickSort<Integer>();
qsinteger.quicksort(integerarray, 0, integerarray.length-1);
for(Integer i: integerarray) {
System.out.println(i);
}
String[] stringarray = {"bird","moth","apple","zebra","banana","desert","pig"};
QuickSort<String> qsstring = new QuickSort<String>();
qsstring.quicksort(stringarray, 0, stringarray.length-1);
for(String i: stringarray) {
System.out.println(i);
}
Circle[] circlearray = new Circle[20];
Random rand = new Random();
for (int index = 0; index < 20; index++)
{
circlearray[index] = new Circle();
circlearray[index].xValue = Math.abs(rand.nextInt()) % 100;
circlearray[index].yValue = Math.abs(rand.nextInt()) % 100;
circlearray[index].radius = Math.abs(rand.nextInt()) % 100;
}
System.out.println("Circle Array Unsorted....");
for(int i = 0;i < 20;i++){
System.out.println(circlearray[i]);
}
QuickSort<Circle> qscircle = new QuickSort<Circle>();
qscircle.quicksort(circlearray, 0, circlearray.length-1);
System.out.println("Circle Array Sorted");
for(Circle i: circlearray) {
System.out.println(i);
}
}
}
If you run this code you will see the results from the main method. First it sorts an Integer array and then a String array. Then it makes a Circle array and prints them out unsorted then it sorts them and then prints them out again sorted. That pretty much does it for this example on quicksorting. In the future I will probably go through the rest of the sorting algorithms. Hope you enjoyed it.
Donate or Subscribe to support Copypasteearth!!!!!
TP-Link AX1800 WiFi 6 Router V4 (Archer AX21) – Dual Band Wireless Internet Router, Gigabit Router, Easy Mesh, Works with Alexa - A Certified for Humans Device
$84.99 (as of November 19, 2024 02:53 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire HD 8 Kids tablet, ages 3-7. Top-selling 8" kids tablet on Amazon - 2022. Ad-free content with parental controls included, 13-hr battery, 64 GB, Blue
$79.99 (as of November 19, 2024 02:53 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire 7 Kids tablet (newest model) ages 3-7. Top-selling 7" kids tablet on Amazon | ad-free content with parental controls included, 10-hr battery, 16 GB, Purple
$109.99 (as of November 19, 2024 02:53 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire HD 10 Kids Pro tablet (newest model) ages 6-12 | Bright 10.1" HD screen | Slim case for older kids, ad-free content, parental controls, 13-hr battery, 32 GB, Happy Day
$189.99 (as of November 19, 2024 02:53 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Amazon Fire Max 11 tablet (newest model) vivid 11” display, all-in-one for streaming, reading, and gaming, 14-hour battery life, optional stylus and keyboard, 64 GB, Gray
$229.99 (as of November 19, 2024 02:53 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Author: John Rowan
I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.