排序——基础篇
我们以1 2 9 8 7 5 6 4 3为案例将这组数从小到大排列
选择排序法
当前元素与之后所有元素进行比较,如果当前元素大于后者,则交换。
代码实现
class Choice(){
public static void main(String[] args){
int[] arr={1,2,9,8,7,6,5,4,3};//数组初始化
for(int i=0;i<arr.length;i++){
for(int j=i;j<arr.length;j++){
if(arr[i]<arr[j]){//如果前面数大于后面的就,交换两个数
a[i]=a[i]*a[j];
a[j]=a[i]/a[j];
a[i]=a[i]/a[j]; }
}
}
for(int k=0;k<arr.length;k++){
System.out.print(arr[k]+" ");
}
System.out.println();
}
}
运行结果
1 2 3 4 5 6 7 8 9
冒泡排序法
第一轮: 从第一个元素开始将前面的元素和后面的元素进行比较,如果前面的元素大于后面的就交换,比较到最后一个元素
第二轮:从第二个元素开始进行第一论同样的操作,比较到倒数第二个元素
……
代码实现
import java.util.*;
class Sort{
public static void main(String[] args){
bubbleSort();
}
public static void bubbleSort(){
int[] arr={1,2,9,8,7,6,5,4,3};
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
swap(arr,j,j+1);
}
}
}
System.out.println(Array.toString(arr));
}
public static void swap(int[] arr,int i,int j){
arr[i]=arr[i]+arr[j];
arr[j]=arr[i]-arr[j];
arr[i]=arr[i]-arr[j];
}
}
运行结果
1 2 3 4 5 6 7 8 9
插入排序法
插入排序法是将数字和他左边的数进行比较,如果左边的数大就交换位置
代码实现
import java.util.*;
class Sort{
public static void main(String[] args){
inserSort();
}
public static void inserSort(){
int[] arr={1,2,9,8,7,6,5,4,3};
int e;
int j;
for(int i=1;i<arr.length;i++){
e=arr[i];
for(j=i;j>0&&arr[j-1]>e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
System.out.println(Array.toString(arr));
}
public static void swap(int[] arr,int i,int j){
arr[i]=arr[i]+arr[j];
arr[j]=arr[i]-arr[j];
arr[i]=arr[i]-arr[j];
}
}
运行结果
1 2 3 4 5 6 7 8 9
计数排序法
将数组中的数按照下标与元素关系存入新数组中
代码实现
import java.util.*;
class Sort{
public static void main(String[] args){
countSort();
}
public static void countSort(){
int[] arr={1,2,9,8,7,6,5,4,3,-10,-8};
int min=arr[0];
int max=arr[0];
for(int i=0;i<arr.length;i++){//O(n)
if(arr[i]>max){
max=arr[i];
}
if(arr[i]<min){
min=arr[i];
}
}
int[] nums=new int[max-min+1];
int offset=min;
for(int i=0;i<arr.length;i++){//O(n)
nums[arr[i]-offset]++;
}
int index=0;
for(int i=0;i<nums.length;i++){//O(m)
if(nums[i]!=0){
for(int j=0;j<nums[i];j++){
arr[index++]=i+offset;
}
}
}
System.out.println(Array.toString(arr));
}
public static void swap(int[] arr,int i,int j){
arr[i]=arr[i]+arr[j];
arr[j]=arr[i]-arr[j];
arr[i]=arr[i]-arr[j];
}
}
运行结果
-10 -6 1 2 3 4 5 6 7 8 9
习题
1、
/*
数据:用户输入的列表长度 用户输入的列表
指令:提示用户输入列表长度 提示用户输入列表 调用函数isConsecutiveFour()并输出列表是否有四个连续的数
步骤:
1、提示用户输入一个列表的长度
2、提示用户输入列表
3、调用函数并输出
*/
import java.util.Scanner;
class ProblemSix{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1、提示用户输入列表长度
System.out.print("Enter the number of values:");
int length=scanner.nextInt();
//2、提示用户输入列表
System.out.print("Enter the values:");
int[] values=new int[length];
for(int i=0;i<length;i++){
values[i]=scanner.nextInt();
}
//3、调用函数isConsecutiveFour(),并输出列表是否有四个连续的数
if(isConsecutiveFour(values)){
System.out.println("The list has consecutive four");
}else{
System.out.println("The list has no consecutive four");
}
}
public static boolean isConsecutiveFour(int[] values){
int count=1;
for(int i=1;i<values.length;i++){
if(values[i]==values[i-1]){
count+=1;
}else{
count=1;
}
if(count==4){
return true;
}
}
return false;
}
}
运行结果
Enter the number of values:9
Enter the values:1 2 3 4 5 6 7 8 9
The list has no consecutive four
Enter the number of values:9
Enter the values:1 2 3 3 3 3 4 5 6
The list has consecutive four
2、
/*
数据:用户输入的两个列表的长度和内容
指令:提示用户输入两个有序列表长度和内容 调用函数merge() 输出合并之后的列表
步骤:
1、提示用户输入两个列表长度和内容
2、调用函数merge()得到合并后的列表
3、输出合并之后的列表
*/
import java.util.*;
class ProblemSeven{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1、提示用户数如两个有序列表
System.out.print("Enter list1(第一个代表列表长度): ");
int length1=scanner.nextInt();
int[] list1=new int[length1];
for(int i=0;i<length1;i++){
list1[i]=scanner.nextInt();
}
System.out.print("Enter list2: ");
int length2=scanner.nextInt();
int[] list2=new int[length2];
for(int j=0;j<length2;j++){
list2[j]=scanner.nextInt();
}
//2、调用函数merge
//3、输出合并后的数组
System.out.print("The merged list is "+Arrays.toString(merge(list1,list2)));
}
public static int[] merge(int[] list1,int[] list2){
if(list1==null&&list2==null){
return null;
}
if(list1==null){
return list2;
}
if(list2==null){
return list1;
}
int[] list3=new int[list1.length+list2.length];
int maxlength;
int i=0;
int j=0;
int k=0;
while(true){
if(list1[i]<=list2[j]){
list3[k]=list1[i];
i++;
}else{
list3[k]=list2[j];
j++;
}
k++;
if(i==list1.length||j==list2.length){
break;
}
}
if(list1.length==i&&list2.length==j){
return list3;
}else if(list1.length==i){
for(j=j;j<list2.length;j++){
list3[k]=list2[j];
k++;
}
}else{
for(i=i;i<list1.length;i++){
list3[k]=list1[i];
k++;
}
}
return list3;
}
}
运行结果
Enter list1(第一个代表列表长度): 5 1 2 3 4 5
Enter list2: 6 1 2 4 6 7 8
The merged list is [1, 1, 2, 2, 3, 4, 4, 5, 6, 7, 8]
3、
import java.util.*;
class ProblemEight{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1、创建一个字符串数组,存5个单词
String[] word={"program","airplane","apple","monkey","person"};
//2、随机产生一个数,表示第几个单词
int number=(int)(Math.random()*word.length);
//3、调用函数猜第一次词
guess(word[number]);
//4、通过循环确定是否继续猜词
while(true){
System.out.print("Do you want to guess other words? Enter y or n: ");
String choice=scanner.nextLine();
if(choice.charAt(0)=='y'){
guess(word[(int)(Math.random()*word.length)]);
}else{
break;
}
}
}
public static void guess(String word){
Scanner scanner=new Scanner(System.in);
char[] cipher=new char[word.length()];
int miss=0;
for(int i=0;i<word.length();i++){//初始化数组为*
cipher[i]='*';
}
while(true){
int number=word.length();
System.out.print("(Guess) Enter a letter in word: ");
show(cipher);
System.out.print(" > ");
char letter=scanner.nextLine().charAt(0);
for(int j=0;j<word.length();j++){//遍历单词看是单词中否有用户输入的字母
if(letter==word.charAt(j)&&letter!=cipher[j]){
cipher[j]=letter;
}else if(letter==word.charAt(j)&&letter==cipher[j]){
break;
}else{
number--;
}
if(number==0){
System.out.println(letter+" is not in word ");
miss++;
break;
}
}
int k;
for(k=0;k<word.length();k++){
if(cipher[k]!=word.charAt(k)){
break;
}
}
if(k==word.length()){
System.out.println("The word is "+word+" .You x=missed it "+miss+" time");
return;
}
}
}
public static void show(char[] cipher){//展示数组
for(int i=0;i<cipher.length;i++){
System.out.print(cipher[i]);
}
}
}
运行结果
(Guess) Enter a letter in word: ****** > q
q is not in word
(Guess) Enter a letter in word: ****** > p
p is not in word
(Guess) Enter a letter in word: ****** > a
a is not in word
(Guess) Enter a letter in word: ****** > m
(Guess) Enter a letter in word: m***** > o
(Guess) Enter a letter in word: mo**** > o
o is already in the word
(Guess) Enter a letter in word: mo**** > l
l is not in word
(Guess) Enter a letter in word: mo**** > k
(Guess) Enter a letter in word: mo*k** > e
(Guess) Enter a letter in word: mo*ke* > y
(Guess) Enter a letter in word: mo*key > n
The word is monkey .You x=missed it 4 time
Do you want to guess other words? Enter y or n: n
(Guess) Enter a letter in word: ****** > m
(Guess) Enter a letter in word: m***** > o
(Guess) Enter a letter in word: mo**** > n
(Guess) Enter a letter in word: mon*** > k
(Guess) Enter a letter in word: monk** > r
r is not in word
(Guess) Enter a letter in word: monk** > e
(Guess) Enter a letter in word: monke* > y
The word is monkey .You x=missed it 1 time
Do you want to guess other words? Enter y or n: y
(Guess) Enter a letter in word: ***** > h
h is not in word
(Guess) Enter a letter in word: ***** > d
d is not in word
(Guess) Enter a letter in word: ***** > f
f is not in word
(Guess) Enter a letter in word: ***** > a
(Guess) Enter a letter in word: a**** > p
(Guess) Enter a letter in word: app** > r
r is not in word
(Guess) Enter a letter in word: app** > l
(Guess) Enter a letter in word: appl* > e
The word is apple .You x=missed it 4 time
Do you want to guess other words? Enter y or n: n
4、
import java.util.*;
class ProblemNine{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
//1、定义一个矩阵,二维数组
double[][] column=new double[3][4];
//提示用户输入有一个数组
System.out.println("Enter a 3-by-4 matrix row by row: ");
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
column[i][j]=scanner.nextDouble();
}
}
//调用函数返回每列的和,并输出
for(int k=0;k<4;k++){
System.out.println("Sum of elements at column "+k+" is "+sumColumn(column,k));
}
}
public static double sumColumn(double[][] column,int columnIndex){
double sum=0;
for(int i=0;i<column.length;i++){
sum+=column[i][columnIndex];
}
return sum;
}
}
运行结果
Enter a 3-by-4 matrix row by row:
1 2 3 4
4 6 8 9
2 3 6 7
Sum of elements at column 0 is 7.0
Sum of elements at column 1 is 11.0
Sum of elements at column 2 is 17.0
Sum of elements at column 3 is 20.0