Skip to content

Instantly share code, notes, and snippets.

@syedjafer
Created April 30, 2021 11:33
Show Gist options
  • Select an option

  • Save syedjafer/2af344d596b90a68061ab405cc355b64 to your computer and use it in GitHub Desktop.

Select an option

Save syedjafer/2af344d596b90a68061ab405cc355b64 to your computer and use it in GitHub Desktop.
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