java实现选择排序 的一个问题 请教大家

evilCow 2008-02-22 01:40:45
这是我用java实现选择排序写的代码
可是结果显示是:4 13 27 38 49 49 55 65 97 76

想请教一下 为什么76不能被排序?
这段程序怎么修改 就可以实现完全的排序?
谢谢各位了


class SelectSort {

void selectSort(int[] sortIn) {
int i, j, temp,min=0;
for(i=0; i<sortIn.length; i++) {
for(j=i; j<sortIn.length-1; j++) {

if(sortIn[j] > sortIn[j+1]) {
//min = j+1;
if(sortIn[min] > sortIn[j+1]) {
min = j+1;
}
}
}
temp = sortIn[i];
sortIn[i] = sortIn[min];
sortIn[min] = temp;
}
}

public static void main(String[] args) {
int[] sort = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
SelectSort ss = new SelectSort();
ss.selectSort(sort);
for(int num : sort) {
System.out.print(" " +num);
}
}
}
...全文
237 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
evilCow 2008-02-23
  • 打赏
  • 举报
回复
感谢楼上各位 问题已经解决
Johnson_Hong 2008-02-22
  • 打赏
  • 举报
回复
你掉了一句
void selectSort(int[] sortIn) {
int i, j, temp,min=0;
for(i=0; i<sortIn.length; i++) {
min = i;
for(j=i; j<sortIn.length-1; j++) {

if(sortIn[j] > sortIn[j+1]) {
//min = j+1;
if(sortIn[min] > sortIn[j+1]) {
min = j+1;
}
}
}
temp = sortIn[i];
sortIn[i] = sortIn[min];
sortIn[min] = temp;
}
}
另外3楼的做法是冒泡排序,不是选择排序
sldarkangel 2008-02-22
  • 打赏
  • 举报
回复
public class Test {
void selectSort(int[] sortIn) {
int tmp = 0;
for (int i = 0; i < sortIn.length; i++) {//运行次数是传入的数组的长度
for (int j = 0; j < sortIn.length - 1; j++) {//j的值与i无关
if (sortIn[j] > sortIn[j + 1]) {
tmp = sortIn[j];
sortIn[j] = sortIn[j + 1];
sortIn[j + 1] = tmp;
}
}
}
}

public static void main(String[] args) {
int[] sort = { 49, 38, 65, 97, 76, 13, 27, 49, 55, 4 };
Test ss = new Test();
ss.selectSort(sort);
for (int num : sort) {
System.out.print(" " + num);
}
}
}
OhMyGod0512 2008-02-22
  • 打赏
  • 举报
回复
package csdn;

class SelectSort {
int term;
void selectSort(int[] sortIn) {
for(int i=0;i<sortIn.length;i++){
for(int j=i+1;j<sortIn.length;j++){
if(sortIn[i]>sortIn[j]){
term=sortIn[i];
sortIn[i]=sortIn[j];
sortIn[j]=term;
}
}
}
}

public static void main(String[] args) {
int[] sort = {49, 38, 65, 97, 76, 13, 27, 49, 55, 4};
SelectSort ss = new SelectSort();
ss.selectSort(sort);
for(int num : sort) {
System.out.print(" " +num);
}
}
}

可以吗?
healer_kx 2008-02-22
  • 打赏
  • 举报
回复
肯定是简单的逻辑错误,发生在边缘地带的小错误。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧