Created
April 30, 2021 11:33
-
-
Save syedjafer/2af344d596b90a68061ab405cc355b64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| public class QSort{ | |
| static void printArray(int[] arr, int size) | |
| { | |
| for(int i = 0; i < size; i++) | |
| System.out.print(arr[i] + " "); | |
| System.out.println(); | |
| } | |
| static int partition(int[] arr, int low, int high) | |
| { | |
| int pivot = arr[high]; | |
| int partition_index = low; | |
| int temp ; | |
| for(int itr = low; itr <= high - 1; itr++) | |
| { | |
| if (arr[itr] < pivot) | |
| { | |
| temp = arr[itr]; | |
| arr[itr] = arr[partition_index]; | |
| arr[partition_index] = temp; | |
| partition_index += 1; | |
| } | |
| } | |
| temp = arr[high]; | |
| arr[high] = arr[partition_index]; | |
| arr[partition_index] = temp; | |
| return partition_index; | |
| } | |
| static void qsort(int[] arr, int low, int high){ | |
| if (low < high){ | |
| int partition_index = partition(arr, low, high); | |
| qsort(arr, low, partition_index-1); | |
| qsort(arr, partition_index+1, high); | |
| } | |
| } | |
| public static void main(String []args){ | |
| int arr[] = {3, 8, 6, 2, 4, 7, 8, 5}; | |
| qsort(arr, 0, arr.length-1); | |
| printArray(arr, 8); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment