@toc
f(n) = 2 × 2<sup>n-1</sup> = 2 × f(n-1)
f(1) = 2<sup>1</sup> = 2
Program
f(n)
Hanoi(n, A, B, C)
if n=1 then move(1, A, C)
else
Hanoi(n-1, A, C, B)
move(1, A, C)
move(n-1, B, A, C)
T(n) = 2T(n-1) + 1
T(1) = 1
SelectionSort(i)
if i >= n then return 0
else
k = i
for j <- i+1 to n do
if A[j] < A[k] then
k <- j
if k != i then A[i] <-> A[k]
SelectionSort(i+1)
T(n) = T(n-1) + n
T(1) = 1
Java代码实现
package sort;
import java.util.Arrays;
public class RecursiveSelectionSort {
public static void main(String[] args) {
double[] list = {3, 1, 5, 7, 2};
sort(list);
/*
for (int i = 0; i < list.length; i ++)
System.out.print(list[i]+" ");
*/
System.out.println(Arrays.toString(list));
}
public static void sort(double[] list) {
sort(list, 0, list.length-1);
}
public static void sort(double[] list, int low, int high) {
if (low < high) {
double currentMin = list[low];
int currentMinIndex = low;
for (int i = low+1; i <= high; i++) {
if (currentMin > list[i]) {
currentMin = list[i];
currentMinIndex = i;
}
}
list[currentMinIndex] = list[low];
list[low] = currentMin;
sort(list, low+1, high);
}
}
}
GeneratingPerm1()
for j <- 1 to n do
P[j] <- j
Perm1()
Perm1(m)
if m = n then output P[1...n]
else
for j <- m to n do
P[j] <-> P[m]
Perm1(m+1)
P[j] <-> P[m]
T(n) = nT(n-1) + n
GeneratingPerm2()
for j<-1 to n do
p[j]<-0
Perm2(n)
Perm2(m)
if m = 0 then ouput P[1 ... n]
else
for j<-1 to n do
if P[j]=0 then
P[j]<-m
Perm2(m-1)
P[j]<-0
案例
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有