Skip to content

Commit

Permalink
Added hoare approach quick sort
Browse files Browse the repository at this point in the history
  • Loading branch information
charandeepsinghb committed Jul 7, 2024
1 parent dc0a7a9 commit c2a22c4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data_algo/data/quick sort/src/quickSort/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Util {
* @param str Print message
*/
public static void devLog(String str) {
// System.out.println(str);
System.out.println(str);
}

/**
Expand Down
29 changes: 25 additions & 4 deletions data_algo/data/quick sort/src/quickSort/algo/Hoare.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package quickSort.algo;

import java.util.Arrays;

import quickSort.Util;

public class Hoare {

/**
Expand All @@ -10,7 +14,24 @@ public class Hoare {
* @param hi High
*/
private static int partition(Integer[] arr, int lo, int hi) {
return 0;
int p = arr[lo];

int i = lo - 1;
int j = hi;

while (true) {
Util.devLog("Arr (dev): " + Arrays.asList(arr) + ", i: " + i + ", j: " + j + ", p: " + p);
do {
i = i + 1;
} while (arr[i] < p);
do {
j = j - 1;
} while (arr[j] > p);
if (i >= j) {
return j;
}
Util.swap(arr, i, j);
}
}

/**
Expand All @@ -20,7 +41,7 @@ private static int partition(Integer[] arr, int lo, int hi) {
* @param lo Low
* @param hi High
*/
protected static void quickSort(Integer[] arr, int lo, int hi) {
protected static void sort(Integer[] arr, int lo, int hi) {

// Smallest sub
if (lo < 0 || hi < 0 || lo >= hi) {
Expand All @@ -29,7 +50,7 @@ protected static void quickSort(Integer[] arr, int lo, int hi) {

int splitPoint = partition(arr, lo, hi);

quickSort(arr, lo, splitPoint);
quickSort(arr, splitPoint + 1, hi);
sort(arr, lo, splitPoint);
sort(arr, splitPoint + 1, hi);
}
}
6 changes: 3 additions & 3 deletions data_algo/data/quick sort/src/quickSort/algo/Lomuto.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static int quickSortTweakPartition(Integer[] arr, int lo, int hi) {
* @param lo Low
* @param hi High
*/
protected static void quickSort(Integer[] arr, int lo, int hi) {
protected static void sort(Integer[] arr, int lo, int hi) {

// Smallest sub
if (lo >= hi || lo < 0) {
Expand All @@ -74,7 +74,7 @@ protected static void quickSort(Integer[] arr, int lo, int hi) {

int splitPoint = partition(arr, lo, hi);

quickSort(arr, lo, splitPoint - 1);
quickSort(arr, splitPoint + 1, hi);
sort(arr, lo, splitPoint - 1);
sort(arr, splitPoint + 1, hi);
}
}
4 changes: 2 additions & 2 deletions data_algo/data/quick sort/src/quickSort/algo/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class QuickSort {
public static void quickSort(Integer[] arr) {

// Main calling function
Lomuto.quickSort(arr, 0, arr.length - 1);
// Hoare.quickSort(arr, 0, arr.length - 1);
// Lomuto.quickSort(arr, 0, arr.length - 1);
Hoare.sort(arr, 0, arr.length - 1);
}
}

0 comments on commit c2a22c4

Please sign in to comment.