public class QuickSort {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 3, 7, 1};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
groovy
class QuickSort {
static void main(String[] args) {
int[] arr = [5, 2, 9, 3, 7, 1]
quickSort(arr, 0, arr.length - 1)
println arr
}
static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high)
quickSort(arr, low, pivot - 1)
quickSort(arr, pivot + 1, high)
}
}
static int partition(int[] arr, int low, int high) {
int pivot = arr[high]
int i = low - 1
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++
swap(arr, i, j)
}
}
swap(arr, i + 1, high)
return i + 1
}
static void swap(int[] arr, int i, int j) {
int temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
groovy
class DynamicClass {
def greeting = "Hello, "
def sayHello(String name) {
println greeting + name
}
}
def dynamicObj = new DynamicClass()
dynamicObj.sayHello("John")
dynamicObj.addMethod("sayGoodbye", { String name -> println "Goodbye, " + name })
dynamicObj.sayGoodbye("John")