Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.Arrays;

public class QuickSort {

static void swap(int[] arr, int i, int j) {

int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

static int partition(int[] arr, int low, int high) {

int pivot = arr[high];
int i = (low - 1);

for(int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}

static void quickSort(int[] arr, int low, int high) {

if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

public static void main(String[] args) {

int[] arr = { 8,10,3,2,6};
int n = arr.length;

quickSort(arr, 0, n - 1);
System.out.println("Sorted array: " + Arrays.toString(arr));
}

}
46 changes: 46 additions & 0 deletions quickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.Arrays;

public class QuickSort {

static void swap(int[] arr, int i, int j) {

int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

static int partition(int[] arr, int low, int high) {

int pivot = arr[high];
int i = (low - 1);

for(int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}

static void quickSort(int[] arr, int low, int high) {

if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

public static void main(String[] args) {

int[] arr = { 8,10,3,2,6};
int n = arr.length;

quickSort(arr, 0, n - 1);
System.out.println("Sorted array: " + Arrays.toString(arr));
}

}