how to quicksort a string array in java
Java
package com.javacodegeeks.sorting.quicksort;
public class QuicksortStringExample {
private static String []a;
public static void main(String[] args) {
// Get an String array
a = new String[]{"X","E","C","A"};
// prints the given array
printArray();
// sort the array
sort();
System.out.println("");
//prints the sorted array
printArray();
}
// This method sort an array internally and internally calls quickSort
public static void sort(){
int left = 0;
int right = a.length-1;
quickSort(left, right);
}
// This method is used to sort the array using quicksort algorithm.
// It takes left and the right end of the array as two cursors
private static void quickSort(int left,int right){
// If both cursor scanned the complete array quicksort exits
if(left >= right)
return;
// Pivot using median of 3 approach
String pivot = getMedian(left, right);
int partition = partition(left, right, pivot);
// Recursively, calls the quicksort with the different left and right parameters of the sub-array
quickSort(0, partition-1);
quickSort(partition+1, right);
}
// This method is used to partition the given array and returns the integer which points to the sorted pivot index
private static int partition(int left,int right,String pivot){
int leftCursor = left-1;
int rightCursor = right;
while(leftCursor < rightCursor){
while(((Comparable<String>)a[++leftCursor]).compareTo(pivot) < 0);
while(rightCursor > 0 && ((Comparable<String>)a[--rightCursor]).compareTo(pivot) > 0);
if(leftCursor >= rightCursor){
break;
}else{
swap(leftCursor, rightCursor);
}
}
swap(leftCursor, right);
return leftCursor;
}
public static String getMedian(int left,int right){
int center = (left+right)/2;
if(((Comparable<String>)a[left]).compareTo(a[center]) > 0)
swap(left,center);
if(((Comparable<String>)a[left]).compareTo(a[right]) > 0)
swap(left, right);
if(((Comparable<String>)a[center]).compareTo(a[right]) > 0)
swap(center, right);
swap(center, right);
return a[right];
}
// This method is used to swap the values between the two given index
public static void swap(int left,int right){
String temp = a[left];
a[left] = a[right];
a[right] = temp;
}
public static void printArray(){
for(String i : a){
System.out.print(i+" ");
}
}
}
Also in Java:
- Title
- reading string after double in java
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- java split string
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- java dictionary
- Category
- Java
- Title
- java create new object
- Category
- Java
- Title
- how to iterate hashmap java
- Category
- Java
- Title
- treeset java descending order using comparator
- Category
- Java
- Title
- spigot custom join message
- Category
- Java
- Title
- java load image
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- java array to list
- Category
- Java
- Title
- java swing button on click
- Category
- Java
- Title
- how to do sex java
- Category
- Java
- Title
- java singleton
- Category
- Java
- Title
- remove first character from string
- Category
- Java
- Title
- how to convert char to uppercase java
- Category
- Java
- Title
- exception handling and reprompting
- Category
- Java
- Title
- difference between java and javascript
- Category
- Java
- Title
- dataframe to dict without index
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- convert integer array to string array
- Category
- Java
- Title
- how to change actionbar color in android programmatically
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- loop through array java
- Category
- Java
- Title
- remove duplicates from singly linked list
- Category
- Java
- Title
- import java.util.*;
- Category
- Java
- Title
- how to quicksort a string array in java
- Category
- Java
- Title
- java create file with content
- Category
- Java
- Title
- boolean checkbox swing
- Category
- Java
- Title
- summary of operators java
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- binary number input in int java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- ujava saum of positive integers
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- two dimensional array in java example program
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- java store hexadecimal value
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- java int to hex fixed length
- Category
- Java
- Title
- break for loop java
- Category
- Java
- Title
- java bukkit double jump
- Category
- Java
- Title
- java indexof array
- Category
- Java
- Title
- using get in map in java
- Category
- Java
- Title
- how to initialize an array in java
- Category
- Java
- Title
- java builder pattern example
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- java stack peek
- Category
- Java
- Title
- funzione random in java
- Category
- Java
- Title
- check java version cmd
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- arrays.aslist.add
- Category
- Java
- Title
- string length in java
- Category
- Java
- Title
- spigot spawn entity
- Category
- Java
- Title
- waht does&& mean in java
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- how to use for loop for array in java
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- java string regexp replace
- Category
- Java
- Title
- minecraft sounds spigot
- Category
- Java
- Title
- how to see if a shape is touching another shape in java
- Category
- Java
- Title
- for loop java
- Category
- Java
- Title
- how to print array in java using for loop
- Category
- Java
- Title
- Java sort Map by values
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- teimpo en segundos java
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- get current unix timestamp java
- Category
- Java
- Title
- java how to get current date
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the `--no-shrink` flag to this command.
- Category
- Java
- Title
- java string to char array
- Category
- Java
- Title
- Java loop throug gson JsonElement
- Category
- Java
- Title
- java syntax
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- difference between java and javax
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- transformer un string en double java
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- java 8 random string generator
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- java if a or b
- Category
- Java
- Title
- best companies to workk in
- Category
- Java
- Title
- java string to uuid
- Category
- Java
- Title
- Java how to handle HTTP GET request after establishing TCP connection
- Category
- Java
- Title
- java override equals and hashcode
- Category
- Java
- Title
- change fragment in android studio
- Category
- Java
- Title
- math.pow java
- Category
- Java
- Title
- thread sleep java
- Category
- Java
- Title
- java clear console
- Category
- Java
- Title
- get material of block bukkit
- Category
- Java
- Title
- listview get selected java
- Category
- Java