java语言程序设计与数据结构7.4

博客内容展示了如何使用Java实现数组的联动排序,包括冒泡排序和选择排序两种方式。程序读取用户输入的学生姓名和分数,进行升序排序,并检查列表是否已排序。此外,还提供了选择排序的两种不同实现,一种是找到最小值并交换到前面,另一种是找到最大值并交换到后面。
摘要由CSDN通过智能技术生成

java语言程序设计与数据结构7.4

在这里插入图片描述

在这里插入图片描述
如何让两个数组联动起来?
试试排一个的顺序的同时排另外一个。

package four;

import java.util.Scanner;

public class T7_17 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the numbers of student: ");
		int n=input.nextInt();
		
		String[] names=new String[n];
		System.out.print("Enter the names of student: ");
		for(int i=0;i<n;i++)
			names[i]=input.next();
		
		int[] scores=new int[n];
		System.out.print("Enter the scores of student: ");
		for(int i=0;i<n;i++) 
			scores[i]=input.nextInt();
		
		for(int i=0;i<n;i++)
			for(int j=0;j<n-1-i;j++)
				if(scores[j]<scores[j+1]) {
					int temp=scores[j];
					scores[j]=scores[j+1];
					scores[j+1]=temp;
					String temp1=names[j];
					names[j]=names[j+1];
					names[j+1]=temp1;
				}
		
		for(int i=0;i<n;i++) 
			System.out.print(names[i]+" ");
	}

}

在这里插入图片描述
18.
在这里插入图片描述

package four;

import java.util.Scanner;

public class T7_18 {
	public static void main(String[] arg) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the length of array n: ");
        int n=input.nextInt();
		int[] array=new int[n];
		System.out.print("Enter n numbers: ");
		for(int i=0;i<array.length;i++)
			array[i]=input.nextInt();
		bubbleSort(array);
		System.out.print("After bubbleSort: ");
		for(int i=0;i<array.length;i++)
			System.out.print(array[i]+" ");
	}
	public static void bubbleSort(int[] a) {
		for(int i=0;i<a.length-1;i++)
			for(int j=0;j<a.length-i-1;j++) 
				if(a[j]>a[j+1]) {
					int temp;
					temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
	}
}

在这里插入图片描述

在这里插入图片描述
初步想法:如果从前往后两个两个比较,有是list[i]>list[i+1]的,则return false,如果没有,则顺利通过重重大山的阻隔,冲向最后的一句return true。

package four;

import java.util.Scanner;

public class T7_19 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the size of the list: ");
        int n=input.nextInt();
		int[] list=new int[n];
		System.out.print("Enter the contents of the list: ");
		for(int i=0;i<list.length;i++)
			list[i]=input.nextInt();
		System.out.print("The list has "+n+" integers ");
		for(int i=0;i<list.length;i++)
			if(i==list.length-1)
				System.out.println(list[i]);
			else
				System.out.print(list[i]+" ");
		if(isSorted(list))
			System.out.println("The list has already sorted.");
		else
			System.out.println("The list is not sorted.");
	}
	
	//是否为升序
	public static boolean isSorted(int[ ] list) {
		for(int i=0;i<list.length-1;i++) 
			if(list[i]>list[i+1])
				return false;
		return true;
	}
}

在这里插入图片描述
在这里插入图片描述
20.
在这里插入图片描述
用选择排序进行升序排序。
选择排序有两种写法,一种是重复找最小值,一种是重复找最大值:
这种是重复找最小值的:

package four;

import java.util.Scanner;

public class TestSelectionSort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the size of the list: ");
        int n=input.nextInt();
		double[] list=new double[n];
		System.out.print("Enter the contents of the list: ");
		for(int i=0;i<list.length;i++)
			list[i]=input.nextDouble();
		
		selectionSort(list);
		
		System.out.print("After selectionSort: ");
		for(int i=0;i<list.length;i++)
			System.out.print(list[i]+" ");
	}
	//重复地在当前数组中找到最小值,然后将这个最小值与该数组中的第一位进行交换。
	public static void selectionSort(double[] list) {
		for(int i=0;i<list.length;i++) {
			double currentMin=list[i];
			int currentMinIndex=i;
			for(int j=i+1;j<list.length;j++) {
				if(currentMin>list[j]) {
					currentMin=list[j];
					currentMinIndex=j;
				}
			}
			
			if(currentMinIndex!=i) {
				list[currentMinIndex]=list[i];
				list[i]=currentMin;
			}
		}
	}

}

//1 9 4.5 6.6 5.7 -4.5

这种是重复找最大值的:

package four;

import java.util.Scanner;

public class T7_20 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the size of the list: ");
        int n=input.nextInt();
		double[] list=new double[n];
		System.out.print("Enter the contents of the list: ");
		for(int i=0;i<list.length;i++)
			list[i]=input.nextDouble();
		
		selectionSort0(list);
		
		System.out.print("After selectionSort: ");
		for(int i=0;i<list.length;i++)
			System.out.print(list[i]+" ");
	}
	//重复地在当前数组中找到最大值,然后将这个最大值与该数组中的最后一位进行交换。
	public static void selectionSort0(double[] list) {
		for(int i=list.length-1;i>0;i--) {
			double currentMax=list[i];
			int currentMaxIndex=i;
			for(int j=i-1;j>=0;j--) {
				if(currentMax<list[j]) {
					currentMax=list[j];
					currentMaxIndex=j;
				}
			}
			
			if(currentMaxIndex!=i) {
				list[currentMaxIndex]=list[i];
				list[i]=currentMax;
			}
		}
	}

}

//1 9 4.5 6.6 5.7 -4.5

运行结果都一样:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

emmaing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值