用java写出冒泡排序和选择排序还有算法思想!急急急急急急啊
发布网友
发布时间:2022-05-12 04:00
我来回答
共2个回答
热心网友
时间:2023-11-09 04:46
算法可以写一下,至于‘算法思想’,那完全就是口水,绝对不写。追问用选择排序和冒泡排序对上面数组进行升序排列啊,刚刚的题目就不要看了
追答public class SuanFa {
public static void main(String[] args) {
int[] data1=new int[]{4,3,8,1,6,5,7,2};
int[] data2=new int[]{4,3,8,1,6,5,7,2};
xuanZe(data1);
for (int i = 0; i arr[j]?arr[i]:arr[j];
}
}
return arr;
}
}
热心网友
时间:2023-11-09 04:47
楼主,程序如下:
冒泡排序:
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int[] a = new int[8];
Scanner scanner = new Scanner(System.in);
int i = 0;
int sum = 0;
while(i < a.length)
{
System.out.print("请输入数字:");
a[i] = scanner.nextInt();
i++;
}
bubbleSort(a);
for(int num : a)
{
sum += num;
System.out.print(num + "\t");
}
System.out.println();
System.out.println("最大值:" + a[7]);
System.out.println("数组之和:" + sum);
}
public static void bubbleSort(int[] a)
{
boolean flag = true;
while(flag)
{
flag = false;
for(int i=0;i<a.length-1;i++)
{
if(a[i] > a[i+1])
{
int temp = 0;
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
flag = true;
}
}
}
选择排序:
import java.util.Scanner;
public class SelectSort {
public static void main(String[] args) {
int[] a = new int[8];
Scanner scanner = new Scanner(System.in);
int i = 0;
int sum = 0;
while(i < a.length)
{
System.out.print("请输入数字:");
a[i] = scanner.nextInt();
i++;
}
selectSort(a);
for(int num : a)
{
sum += num;
System.out.print(num + "\t");
}
System.out.println();
System.out.println("最大值:" + a[7]);
System.out.println("数组之和:" + sum);
}
public static void selectSort(int[] a)
{
for(int i=0;i<a.length;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i] > a[j])
{
int temp = 0;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
请采纳!加分吧!