| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
 免费IT实战开发视频教程合集分享 千里之行 始于足下

问题点数：0  回复次数：4

public class QuickBest {

// postcondition: a[lo..hi] is best-case input for quicksorting that subarray
private static void best(int[] a, int lo, int hi) {

// precondition:  a[lo..hi] contains keys lo to hi, in order
for (int i = lo; i <= hi; i++)
assert a[i] == i;

if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
best(a, lo, mid-1);
best(a, mid+1, hi);
exch(a, lo, mid);
}

public static int[] best(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = i;
best(a, 0, n-1);
return a;
}

// exchange a[i] and a[j]
private static void exch(int[] a, int i, int j) {
int swap = a[i];
a[i] = a[j];
a[j] = swap;
}

public static void main(String[] args) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int n = Integer.parseInt(args[0]);  还有这行代码是什么意思？
int[] a = best(n);
for (int i = 0; i < n; i++)
// StdOut.println(a[i]);
System.out.print(alphabet.charAt(a[i]));
System.out.println();
}
}
得分:0

wechat    PrinceThumb
得分:0

得分:0

wechat    PrinceThumb
得分:0
package com.demo;

/*
* q1:
* 这段代码,需要在eclipse中运行
*/
//请问这个代码要怎么运行
public class QuickBest {
// postcondition: a[lo..hi] is best-case input for quicksorting that subarray
private static void best(int[] a, int lo, int hi) {
// precondition:  a[lo..hi] contains keys lo to hi, in order
for (int i = lo; i <= hi; i++)
assert a[i] == i;

if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
best(a, lo, mid-1);
best(a, mid+1, hi);
exch(a, lo, mid);
}

public static int[] best(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = i;
best(a, 0, n-1);
return a;
}

// exchange a[i] and a[j]
private static void exch(int[] a, int i, int j) {
int swap = a[i];
a[i] = a[j];
a[j] = swap;
}

public static void main(String[] args) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//因为args没有赋值,报数组指针越界异常了
//int n = Integer.parseInt(args[0]);  //还有这行代码是什么意思？

/*
* q2:
* 分析:
* 1.这段代码的意思明显,就是给alphabet这个字符串排序
* 2.n的取值应该是alphabet的长度,因此修改为下面这句话
*/
int n = alphabet.length();
int[] a = best(n);
for (int i = 0; i < n; i++)
// StdOut.println(a[i]);
System.out.print(alphabet.charAt(a[i]));
System.out.println();
}
}

• 5
• 1/1页
• 1