编程对10个整数进行升序排序。JAVA语言
4个回答
展开全部
// 分别用JAVA冒泡算法和选择算法对整型数组进行由小到大排序,并输出
package mym3;
public class Mym3 {
public int[] SelectSort(int pArray[]) {
int temp;
for (int i = 0; i < pArray.length - 1; i++) {
for (int j = i + 1; j < pArray.length; j++) {
if (pArray[i] > pArray[j]) {
temp = pArray[i];
pArray[i] = pArray[j];
pArray[j] = temp;
}
}
}
return pArray;
}
public int[] BubbleSort(int pArray[]) {
int temp, i, j;
for (j = 0; j < pArray.length - 1; j++) {
for (i = 0; i < pArray.length - 1 - j; i++) {
if (pArray[i] > pArray[i + 1]) {
temp = pArray[i];
pArray[i] = pArray[i + 1];
pArray[i + 1] = temp;
}
}
}
return pArray;
}
public static void main(String[] args) {
Mym3 st = new Mym3();
int test[] = { 1,4,5,6,7,8,9,11,111,0 };
int length = test.length;
int i = 0, j = 0;
int testSelect[] = st.SelectSort(test);
int testBubble[] = st.BubbleSort(test);
while (i < length) {
System.out.println("Select Sort: " + testSelect[i]);
i++;
}
while (j < length) {
System.out.println("Bubble Sort: " + testBubble[j]);
j++;
}
}
package mym3;
public class Mym3 {
public int[] SelectSort(int pArray[]) {
int temp;
for (int i = 0; i < pArray.length - 1; i++) {
for (int j = i + 1; j < pArray.length; j++) {
if (pArray[i] > pArray[j]) {
temp = pArray[i];
pArray[i] = pArray[j];
pArray[j] = temp;
}
}
}
return pArray;
}
public int[] BubbleSort(int pArray[]) {
int temp, i, j;
for (j = 0; j < pArray.length - 1; j++) {
for (i = 0; i < pArray.length - 1 - j; i++) {
if (pArray[i] > pArray[i + 1]) {
temp = pArray[i];
pArray[i] = pArray[i + 1];
pArray[i + 1] = temp;
}
}
}
return pArray;
}
public static void main(String[] args) {
Mym3 st = new Mym3();
int test[] = { 1,4,5,6,7,8,9,11,111,0 };
int length = test.length;
int i = 0, j = 0;
int testSelect[] = st.SelectSort(test);
int testBubble[] = st.BubbleSort(test);
while (i < length) {
System.out.println("Select Sort: " + testSelect[i]);
i++;
}
while (j < length) {
System.out.println("Bubble Sort: " + testBubble[j]);
j++;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该有专门的sort函数,我没有学过java,自己找找吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
en
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
饰非遂过
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询