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
运行结果都一样: