Skip to content

Commit

Permalink
Merge pull request #81 from nikitachoudhary241/main
Browse files Browse the repository at this point in the history
Added Java Code
  • Loading branch information
prathamesh-borse authored Oct 24, 2021
2 parents c813d0a + b9063f6 commit 98ee806
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Java Codes/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Sorting;

class QuickSort {
public static void main(String[] args) {
int[] ar = { 2, 2, 5, 1, 6, 6, 9, 11, 12 };
quickSort(ar, 0, ar.length - 1);
for (int i : ar) {
System.out.print(i + " ");
}
}

static void swap(int[] ar, int i, int j) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}

static void quickSort(int[] ar, int l, int h) {
if (l < h) {
int p = partition(ar, l, h);
quickSort(ar, l, p - 1);
quickSort(ar, p + 1, h);
}
}

public static int partition(int[] ar, int l, int h) {
int pivot = ar[h];
int i = l;
for (int j = l; j <= h; j++) {
if (ar[j] < pivot) {
swap(ar, i++, j);
}
}
swap(ar, i, h);
return i;
}
}
37 changes: 37 additions & 0 deletions QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Sorting;

class QuickSort {
public static void main(String[] args) {
int[] ar = { 2, 2, 5, 1, 6, 6, 9, 11, 12 };
quickSort(ar, 0, ar.length - 1);
for (int i : ar) {
System.out.print(i + " ");
}
}

static void swap(int[] ar, int i, int j) {
int temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}

static void quickSort(int[] ar, int l, int h) {
if (l < h) {
int p = partition(ar, l, h);
quickSort(ar, l, p - 1);
quickSort(ar, p + 1, h);
}
}

public static int partition(int[] ar, int l, int h) {
int pivot = ar[h];
int i = l;
for (int j = l; j <= h; j++) {
if (ar[j] < pivot) {
swap(ar, i++, j);
}
}
swap(ar, i, h);
return i;
}
}

0 comments on commit 98ee806

Please sign in to comment.