Skip to content

Commit c77f9d3

Browse files
committed
quick sort
1 parent fcc53c4 commit c77f9d3

File tree

6 files changed

+172
-2
lines changed

6 files changed

+172
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.leetcode.recursion;
2+
import java.util.Random;
3+
4+
/**
5+
* pivot : either using a random number of the array , or using a high
6+
*/
7+
public class QuickSort {
8+
9+
void quickSort(int[] arr, int low, int high) {
10+
if (low < high) {
11+
int split = partition(arr, low, high);
12+
quickSort(arr, 0, split - 1);
13+
quickSort(arr, split, high);
14+
}
15+
}
16+
17+
private static int partition(int[] arr, int low, int high) {
18+
// choose pivot
19+
int randomInt = new Random().nextInt(high-low);
20+
int pivot = arr[low + randomInt];
21+
22+
// init double point left , right
23+
int left = low - 1;
24+
int right = high + 1;
25+
26+
// ensure the all number on left be less than pivot
27+
// ensuer the all number on right be greater than pivot
28+
// if not , swap the number on the both sides
29+
while (true) {
30+
while (true) {
31+
left += 1;
32+
if (left > high || arr[left] >= pivot) {
33+
break;
34+
}
35+
}
36+
while (true) {
37+
right -= 1;
38+
if (right < low || arr[right] <= pivot) {
39+
break;
40+
}
41+
}
42+
if (left > right) {
43+
break;
44+
}
45+
// swap
46+
int tmp = arr[left];
47+
arr[left] = arr[right];
48+
arr[right] = tmp;
49+
}
50+
// return the split position.
51+
return left;
52+
}
53+
54+
public static void main(String[] args) {
55+
int[] arr = new int[]{4, 3, 7, 8, 5, 6, 1, 2, 9};
56+
new QuickSort().quickSort(arr, 0, arr.length - 1);
57+
for (int i : arr) {
58+
System.out.println(i);
59+
}
60+
}
61+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.leetcode.recursion;
2+
3+
public class QuickSortS {
4+
public static void main(String[] args) {
5+
int[] arr = {12, 4, 7, 9, 2, 5};
6+
quickSort(arr, 0, arr.length - 1);
7+
System.out.println("Sorted array:");
8+
for (int num : arr) {
9+
System.out.print(num + " ");
10+
}
11+
}
12+
13+
public static void quickSort(int[] arr, int low, int high) {
14+
if (low < high) {
15+
int pivotIndex = partition(arr, low, high);
16+
quickSort(arr, low, pivotIndex - 1);
17+
quickSort(arr, pivotIndex + 1, high);
18+
}
19+
}
20+
21+
public static int partition(int[] arr, int low, int high) {
22+
int pivot = arr[high];
23+
int i = low - 1;
24+
for (int j = low; j < high; j++) {
25+
if (arr[j] < pivot) {
26+
i++;
27+
int temp = arr[i];
28+
arr[i] = arr[j];
29+
arr[j] = temp;
30+
}
31+
}
32+
int temp = arr[i + 1];
33+
arr[i + 1] = arr[high];
34+
arr[high] = temp;
35+
return i + 1;
36+
}
37+
}

src/main/java/com/leetcode/stack/StackOne.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class StackOne {
99

1010

11+
1112
public static void main(String[] args) {
1213

1314
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.leetcode.stack;
2+
3+
4+
import java.util.Stack;
5+
6+
/**
7+
* Kth Largest Element in an Array
8+
*/
9+
public class StackTwo {
10+
11+
public static int partition(int[] nums, int low, int high) {
12+
int pivot = nums[high];
13+
int i = low - 1;
14+
for (int j = low; j < high; j++) {
15+
if (nums[j] <= pivot) {
16+
i++;
17+
int temp = nums[i];
18+
nums[i] = nums[j];
19+
nums[j] = temp;
20+
}
21+
}
22+
int temp = nums[i + 1];
23+
nums[i + 1] = nums[high];
24+
nums[high] = temp;
25+
return i + 1;
26+
}
27+
28+
public static int findKthLargest(int[] nums, int k) {
29+
Stack<int[]> stack = new Stack<>();
30+
stack.push(new int[] {0, nums.length - 1});
31+
while (!stack.isEmpty()) {
32+
int[] bounds = stack.pop();
33+
int low = bounds[0];
34+
int high = bounds[1];
35+
int pivotIndex = partition(nums, low, high);
36+
if (pivotIndex == nums.length - k) {
37+
return nums[pivotIndex];
38+
} else if (pivotIndex < nums.length - k) {
39+
stack.push(new int[] {pivotIndex + 1, high});
40+
} else {
41+
stack.push(new int[] {low, pivotIndex - 1});
42+
}
43+
}
44+
// It's assumed that k is always valid and nums has at least k elements
45+
return -1; // Should never reach here
46+
}
47+
48+
public static void main(String[] args) {
49+
50+
int[] nums = new int[]{9,8,7,6,5,4,3,2,1};
51+
int k = 2;
52+
int kthLargest = new StackTwo().findKthLargest(nums, k);
53+
System.out.println(kthLargest);
54+
}
55+
}

src/test/java/com/recursion/ArrangementTest.java renamed to src/test/java/com/leetcode/recursion/RecursionOneTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.recursion;
1+
package com.leetcode.recursion;
22

3-
import com.leetcode.recursion.RecursionOne;
43
import org.junit.jupiter.api.Assertions;
54
import org.junit.jupiter.api.Test;
65

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.leetcode.stack;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class StackTwoTest {
9+
10+
@Test
11+
public void partitionTest(){
12+
int[] nums = new int[]{1,2,3,4,5,6,7,8,9};
13+
int pivotIndex = StackTwo.partition(nums, 0, nums.length - 1);
14+
Assertions.assertEquals(pivotIndex , 8);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)