Skip to content

Instantly share code, notes, and snippets.

@prasadwrites
Created November 23, 2014 06:36
Show Gist options
  • Select an option

  • Save prasadwrites/82d7d39d9bc321a2b405 to your computer and use it in GitHub Desktop.

Select an option

Save prasadwrites/82d7d39d9bc321a2b405 to your computer and use it in GitHub Desktop.
Quicksort Algorithm in Java
public class QuickSortAlgo{
private void quicksort(int [] arr, int first, int last)
{
int left = first, pivot = first, right = last, temp;
if (first<last)
{
while(left<right)
{
while(arr[left] <= arr[pivot] && left < last)
left++;
while(arr[right] > arr[pivot])
right--;
if(left<right)
{
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
temp = arr[pivot];
arr[pivot] = arr[right];
arr[right] = temp;
quicksort(arr, first, right-1);
quicksort(arr, right+1, last);
}
}
public static void main(String []args){
int [] arr = { 34, 4,67,4,23,65,34,37,73,34,2,7,34};
for (int i=0;i<arr.length-1;i++)
System.out.print(" " +arr[i]);
QuickSortAlgo q = new QuickSortAlgo();
q.quicksort(arr, 0, arr.length-1);
System.out.println();
for (int i=0;i<arr.length-1;i++)
System.out.print(" " +arr[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment