Created
February 19, 2021 08:53
-
-
Save mahmmoudkinawy/9e455a16ecf7abae440d4ed67e94518a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//To run this fucking code just pree right click then run code or Shift + F6 | |
import java.util.Arrays; | |
public class QuickSort { | |
public void sort(int[] arr) { | |
sort(arr, 0, arr.length - 1); | |
} | |
private void sort(int[] arr, int start, int end) { | |
if (start >= end) { | |
return; | |
} | |
//Parititon | |
int boundary = partition(arr, start, end); | |
//Sort left | |
sort(arr, start, boundary - 1); | |
//Sort right | |
sort(arr, boundary + 1, end); | |
} | |
private int partition(int[] arr, int start, int end) { | |
int pivot = arr[end]; | |
int boundary = start - 1; | |
for (int i = start; i <= end; i++) { | |
if (arr[i] <= pivot) { | |
swap(arr, i, ++boundary); | |
} | |
} | |
return boundary; | |
} | |
private void swap(int[] arr, int index1, int index2) { | |
int temp = arr[index1]; | |
arr[index1] = arr[index2]; | |
arr[index2] = temp; | |
} | |
public static void main(String[] args) { | |
int[] nums = {-1, -3, -2, 0, 1, 3, 6, 9, 2, -99, 99}; | |
QuickSort sort = new QuickSort(); | |
sort.sort(nums); | |
System.out.println(Arrays.toString(nums)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment