forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AdarshAddee#230 from Udyansingh/main
Added Quick Sort in java.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |