ไธ.ๆๅบ็ฎๆณ๐
1๏ธโฃ. ๅฟซ้ๆๅบ โก
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
class Solution {
//ๅฟซ้ๆๅบ
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort(int[] nums,int l,int r){
if(l<r){
int pos = random_par(nums,l,r);
quickSort(nums,l,pos-1);
quickSort(nums,pos,r);
}
}
public int random_par(int[] nums,int l,int r){
int t = new Random().nextInt(r-l+1)+l;
swap(nums,t,r);
return par(nums,l,r);
}
public int par(int[] nums,int l,int r){
int pv = nums[r];
int i = l-1;
for(int j=l;j<r;++j){
if(nums[j]<=pv){
i++;
swap(nums,i,j);
}
}
swap(nums,i+1,r);
return (i+1);
}
public void swap(int[] nums,int l,int r){
int t = nums[l];
nums[l]=nums[r];
nums[r]=t;
}
}
2๏ธโฃ. ๅ ๆๅบ๐
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
58
class Solution {
//ๅ ๆๅบ
public int[] sortArray(int[] nums) {
heapSort(nums);
return nums;
}
public void heapSort(int[] nums){
int len = nums.length-1;
buildHeap(nums, len);
for (int i = len; i >= 1; --i) {
swap(nums, i, 0);
len -= 1;
rebuildHeap(nums, 0, len);
}
}
public void buildHeap(int[] nums,int len){
for (int i = len / 2; i >= 0; --i) {
rebuildHeap(nums, i, len);
}
}
public void rebuildHeap(int[] nums,int i,int len){
for (; (i << 1) + 1 <= len;) {
int lson = (i << 1) + 1;
int rson = (i << 1) + 2;
int large;
if (lson <= len && nums[lson] > nums[i]) {
large = lson;
} else {
large = i;
}
if (rson <= len && nums[rson] > nums[large]) {
large = rson;
}
if (large != i) {
swap(nums, i, large);
i = large;
} else {
break;
}
}
}
public void swap(int[]nums,int l,int r){
int t = nums[l];
nums[l]=nums[r];
nums[r]=t;
}
}
3๏ธโฃ. ๅฝๅนถๆๅบ๐ฆ
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
class Solution {
int[] temp;
public int[] sortArray(int[] nums) {
temp = new int[nums.length];
mergeSort(nums,0,nums.length-1);
return nums;
}
public void mergeSort(int[]nums,int l,int r){
if(l<r){
int mid = l+r>>1;
mergeSort(nums,l,mid);
mergeSort(nums,mid+1,r);
int count=0;
int i=l,j=mid+1;
while(i<=mid&&j<=r){
if(nums[i]<=nums[j]){
temp[count++]=nums[i++];
}else{
temp[count++]=nums[j++];
}
}
while(i<=mid){
temp[count++]=nums[i++];
}
while(j<=r){
temp[count++]=nums[j++];
}
for (int k = 0; k < r - l + 1; ++k) {
nums[k + l] = temp[k];
}
}
}
}
4๏ธโฃ. ๐ฅ
5๏ธโฃ. โ
6๏ธโฃ. โ
1
2
3
4
5
6
7
8
9
10
11
12
1๏ธโฃ2๏ธโฃ3๏ธโฃ4๏ธโฃ5๏ธโฃ6๏ธโฃ7๏ธโฃ8๏ธโฃ9๏ธโฃ๐
โซโช๐ค๐ฃ๐ต๐ก๐ด๐ ๐ข
๐งก๐โค๐๐๐๐ค๐ค๐ค๐
๐๐๐๐๐๐๐๐๐๐ข
๐ฅ๐ค๐ฆ๐จ๐ซ๐ถ๐๐๐๐
๐ด๐ต๐ฒ๐ผ๐
ฐ๐
ฑ๐๐๐
พ๐
โ๐๐โโญโ๐ฏ๐
๐#๏ธโฃ*๏ธโฃ
๐ถ๐ธโพโฝโฌ๐ท๐น๐ญ๐ฌ๐๐๐ฅ
๐งโกโญ๐๐๐๐ธ๐๐ ๐๐
๐๐๐ฅ๐ฅ๐ฅ๐
๐๐ฎ๐๐๐น
๐ต๐ถ๐ฐ๐๐๐๐ค
โ
โก
โข
โฃ
โค
######