本文目录一览:
Java编程:简化的插入排序?
代码:(如果输入必须是有序数组,不然方法内要先排序) 结果:
java 数组插入排序改错 急
修改 Score
类声明,实现 java.lang.Comparable
接口;
public class Score implements java.lang.Comparable {
在方法 getStuInfomation()
后追加两个方法(compareTo
为 Comparable
接口要求实现此方法,toString
为重写基类的打印输出方法):
public int compareTo(Object otherScore) {
return this.stuScore - ((Score) otherScore).stuScore;
}
public String toString() {
return "[学号:" + stuNumber + "分数:" + stuScore + "姓名:" + stuName + "]";
}
重新编译 Score.java
,再运行 Test
,输出结果为:
----------
[学号:3分数:323姓名:Lily]
[学号:5分数:332姓名:Mike]
[学号:2分数:445姓名:Fangfang]
[学号:1分数:472姓名:Liming]
[学号:4分数:540姓名:Green]
--2008/04/05
哎......
package method2;
import java.util.ArrayList;
import java.util.Collections;
public class Test {
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add(new Score(1, "Liming", 472));
list.add(new Score(2, "Fangfang", 445));
list.add(new Score(3, "Lily", 323));
list.add(new Score(4, "Green", 540));
list.add(new Score(5, "Mike", 332));
Collections.sort(list, new StuScoreComparator());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
Collections.binarySearch(list, new Score(1, "Liming", 472));
}
}
class StuScoreComparator extends Score implements java.util.Comparator {
public int compare(Object o1, Object o2) {
Score score1 = (Score) o1;
Score score2 = (Score) o2;
return score1.getStuScore() - score2.getStuScore();
}
}
package method2;
/**
*
* @author Liuzhichao
*/
public class Score implements java.lang.Comparable {
private int stuNumber;
private String stuName;
private int stuScore;
Score() {
}
public Score(int stuNumber, String stuName, int stuScore) {
if (stuNumber > 0) {
this.stuNumber = stuNumber;
} else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
if (stuScore > 0) {
this.stuScore = stuScore;
} else {
System.out.println("输入的学生学号不正确");
System.exit(1);
}
this.stuName = stuName;
}
// 设置学生信息
public void setStuNumber(int stuNumber) {
this.stuNumber = stuNumber;
}
// 设置学生学号
public void setStuScore(int stuScore) {
this.stuScore = stuScore;
}
// 设置学生分数
public void setStuName(String stuName) {
this.stuName = stuName;
}
// 获取学生学号
public int getStuNumber() {
return this.stuNumber;
}
// 获取学生分数
public int getStuScore() {
return this.stuScore;
}
// 获取学生名字
public String getStuName() {
return this.stuName;
}
// 获取学生信息
public String getStuInfomation() {
return "名字:" + this.stuName + " 学号:" + this.stuNumber + " 成绩:" + this.stuScore;
}
public int compareTo(Object otherScore) {
return this.stuScore - ((Score) otherScore).stuScore;
}
public String toString() {
return "[学号:" + stuNumber + "分数:" + stuScore + "姓名:" + stuName + "]";
}
}
JAVA中有哪几种常用的排序方法?
最主要的是冒泡排序、选择排序、插入排序以及快速排序。
1、冒泡排序
冒泡排序是一个比较简单的排序方法。在待排序的数列基本有序的情况下排序速度较快。若要排序的数有 n 个,则需要 n-1 轮排序,第 j 轮排序中,从第一个数开始,相邻两数比较,若不符合所要求的顺序,则交换两者的位置;直到第 n+1-j 个数为止,第一个数与第二个数比较,第二个数与第三个数比较,......,第 n-j 个与第 n+1-j 个比较,共比较 n-1 次。此时第 n+1-j 个位置上的数已经按要求排好,所以不参加以后的比较和交换操作。 例如:第一轮排序:第一个数与第二个数进行比较,若不符合要求的顺序,则交换两者的位置,否则继续进行二个数与第三个数比较......。直到完成第 n-1 个数与第 n 个数的比较。此时第 n 个位置上的数已经按要求排好,它不参与以后的比较和交换操作;第二轮排序:第一个数与第二个数进行比较,......直到完成第 n-2 个数与第 n-1 个数的比较;......第 n-1 轮排序:第一个数与第二个数进行比较,若符合所要求的顺序,则结束冒泡法排序;若不符合要求的顺序,则交换两者的位置,然后结束冒泡法排序。 共 n-1 轮排序处理,第 j 轮进行 n-j 次比较和至多 n-j 次交换。 从以上排序过程可以看出,较大的数像气泡一样向上冒,而较小的数往下沉,故称冒泡法。
public void bubbleSort(int a[]) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
2、选择排序
选择法的原理是先将第一个数与后面的每一个数依次比较,不断将小的赋给第一个数,从而找出最小的。然后第二个数与后面的每一个数依次比较,从而找出第二小的,然后第三个数与后面的每一个数依次比较,从而找出第三小的,直到找到最后一个数。
public void sort(int x[]) {
int n = x.length;
int k, t;
for (int i = 0; i < n - 1; i++) {
k = i;
for (int j = i + 1; j <= n; j++) {
if (x[j] < x[k]) k = j;
if (k != i) {
t = x[i];
x[i] = x[k];
x[k] = t;
}
}
}
}
3、插入排序
插入排序的原理是对数组中的第 i 个元素,认为它前面的 i-1 个已经排序好,然后将它插入到前面的 i-1 个元素中。插入排序对少量元素的排序较为有效。
public void sort(int obj[]) {
for (int j = 1; j < obj.length; j++) {
int key = obj[j];
int i = j - 1;
while (i >= 0 && obj[i] > key) {
obj[i + 1] = obj[i];
i--;
}
obj[i + 1] = key;
}
}
4、快速排序
快速排序是对冒泡排序的一种改进。它的基本思想是:通过一次排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
public void quickSort(int obj[], int low, int high) {
int i = low;
int j = high;
int keyValue = obj[i];
while (i < j) {
int temp = 0;
while (i < j && obj[j] >= keyValue) {
j = j - 1;
}
temp = obj[j];
obj[j] = obj[i];
obj[i] = temp;
while (i < j && obj[i] <= keyValue) {
i = i + 1;
}
temp = obj[j];
obj[j] = obj[i];
obj[i] = temp;
}
obj[i] = keyValue;
if (low < i - 1) {
quickSort(obj, low, i - 1);
}
if (high > i + 1) {
quickSort(obj, i + 1, high);
}
}