Skip to content

Commit

Permalink
Create Quick sort.java
Browse files Browse the repository at this point in the history
Quick sort in Java
  • Loading branch information
kkname776 committed Oct 18, 2020
1 parent a6154b2 commit 3d9067b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Java_Programs_for_beginners/Quick sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Quick sort in Java

import java.util.Arrays;

class QuickSort {

// Function to partition the array on the basis of pivot element
int partition(int array[], int low, int high) {

// Select the pivot element
int pivot = array[high];
int i = (low - 1);

// Put the elements smaller than pivot on the left and
// greater than pivot on the right of pivot
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return (i + 1);
}

void quickSort(int array[], int low, int high) {
if (low < high) {

// Select pivot position and put all the elements smaller
// than pivot on left and greater than pivot on right
int pi = partition(array, low, high);

// Sort the elements on the left of pivot
quickSort(array, low, pi - 1);

// Sort the elements on the right of pivot
quickSort(array, pi + 1, high);
}
}

// Driver code
public static void main(String args[]) {
int[] data = { 8, 7, 2, 1, 0, 9, 6 };
int size = data.length;
QuickSort qs = new QuickSort();
qs.quickSort(data, 0, size - 1);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}

0 comments on commit 3d9067b

Please sign in to comment.