Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
71350c23
A
algorithm
项目概览
GreyZeng
/
algorithm
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
71350c23
编写于
1月 11, 2024
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
quick sort ref
上级
863a5494
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
59 addition
and
90 deletion
+59
-90
src/main/java/git/snippet/quicksort/Code_QuickSort.java
src/main/java/git/snippet/quicksort/Code_QuickSort.java
+17
-90
src/test/java/git/snippet/quicksort/Code_QuickSortTest.java
src/test/java/git/snippet/quicksort/Code_QuickSortTest.java
+42
-0
未找到文件。
src/main/java/git/snippet/quicksort/
LintCode_0464_SortIntegersII
.java
→
src/main/java/git/snippet/quicksort/
Code_QuickSort
.java
浏览文件 @
71350c23
...
...
@@ -18,25 +18,18 @@ import java.util.Stack;
// 3)把每一种情况都列出来,会有每种情况下的时间复杂度,但概率都是1/N
// 4)那么所有情况都考虑,时间复杂度就是这种概率模型下的长期期望!
// 时间复杂度O(N*logN),额外空间复杂度O(logN)都是这么来的。
public
class
LintCode_0464_SortIntegersII
{
public
static
void
swap
(
int
[]
arr
,
int
l
,
int
r
)
{
if
(
l
!=
r
)
{
arr
[
l
]
=
arr
[
l
]
^
arr
[
r
];
arr
[
r
]
=
arr
[
l
]
^
arr
[
r
];
arr
[
l
]
=
arr
[
l
]
^
arr
[
r
];
}
}
public
class
Code_QuickSort
{
// 递归方法
public
void
sortIntegers2
(
int
[]
a
)
{
public
static
void
sort1
(
int
[]
a
)
{
if
(
null
==
a
||
a
.
length
<=
1
)
{
return
;
}
quickSort
(
a
,
0
,
a
.
length
-
1
);
}
public
void
quickSort
(
int
[]
arr
,
int
l
,
int
r
)
{
// 快排算法递归解法
public
static
void
quickSort
(
int
[]
arr
,
int
l
,
int
r
)
{
if
(
l
<
r
)
{
int
p
=
l
+
(
int
)
(
Math
.
random
()
*
(
r
-
l
));
swap
(
arr
,
p
,
r
);
...
...
@@ -46,7 +39,7 @@ public class LintCode_0464_SortIntegersII {
}
}
public
int
[]
sortColors
(
int
[]
arr
,
int
l
,
int
r
)
{
public
static
int
[]
sortColors
(
int
[]
arr
,
int
l
,
int
r
)
{
int
i
=
l
;
int
p
=
arr
[
r
];
int
s
=
l
-
1
;
...
...
@@ -64,7 +57,8 @@ public class LintCode_0464_SortIntegersII {
return
new
int
[]{
s
+
1
,
e
-
1
};
}
public
void
sortIntegers21
(
int
[]
a
)
{
// 快速排序非递归版本
public
static
void
sort2
(
int
[]
a
)
{
if
(
null
==
a
||
a
.
length
<=
1
)
{
return
;
}
...
...
@@ -87,84 +81,8 @@ public class LintCode_0464_SortIntegersII {
}
}
// merge sort
public
void
sortIntegers23
(
int
[]
a
)
{
if
(
null
==
a
||
a
.
length
<=
1
)
{
return
;
}
mergeSort
(
a
,
0
,
a
.
length
-
1
);
}
public
void
mergeSort
(
int
[]
arr
,
int
l
,
int
r
)
{
if
(
l
!=
r
)
{
int
m
=
l
+
((
r
-
l
)
>>
1
);
mergeSort
(
arr
,
l
,
m
);
mergeSort
(
arr
,
m
+
1
,
r
);
merge
(
arr
,
l
,
m
,
r
);
}
}
public
void
merge
(
int
[]
arr
,
int
l
,
int
m
,
int
r
)
{
int
[]
help
=
new
int
[
r
-
l
+
1
];
int
i
=
0
;
int
ls
=
l
;
int
rs
=
m
+
1
;
while
(
ls
<=
m
&&
rs
<=
r
)
{
if
(
arr
[
ls
]
<=
arr
[
rs
])
{
help
[
i
++]
=
arr
[
ls
++];
}
else
{
help
[
i
++]
=
arr
[
rs
++];
}
}
while
(
ls
<=
m
)
{
help
[
i
++]
=
arr
[
ls
++];
}
while
(
rs
<=
r
)
{
help
[
i
++]
=
arr
[
rs
++];
}
for
(
i
=
0
;
i
<
help
.
length
;
i
++)
{
arr
[
l
+
i
]
=
help
[
i
];
}
}
// heap sort
public
void
sortIntegers24
(
int
[]
arr
)
{
if
(
null
==
arr
||
arr
.
length
<
2
)
{
return
;
}
for
(
int
i
=
arr
.
length
-
1
;
i
>=
0
;
i
--)
{
heapify
(
arr
,
i
,
arr
.
length
);
}
int
heapSize
=
arr
.
length
;
// smallest to the arr.length - 1;
swap
(
arr
,
0
,
--
heapSize
);
while
(
heapSize
>
0
)
{
heapify
(
arr
,
0
,
heapSize
);
swap
(
arr
,
0
,
--
heapSize
);
}
}
public
void
heapify
(
int
[]
arr
,
int
i
,
int
size
)
{
int
leftChildIndex
=
2
*
i
+
1
;
while
(
leftChildIndex
<
size
)
{
int
largest
=
leftChildIndex
+
1
<
size
&&
arr
[
leftChildIndex
+
1
]
>=
arr
[
leftChildIndex
]
?
leftChildIndex
+
1
:
leftChildIndex
;
largest
=
arr
[
i
]
>
arr
[
largest
]
?
i
:
largest
;
if
(
largest
==
i
)
{
break
;
}
swap
(
arr
,
largest
,
i
);
i
=
largest
;
leftChildIndex
=
2
*
i
+
1
;
}
}
// 快速排序非递归版本
public
class
Op
{
public
static
class
Op
{
public
int
l
;
public
int
r
;
...
...
@@ -173,4 +91,13 @@ public class LintCode_0464_SortIntegersII {
this
.
r
=
r
;
}
}
public
static
void
swap
(
int
[]
arr
,
int
l
,
int
r
)
{
if
(
l
!=
r
)
{
arr
[
l
]
=
arr
[
l
]
^
arr
[
r
];
arr
[
r
]
=
arr
[
l
]
^
arr
[
r
];
arr
[
l
]
=
arr
[
l
]
^
arr
[
r
];
}
}
}
src/test/java/git/snippet/quicksort/Code_QuickSortTest.java
0 → 100644
浏览文件 @
71350c23
package
git.snippet.quicksort
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
java.util.Arrays
;
import
static
git
.
snippet
.
common
.
Generator
.
copyArray
;
import
static
git
.
snippet
.
common
.
Generator
.
generateRandomArray
;
@DisplayName
(
"快速排序"
)
public
class
Code_QuickSortTest
{
@Test
@DisplayName
(
"快速排序递归解法测试"
)
public
void
mergeSortTest2
()
{
int
times
=
500000
;
// 测试的次数
int
maxSize
=
100
;
// 数组的最大长度是100
int
maxValue
=
100
;
// 数组元素的大小[-100,100]
for
(
int
i
=
0
;
i
<
times
;
i
++)
{
int
[]
arr1
=
generateRandomArray
(
maxSize
,
maxValue
);
int
[]
arr4
=
copyArray
(
arr1
);
Code_QuickSort
.
sort1
(
arr1
);
Arrays
.
sort
(
arr4
);
Assertions
.
assertArrayEquals
(
arr1
,
arr4
);
}
}
@Test
@DisplayName
(
"快速排序非递归解法测试"
)
public
void
sortTest1
()
{
int
times
=
500000
;
// 测试的次数
int
maxSize
=
100
;
// 数组的最大长度是100
int
maxValue
=
100
;
// 数组元素的大小[-100,100]
for
(
int
i
=
0
;
i
<
times
;
i
++)
{
int
[]
arr1
=
generateRandomArray
(
maxSize
,
maxValue
);
int
[]
arr4
=
copyArray
(
arr1
);
Code_QuickSort
.
sort2
(
arr1
);
Arrays
.
sort
(
arr4
);
Assertions
.
assertArrayEquals
(
arr1
,
arr4
);
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录