Sorting algorithms are used everyday to sort all types of information in computer programs so I decided to share a O(Nlog2N) sorting algorithm called Merge Sort with you today. This is written in Java Programming. I shared a post last year on Quick Sort algorithm and I just decided to do all of the Big-O (Nlog2N) sorting algorithms. Of course Java has many built in data structures and uses the best sorting algorithms already, but if you are learning about data structures and algorithms you might find this post handy.
First I will post the Circle class just like in the Quicksort example that can be found here by the way QuickSort sorting algorithm in java with Generics that implement Comparable
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;
}
}
Secondly you are going to need the MergeSort class which also has the main method inside it so it can run the program. The class has static methods called merge and mergeSort that do all of the work. They basically keep splitting and sorting the array untill everything is sorted and then merges it all back together.
MergeSort.java
import java.util.Arrays;
import java.util.Random;
/**
* author: copypasteearth
* date: 7/17/2019
*/
public class MergeSort<T extends Comparable<T>> {
public static <T extends Comparable<T>> void merge(int leftFirst, int leftLast, int rightFirst, int rightLast, T[] array){
T[] tempArray = Arrays.copyOf(array,array.length);
int index = leftFirst;
int saveFirst = leftFirst;
while((leftFirst <= leftLast) && (rightFirst <= rightLast)){
if(array[leftFirst].compareTo(array[rightFirst]) < 0){
tempArray[index] = array[leftFirst];
leftFirst++;
}else{
tempArray[index] = array[rightFirst];
rightFirst++;
}
index++;
}
while(leftFirst <= leftLast){
tempArray[index] = array[leftFirst];
leftFirst++;
index++;
}
while(rightFirst <= rightLast){
tempArray[index] = array[rightFirst];
rightFirst++;
index++;
}
for(index = saveFirst; index <= rightLast;index++){
array[index] = tempArray[index];
}
}
public static <T extends Comparable<T>> void mergeSort(int first, int last,T[] array){
if(first < last){
int middle = (first + last) / 2;
mergeSort(first,middle,array);
mergeSort(middle+1,last,array);
merge(first,middle,middle+1,last,array);
}
}
public static void main(String[] args){
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]);
}
MergeSort<Circle> mscircle = new MergeSort<Circle>();
mscircle.mergeSort( 0, circlearray.length-1,circlearray);
System.out.println("Circle Array Sorted");
for(Circle i: circlearray) {
System.out.println(i);
}
}
}
And that pretty much sums it up for MergeSort. Another one of your should be favorited sorting algorithms that run at a whopping O(Nlog2N) complexity. Thanks for your time and i hope you liked this article and got some use out of it. I am leaving the Link to this and QuickSort on github here it is https://github.com/copypasteearth/Sorting
Amazon Fire HD 10 tablet (newest model) built for relaxation, 10.1" vibrant Full HD screen, octa-core processor, 3 GB RAM, 32 GB, Ocean
$139.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.)Charger for MacBook Air MacBook Pro 13 14 15 16 inch 2024 2023 2022 2021 2020, M1 M2 M3 M4 Laptop 70W USB C Power Adapter, iPad, LED, 6.6FT USB-C Cable, Charging as Fast as Original Quality
$27.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.)Charger for Dell Laptop Computer 65W 45W Round Tip Power Adapter
$9.90 (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.)TP-Link Deco AX3000 WiFi 6 Mesh System(Deco X55) - Covers up to 6500 Sq.Ft. , Replaces Wireless Router and Extender, 3 Gigabit ports per unit, supports Ethernet Backhaul (3-pack)
$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 eero 6+ mesh wifi router (newest model) - Say goodbye to wifi dead spots, Coverage up to 4,500 sq. ft., Connect 75+ devices, 3-pack
$194.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.