Skip to content

Commit

Permalink
Merge pull request AdarshAddee#230 from Udyansingh/main
Browse files Browse the repository at this point in the history
Added Quick Sort in java.
  • Loading branch information
AdarshAddee committed Oct 3, 2022
2 parents 8669062 + 63e959a commit 80b0c77
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
| Khushi Marothi | <a href="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/khushimarothi">Khushi Marothi</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Aditya Giri | <a href="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/aditya-464">Aditya Giri</a> | <a href="mailto:[email protected]">E-Mail</a> |
| KryPtoN | <a href="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/Kry9toN">Kry9toN</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Udyansingh | <a href="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/Udyansingh">Udyan Singh</a> | <a href="[email protected]">E-Mail</a> |
| Hardik Pratap Singh | <a href="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/hardik-pratap-singh">Hardik Pratap Singh</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Suraj Bhandarkar S | <a href="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/Suraj-Bhandarkar-S">Adarsh Addee</a> | <a href="mailto:[email protected]">E-Mail</a> |
| Ashutosh Shukla|<a href = "https://github.com/AshutoshBuilds">Ashutosh Shukla</a>|<a href ="[email protected]">E-mail</a> |
Expand Down
51 changes: 51 additions & 0 deletions Java/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//Github Username: Udyansingh

import java.util.Arrays;
import java.util.Scanner;

public class QuickSort {

public static int partition(int[] arr, int si, int ei) {
int pid, piv = arr[si], count = 0, i = si;
for (i = si + 1; i <= ei; i++) {
if (arr[i] <= piv) {
count++;
}
}
pid = count + si;
arr[si] = (arr[pid] + arr[si]) - (arr[pid] = arr[si]);
while (si < pid && ei > pid) {
if (arr[si] < piv) {
si++;
} else if (arr[ei] > piv) {
ei--;
} else {
arr[si] = (arr[ei] + arr[si]) - (arr[ei--] = arr[si++]);
}
}
return pid;
}

public static void quickSort(int[] arr, int si, int ei) {
if (si < ei) {
int p = partition(arr, si, ei);
quickSort(arr, si, p - 1);
quickSort(arr, p + 1, ei);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in).useDelimiter(",");
System.out.println("Hint: Enter Array -> 1,2,3,4,5,6 ");
System.out.print("Enter Array -> ");
String input = sc.nextLine();
String[] split = input.split(",");
int[] arr = new int[split.length];
for (int i = 0; i < split.length; i++) {
arr[i] = Integer.parseInt(split[i]);
}
quickSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array -> " + Arrays.toString(arr));
sc.close();
}
}

0 comments on commit 80b0c77

Please sign in to comment.