Quicksort java
import java.util.Arrays;
public class QuickSortInJava
{
int partition(int[] arrNumbers, int low, int high)
{
int pivot = arrNumbers[high];
// smaller element index
int a = (low - 1);
for(int b = low; b < high; b++)
{
// if current element is smaller than the pivot
if(arrNumbers[b] < pivot)
{
a++;
// swap arrNumbers[a] and arrNumbers[b]
int temp = arrNumbers[a];
arrNumbers[a] = arrNumbers[b];
arrNumbers[b] = temp;
}
}
// swap arrNumbers[a + 1] and arrNumbers[high] or pivot
int temp = arrNumbers[a + 1];
arrNumbers[a + 1] = arrNumbers[high];
arrNumbers[high] = temp;
return a + 1;
}
void sort(int[] arrNumbers, int low, int high)
{
if (low < high)
{
int p = partition(arrNumbers, low, high);
/* recursive sort elements before
partition and after partition */
sort(arrNumbers, low, p - 1);
sort(arrNumbers, p + 1, high);
}
}
static void displayArray(int[] arrNumbers)
{
int s = arrNumbers.length;
for(int a = 0; a < s; ++a)
System.out.print(arrNumbers[a] + " ");
System.out.println();
}
public static void main(String[] args)
{
int[] arrNumbers = {59, 74, 85, 67, 56, 29, 68, 34};
int s = arrNumbers.length;
QuickSortInJava obj = new QuickSortInJava();
obj.sort(arrNumbers, 0, s - 1);
System.out.println("After sorting array: ");
displayArray(arrNumbers);
}
}
3.78
9
// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.youtube.com/watch?v=aXXWXz5rF64
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
function partition(list, start, end) {
const pivot = list[end];
let i = start;
for (let j = start; j < end; j += 1) {
if (list[j] <= pivot) {
[list[j], list[i]] = [list[i], list[j]];
i++;
}
}
[list[i], list[end]] = [list[end], list[i]];
return i;
}
function quicksort(list, start = 0, end = undefined) {
if (end === undefined) {
end = list.length - 1;
}
if (start < end) {
const p = partition(list, start, end);
quicksort(list, start, p - 1);
quicksort(list, p + 1, end);
}
return list;
}
quicksort([5, 4, 2, 6, 10, 8, 7, 1, 0]);
Thank you!
9
0
4.17
6
//GOD's quicksort
public static <E extends Comparable<E>> List<E> sort(List<E> col) {
if (col == null || col.isEmpty())
return Collections.emptyList();
else {
E pivot = col.get(0);
Map<Integer, List<E>> grouped = col.stream()
.collect(Collectors.groupingBy(pivot::compareTo));
return Stream.of(sort(grouped.get(1)), grouped.get(0), sort(grouped.get(-1)))
.flatMap(Collection::stream).collect(Collectors.toList());
}
}
Thank you!
6
0
0
0
import java.util.*;
class QuickSort {
//selects last element as pivot, pi using which array is partitioned.
int partition(int intArray[], int low, int high) {
int pi = intArray[high];
int i = (low-1); // smaller element index
for (int j=low; j<high; j++) {
// check if current element is less than or equal to pi
if (intArray[j] <= pi) {
i++;
// swap intArray[i] and intArray[j]
int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
}
}
// swap intArray[i+1] and intArray[high] (or pi)
int temp = intArray[i+1];
intArray[i+1] = intArray[high];
intArray[high] = temp;
return i+1;
}
//routine to sort the array partitions recursively
void quick_sort(int intArray[], int low, int high) {
if (low < high) {
//partition the array around pi=>partitioning index and return pi
int pi = partition(intArray, low, high);
// sort each partition recursively
quick_sort(intArray, low, pi-1);
quick_sort(intArray, pi+1, high);
}
}
}
class QUICK_SORT{
public static void main(String args[]) {
//initialize a numeric array, intArray
int intArray[] = {3,2,1,6,5,4};
int n = intArray.length;
//print the original array
System.out.println("Original Array: " + Arrays.toString(intArray));
//call quick_sort routine using QuickSort object
QuickSort obj = new QuickSort();
obj.quick_sort(intArray, 0, n-1);
//print the sorted array
System.out.println("\nSorted Array: " + Arrays.toString(intArray));
}
}
Thank you!
0
0
4.6
5
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;
}
Thank you!
5
0
4
1
//Worst case: if pivot was at the end and all numbers are greater than the pivot
//Best case: (n log n): due to you cutting the array in half n times
// Average case(n log n): ^
public class QuickSort {
public static void main(String [] args) {
int [] arr = {5, 1, 6, 4, 2, 3};
quickSort(arr, 0, arr.length);
/*Using a for each loop to print out the ordered numbers from the array*/
for(int i: arr){
System.out.println(i);
}// End of the for-each loop
}// End of the main method
public static void quickSort(int [] arr, int start, int end) {
/*Base case*/
if(start >= end) {
return;
}// End of the base case
int pIndex = partition(arr, start, end);// The index of the pivot.
//System.out.println(pIndex);
/*Recursive cases*/
quickSort(arr, start, pIndex - 1);// Left side of the array
quickSort(arr, pIndex + 1, end);// Right side of the array
}// End of the quicksort method
public static int partition(int [] arr, int start, int end) {
int pivot = arr[end - 1];// Select the pivot to be the last element
int pIndex = start;// (Partition index) Indicates where the pivot will be.
for(int i = start; i < end; i++) {
if(arr[i] < pivot) {
// if a number is smaller than the pivot, it gets swapped with the Partition index
int temp = arr[pIndex];
arr[pIndex] = arr[i];
arr[i] = temp;
pIndex++;// increments the partition index, only stops when pivot is in the right area
}// End of the if statement
}// End of the for loop
// This swaps the pivot with the element that stopped where the pivot should be
arr[end - 1] = arr[pIndex];
arr[pIndex] = pivot;
return pIndex;
}// End of the partition method
}// End of the QuickSort class
Thank you!
1
0
Are there any code examples left?
New code examples in category Java
-
Java 2022-03-27 21:35:04 Sort string array in case insensitive order and case sensitive order java
-
Java 2022-03-27 21:25:10 java -jar -l resources\es.porperties -i ejemplo.txt -o inject.bin
-
Java 2022-03-27 21:20:21 Debug & Fix a 2-Dimensional Array Java Console Application
-
Java 2022-03-27 20:40:19 TreeSet headSet(E toElement) method in java
-
Java 2022-03-27 19:30:06 close keyboard android
-
Java 2022-03-27 19:20:40 java measure execution time
-
Java 2022-03-27 19:10:06 how to add cardview support in android studio
-
Java 2022-03-27 18:55:08 android studio lower case letters on a button