Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c00eaf

Browse files
Added Quicksort
1 parent 03bb256 commit 3c00eaf

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

‎Sorting/Quicksort/QuickSort.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.Arrays;
2+
3+
public class QuickSort {
4+
5+
public int[] quickSort(int[] arr) {
6+
if (arr.length <= 1) {
7+
return arr;
8+
}
9+
return quickSort(arr, 0, arr.length - 1);
10+
}
11+
12+
public int[] quickSort(int[] arr, int start, int end) {
13+
if (end > start) {
14+
int partition = partition(arr, start, end);
15+
if (start < partition) {
16+
quickSort(arr, start, partition);
17+
}
18+
if (partition + 1< end) {
19+
quickSort(arr, partition + 1, end);
20+
}
21+
}
22+
return arr;
23+
}
24+
25+
public int partition(int[] arr, int leftPointer, int rightPointer) {
26+
//System.out.println("Partitioning the current subarray:");
27+
//System.out.println(Arrays.toString(Arrays.copyOfRange(arr, leftPointer, rightPointer + 1)));
28+
29+
int pivot = arr[Math.floorDiv((leftPointer + rightPointer), 2)];
30+
//System.out.println("The pivot value is: " + pivot);
31+
32+
while (leftPointer < rightPointer) {
33+
while (arr[leftPointer] < pivot) {
34+
leftPointer++;
35+
//System.out.println("Incrementing left pointer to " + arr[leftPointer]);
36+
}
37+
while (arr[rightPointer] > pivot) {
38+
rightPointer--;
39+
// System.out.println("Decrementing right pointer to " + arr[rightPointer]);
40+
}
41+
if (leftPointer < rightPointer) {
42+
//System.out.println("Swapping " + arr[leftPointer] + " and " + arr[rightPointer]);
43+
Swap.swap(arr, leftPointer, rightPointer);
44+
}
45+
}
46+
return leftPointer;
47+
}
48+
49+
public static void main(String[] args) {
50+
QuickSort qs = new QuickSort();
51+
int[] unsorted = {39, 15, 24, 35, 76, 19};
52+
System.out.println("Sorting the array " + Arrays.toString(unsorted));
53+
qs.quickSort(unsorted);
54+
System.out.println("After sorting: " + Arrays.toString(unsorted));
55+
}
56+
}

‎Sorting/Quicksort/readme.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Quick Sort
2+
Quicksort is an efficient recursive algorithm for sorting arrays or lists of values. The algorithm is a comparison sort, where values are ordered by a comparison operation such as **> or <**.
3+
4+
Quicksort uses a **divide and conquer strategy**, breaking the problem into smaller sub-problems until the solution is so clear there’s nothing to solve.
5+
6+
We choose a single pivot element from the list. Every other element is compared with the pivot, which partitions the array into three groups.
7+
8+
1. A sub-array of elements smaller than the pivot.
9+
2. The pivot itself.
10+
3. A sub-array of elements greater than the pivot.
11+
12+
The process is repeated on the sub-arrays until they contain zero or one element.
13+
14+
Elements in the "smaller than" group are never compared with elements in the "greater than" group.
15+
16+
If the smaller and greater groupings are roughly equal, this cuts the problem in half with each partition step.
17+
18+
Depending on the implementation, the sub-arrays of one element each are recombined into a new array with sorted ordering, or values within the original array are swapped in-place, producing a sorted mutation of the original array.
19+
20+
## Quick Sort Runtime
21+
The key to Quicksort’s runtime efficiency is the division of the array.
22+
23+
The **worst case** occurs when we have an **_imbalanced partition_** like when the first element is continually chosen in a sorted data-set.
24+
25+
One popular strategy is to select a random element as the pivot for each step. The benefit is that no particular data set can be chosen ahead of time to make the algorithm perform poorly.
26+
27+
Another popular strategy is to take the first, middle, and last elements of the array and choose the median element as the pivot.
28+
29+
The benefit is that the division of the array tends to be more uniform.
30+
31+
Quicksort is an unusual algorithm in that the **worst case runtime is O(N^2)**, but the **average case is O(N * logN)**.
32+
33+
## Code
34+
1. We established a base case where the algorithm will complete when the start and end pointers indicate a list with one or zero elements.
35+
36+
2. If we haven’t hit the base case, we randomly selected an element as the pivot and swapped it to the end of the list
37+
38+
3. We then iterate through that list and track all the "lesser than" elements by swapping them with the iteration index and incrementing a lesser_than_pointer.
39+
40+
4. Once we’ve iterated through the list, we swap the pivot element with the element located at lesser_than_pointer.
41+
42+
5. With the list partitioned into two sub-lists, we repeat the process on both halves until base cases are met.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /