-
Notifications
You must be signed in to change notification settings - Fork 97
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 #16 from ayusshrathore/main
added QuickSort.java
- Loading branch information
Showing
1 changed file
with
51 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Explanation: | ||
* | ||
* In this algorithm, we start by selecting the item which is known as a pivot, | ||
* and then we re-arrange all the elements such that elements smaller than the | ||
* pivot are on the left side & elements larger than the pivot appear on the | ||
* right side of the pivot which is called partitioning, so we divide the array | ||
* into two partitions that are separated by the pivot. | ||
* | ||
* The same process is continued into the other two partitions, also in the case | ||
* of reordering the items we did not worry about the right partition so we have | ||
* narrowed down the scope of our work this is the reason why quick sort has | ||
* logarithmic complexity because at every step we almost narrow down the size | ||
* of our partition almost breaking them into half. | ||
* | ||
* Space & Time Complexity: | ||
* | ||
* Space -> O(log(n)) | ||
* | ||
* Time -> O(n log(n)) | ||
* | ||
*/ | ||
|
||
public class QuickSort { | ||
public static void sort(int[] arr) { | ||
sort(arr, 0, arr.length - 1); | ||
} | ||
|
||
private static void sort(int[] arr, int start, int end) { | ||
if (start >= end) | ||
return; | ||
int boundary = partition(arr, start, end); | ||
sort(arr, start, boundary - 1); | ||
sort(arr, boundary + 1, end); | ||
} | ||
|
||
private static int partition(int[] arr, int start, int end) { | ||
int pivot = arr[end]; | ||
int boundary = start - 1; | ||
for (int i = start; i <= end; i++) | ||
if (arr[i] <= pivot) | ||
swap(arr, i, ++boundary); | ||
return boundary; | ||
} | ||
|
||
private static void swap(int[] arr, int i, int j) { | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
} |