Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

quicksort for arraylist

public static ArrayList<Vehicle> quickSort(ArrayList<Vehicle> list)
{
    if (list.isEmpty()) 
        return list; // start with recursion base case
    ArrayList<Vehicle> sorted;  // this shall be the sorted list to return, no needd to initialise
    ArrayList<Vehicle> smaller = new ArrayList<Vehicle>(); // Vehicles smaller than pivot
    ArrayList<Vehicle> greater = new ArrayList<Vehicle>(); // Vehicles greater than pivot
    Vehicle pivot = list.get(0);  // first Vehicle in list, used as pivot
    int i;
    Vehicle j;     // Variable used for Vehicles in the loop
    for (i=1;i<list.size();i++)
    {
        j=list.get(i);
        if (j.compareTo(pivot)<0)   // make sure Vehicle has proper compareTo method 
            smaller.add(j);
        else
            greater.add(j);
    }
    smaller=quickSort(smaller);  // capitalise 's'
    greater=quickSort(greater);  // sort both halfs recursively
    smaller.add(pivot);          // add initial pivot to the end of the (now sorted) smaller Vehicles
    smaller.addAll(greater);     // add the (now sorted) greater Vehicles to the smaller ones (now smaller is essentially your sorted list)
    sorted = smaller;            // assign it to sorted; one could just as well do: return smaller

    return sorted;
}
Comment

PREVIOUS NEXT
Code Example
Java :: convert char array to set in java 
Java :: how to loop through arraylist of objects in java 
Java :: reviews button ade android studio 
Java :: android maven dependency 
Java :: jsonArray to list in java 
Java :: navigation view menu color android 
Java :: hide steam games from friends 
Java :: write in text file android studio 
Java :: java stop script 
Java :: what does setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) is used for 
Java :: check if a list contains a string java 
Java :: java initialize class 
Java :: java remove map 
Java :: android get last crash adb 
Java :: abstraction in java 
Java :: richest customer wealth 
Java :: android setTextColor not working 
Java :: Quick Sort Java Implementation 
Java :: java print 2d array as table 
Java :: how to change custom font to bold italic in java 
Java :: java actionperformed 
Java :: reverse number java 
Java :: java math ceil 
Java :: get spring application context 
Java :: linkedlist java 
Java :: calling this in constructor java 
Java :: remove empty elements from array java 
Java :: lowercase string java 
Java :: java string vers int 
Java :: remove last node from linked list java 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =