-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
executable file
·41 lines (30 loc) · 1.28 KB
/
Copy pathQuickSort.java
File metadata and controls
executable file
·41 lines (30 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.codeWithArsalon.Algorithms;
public class QuickSort {
//ranges from O(n log n) to (n^2)
public void sort (int [] array){
sort(array, 0, array.length - 1);
}
private void sort(int [] array, int start, int end){
if(start >= end) // recursive stop, means single item array
return;
var boundary = partition(array, start, end); //end = index of last item in segment working with
sort(array, start, boundary - 1); //start = start of left partition, boundary - 1 = pivot
sort(array, boundary + 1, end); //right partition starts after boundary (b + 1), goes to end of array
}
private int partition (int [] array, int start, int end){
var pivot = array[end]; //set pivot as last item in segment working with
var boundary = start - 1; //initially left partition is empty
for (var i = start; i <= end; i++){
if (array[i] <= pivot){
boundary++;
swap(array, i, boundary);
}
}
return boundary; //boundary after pivot moved into place
}
private void swap (int [] array, int indexOne, int indexTwo){
var temp = array [indexOne];
array[indexOne] = array[indexTwo];
array[indexTwo] = temp;
}
}