这是我用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);
}
}
}
可是结果显示是: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);
}
}
}
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);
}
}
}可以吗?
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);
}
}
}
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楼的做法是冒泡排序,不是选择排序