-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.java
More file actions
57 lines (41 loc) ยท 1.44 KB
/
Quick.java
File metadata and controls
57 lines (41 loc) ยท 1.44 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package basic.sort.impl;
import basic.sort.Sort;
/*
๊ธฐ๋ณธ ์๋ฆฌ : ํน์ pivot์ ์ค์ฌ์ผ๋ก
์ผ์ชฝ์๋ ์์ ์๋ฅผ ์ค๋ฅธ์ชฝ์ ํฐ์๋ก ์ง์ ํ๋ค.
๊ทธ๋ผ pivot์ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ, ์ค๋ฅธ์ชฝ์ ๋ Quick Sort ์ ์ฉ
๊ทธ๋ ๊ฒ ์์ฐจ์ ์ผ๋ก ๋ฐ๋ณตํด ๋ชจ๋ ์๋ฅผ ์ ๋ ฌํ๋ค.
*/
public class Quick implements Sort {
@Override
public void sort(int[] array) {
quickSort(array,0,array.length-1);
}
public void quickSort(int[] array, int start, int end){
if(start>=end) return;
//์ ๋ ฌ ํ๊ณ ์ ํ๋ ์์ ์ค ๋งจ ์์ pivotIndex๋ก ์ค์
int pivotIndex = start;
//pivot์ ์ ์ฅ.
int pivot = array[pivotIndex];
//ํด๋น ํฌ์ธํฐ์๋ pivot๋ณด๋ค ์์ ์ ๋ง ์์ด์ผํจ(๋ง์ฝ ํฌ๋ค๋ฉด swap ํ์)
int low = pivotIndex+1;
//ํด๋น ํฌ์ธํฐ์๋ pivot๋ณด๋ค ํฐ ์ ๋ง ์์ด์ผํจ(๋ง์ฝ ์๋ค๋ฉด swap ํ์)
int high = end;
while(high>=low){
if(array[low]>pivot&&array[high]<pivot){
swap(array,high,low);
}
if(array[high]>=pivot){
high--;
}
if(array[low]<=pivot){
low++;
}
}
swap(array,pivotIndex,high);
printAllArray(array,pivot);
pivotIndex =high;
quickSort(array,start,pivotIndex-1);
quickSort(array,pivotIndex+1,end);
}
}