package com.shuzu;
import java.util.Iterator;
public class ShuZu_01 {
public static void main(String[] args) {
//声明数组
int arr[];
String str[];
long ser[];
//数组名称 =new 数组元素类型[shuzuyuansugeshu]
arr =new int [10];
//数组元素类型 数组名 =new 数组元素类型[shuzuyuansugeshu]
int month[] =new int [20];
//初始化的数组 1
int arrt [] =new int []{12,34,56,78,94};
//初始化的数组 2
int arrt2[] ={5645,645,147,89};
//Eng1
//Eng2
int num [] =new int []{7,89,7455,551,1522,21559,4222,2225,5441,
7,89,7455,551,1522,21559,4222,2225,5441,2111};
for (int k= 0; k < num.length; k++) {
System.out.print(+k);
}
//Eng3
int numt[] =new int []{7,89,7455,551,1522,21559,4222,2225,5441,
7,89,7455,551,1522,21559,4222,2225,5441,2111};
for(int h=0;h<=18;h++){
System.out.print(h);
}
}
}
package com.shuzu;
import javax.rmi.CORBA.Tie;
/**
* 二位数组
* @author MZFAITHDREAM
*
*/
public class ShuZu_02 {
public static void main(String[] args) {
//二维数组初始化
Tie arrayname[][]={};
int myarr[][]={{100,254},{324,456}};
int a[] [] = new int [6] [7];
for (int i = 2; i < a.length; i++) {
for (int j = 3; j < a[i].length; j++) {
System.out.println(a[i][j]);
}
System.out.println("");
}
//Eng2
int b [][] =new int [] []{{123},{45,67},{78,98},{123,456},{456456},{789,12345},{4237,895},{785,8},{456,45623}};
for (int k = 0; k < b.length; k++) {
for (int c= 0; c < b[k].length; c++) {
System.out.println(b[k][c]);
}
System.out.println();
}
//Eng3
int arr2[][]={{4,8},{9,7},{78,123},{456,45},{45,56}};
System.out.println("数组的元素是:ok");
int d=0;
for(int x[]:arr2){
d++;
int j=0;
for(int e:x){
j++;
if(d==arr2.length&&j==x.length){
System.out.println(e);
}else{
System.out.println(e+",");
}
}
}
}
}
package ShuZu;
import java.util.Arrays;
/**
* 一维数组
* @author MZFAITHDREAM
*
*/
public class ShuZuArray {
public static void main(String[] args) {
// 创建一维数组
int arr[]=new int [] {16,17,832,1908,3102,983,4};
System.out.println(arr);
int arr1[]=new int [] {16,17,82,983,0};
System.out.println(arr1);
int day[]=new int[] {31,28,31,30,31,30,31,30,31,31,30,31,30,31,30,31};
System.out.println(day);
for (int i = 0; i < 12; i++) {
System.out.println((i+1)+"月有"+day[i]+"天有");
}
/**
* 二维数组
*/
int a[][]=new int [4][5];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
/**
* 数组的遍历
*/
int b[][]=new int[][] {{45,67},{78,95},{788,95},{45,6},{7,85},{785,4566}};
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[j].length; j++) {
System.out.print(b[i][j]);
}
System.out.println();
}
/**
* 遍历数组
*/
int arr3[][]={{34,56},{78,23},{43,12}};
System.out.println("数组元素为:");
int i=0;
for (int[] x : arr3) {
i++;
int j=0;
for (int e : x) {
j++;
if (i==arr3.length&&j==x.length) {
System.out.print(e);
} else {
System.out.print(e+",");
}
}
}
}
}
package com.shuzu;
import java.util.Arrays;
public class ShuZu_03 {
public static void main(String[] args) {
//替换元素 填充元素
//fill(int a,int value) 1
int arr [] =new int[20];
Arrays.fill(arr, 20);
for (int i = 0; i < arr.length; i++) {
System.out.println("第"+i+"元素是:"+arr[i]);
}
}
}
package com.shuzu;
import java.util.Arrays;
public class ShuZu_04 {
public static void main(String[] args) {
//替换元素 填充元素
//fill(int a,int value) 1
int arr [] =new int[]{456,45,454,5458,4784,845};
Arrays.fill(arr, 1,2,8);
for (int i = 0; i < arr.length; i++) {
System.out.println("第"+i+"元素是:"+arr[i]);
}
int ar[] =new int[]{4563,453,4543,54584,47845,8456};
Arrays.fill(ar, 1,2,8);
for (int i = 0; i < ar.length; i++) {
System.out.println("第"+i+"元素是:"+ar[i]);
}
int a[] =new int[]{45645,4532,45467,545878,478489,84598};
Arrays.fill(a, 1,2,8);
for (int i = 0; i < a.length; i++) {
System.out.println("第"+i+"元素是:"+a[i]);
}
int arrt [] =new int[]{4536,45456,457894,548958,479884,84875};
Arrays.fill(arrt, 1,2,8);
for (int i = 0; i < arrt.length; i++) {
System.out.println("第"+i+"元素是:"+arrt[i]);
}
}
}
package com.shuzu;
import java.util.Iterator;
public class ShuZu_01 {
public static void main(String[] args) {
//声明数组
int arr[];
String str[];
long ser[];
//数组名称 =new 数组元素类型[shuzuyuansugeshu]
arr =new int [10];
//数组元素类型 数组名 =new 数组元素类型[shuzuyuansugeshu]
int month[] =new int [20];
//初始化的数组 1
int arrt [] =new int []{12,34,56,78,94};
//初始化的数组 2
int arrt2[] ={5645,645,147,89};
//Eng1
//Eng2
int num [] =new int []{7,89,7455,551,1522,21559,4222,2225,5441,
7,89,7455,551,1522,21559,4222,2225,5441,2111};
for (int k= 0; k < num.length; k++) {
System.out.print(+k);
}
//Eng3
int numt[] =new int []{7,89,7455,551,1522,21559,4222,2225,5441,
7,89,7455,551,1522,21559,4222,2225,5441,2111};
for(int h=0;h<=18;h++){
System.out.print(h);
}
}
}
package com.shuzu;
import java.util.Arrays;
public class ShuZu_7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] =new int[] {4567,89,55,54,51,22,2,778,952,01,235,45,88};
int newarr[] =Arrays.copyOf(arr, 13);
for (int i = 0; i < newarr.length; i++) {
System.out.println(newarr[i]);
}
//copofrange(arr,int fromlndx,int tolndex)
int ar [] =new int[] {4567,89,55,54,51,22,2,778,952,01,235,45,88};
int newar[] =Arrays.copyOf(ar, 13);
for (int i = 0; i < newar.length; i++) {
System.out.println(newar[i]);
System.out.println("===========================");
}
//
int a [] =new int[] {4567,89,55,54,51,22,2,778,952,01,235,45,88};
int newa[] =Arrays.copyOfRange(ar,0, 4);
for (int i = 0; i < newa.length; i++) {
System.out.println(newar[i]);
System.out.println("***************************************");
//
int ia [] =new int [] {7,895642,5824,6889,7889};
Arrays.sort(ia);
int index =Arrays.binarySearch(ia, 4);
System.out.println("五的搜因:"+index);
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
String str[] =new String [] {"sjj","djd","jkxl","djkjv","kdkd","kd"};
Arrays.sort(str);
int index1 =Arrays.binarySearch(str,0,6,"sji");
}
}
}
package com.shuzu;
import java.util.Scanner;
public class ShuZu_08 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入杨3角行数ok");
Scanner sc =new Scanner (System.in);
int row =sc.nextInt();
//二数组
int arr[] [] =new int [row] [];
//遍历二数组
for(int i=0;i<row;i++){
arr[i]=new int [i+1];
//遍历一数组
for(int j=0;j<=i;j++){
if(j==0||j==i){
arr[i][j]=1;
}else {
//
arr[i] [j]=arr[i-1][j]+arr[i-1][j-1];
}
System.out.print(arr[i] [j]+"\t");
}
System.out.println();
}
}
}
第一部分java数组基本回顾
第二部分快速排序和冒泡排序图解加第代码回顾
请看以下图解
?
public void test1() {
System.out.println("快速排序");
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数");
int a=sc.nextInt();
System.out.println("请输入第二个数");
int b=sc.nextInt();
System.out.println("请输入第三个数");
int c=sc.nextInt();
System.out.println("请输入第四个数");
int d=sc.nextInt();
System.out.println("请输入第五个数");
int e=sc.nextInt();
int []arr={a,b,c,d,e};
for (int i = 0; i < arr.length -1; i++) {
int minpos=i;
for (int j = i+1; j < arr.length; j++) {
minpos =arr[j] <arr[minpos] ?j:minpos;
}
System.out.println("minpose:"+minpos);
swap(arr, i, minpos);
System.out.println("经过第"+i+"次循环之后,数据内容");
print(arr);
}
}
static void swap(int[] arr,int i,int j ) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static void print (int [] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
?
@Test
public void test2() {
System.out.println("冒泡排序");
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数");
int a=sc.nextInt();
System.out.println("请输入第二个数");
int b=sc.nextInt();
System.out.println("请输入第三个数");
int c=sc.nextInt();
System.out.println("请输入第四个数");
int d=sc.nextInt();
System.out.println("请输入第五个数");
int e=sc.nextInt();
int []array={a,b,c,d,e};
for(int i=0;i<(array.length-1);i++)
{
for(int j=i+1;j<array.length;j++)
{
if(array[i]>array[j])
{
int z=array[i];
array[i]=array[j];
array[j]=z;
}else {
}
}
}
System.out.println("从小到大:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4]);
}
static void print1 (int [] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
运行结果
?
?
今天的重点归并排序
思想:
归并排序就是递归得将原始数组递归对半分隔,直到不能再分(只剩下一个元素)后,开始从最小的数组向上归并排序
1. ?向上归并排序的时候,需要一个暂存数组用来排序,
2. ?将待合并的两个数组,从第一位开始比较,小的放到暂存数组,指针向后移,
3. ?直到一个数组空,这时,不用判断哪个数组空了,直接将两个数组剩下的元素追加到暂存数组里,
4. ?再将暂存数组排序后的元素放到原数组里,两个数组合成一个,这一趟结束。
请看图解
?
?
?理解以下流程图再交你写代码重在思想。
?
?
?
?
?
?
?
?
package ShuZu;
import java.util.Arrays;
import java.util.Scanner;
/**
* /**
* 归并排序就是递归得将原始数组递归对半分隔,
* 直到不能再分(只剩下一个元素)后,开始从最小的数组向上归并排序
1. 向上归并排序的时候,需要一个暂存数组用来排序,
2. 将待合并的两个数组,从第一位开始比较,小的放到暂存数组,指针向后移,
3. 直到一个数组空,这时,不用判断哪个数组空了,直接将两个数组剩下的元素追加到暂存数组里,
4. 再将暂存数组排序后的元素放到原数组里,两个数组合成一个,这一趟结束。
* @author MZFAITHDREAM
*
*/
public class ShuZu2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数");
int a=sc.nextInt();
System.out.println("请输入第二个数");
int b=sc.nextInt();
System.out.println("请输入第三个数");
int c=sc.nextInt();
System.out.println("请输入第四个数");
int d=sc.nextInt();
System.out.println("请输入第五个数");
int e=sc.nextInt();
int []arr={a,b,c,d,e};
merge(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
//归并
public static void merge(int[] arr,int low,int high){
int center = (high+low)/2;
if(low<high){
//递归,直到low==high, 也就是数组已不能再分了,
merge(arr,low,center);
merge(arr,center+1,high);
//当数组不能再分,开始归并排序
mergeSort(arr,low,center,high);
System.out.println(Arrays.toString(arr));
}
}
//排序
public static void mergeSort(int[] arr,int low,int center,int high){
//用于暂存排序后的数组的临时数组
int[] tempArr = new int[arr.length];
int i = low,
j = center+1;
//临时数组的下标
int index = 0;
System.out.println(index);
//循环遍历两个数组的数字,将小的插入到临时数组里
while(i<=center && j<= high){
//左边数组的数小,插入到新数组
if(arr[i]<arr[j]){
tempArr[index] = arr[i];
i++;
}else{//右边数组的数小,插入到新数组
tempArr[index] = arr[j];
j++;
}
index++;
}
//处理左半边数组多余的数据,将左半边多余的数据直接追加的临时数组的后面
while(i<=center){
tempArr[index] = arr[i];
i++;
index++;
}
//处理右半边数组多余的数据,将右半边多余的数据直接追加的临时数组的后面
while(j<= high){
tempArr[index] = arr[j];
j++;
index++;
}
//将临时数组中的数据重新放进原数组
for (int k = 0; k < index; k++) {
arr[k+low] = tempArr[k];
System.out.println(tempArr[k]);
}
}
}
运行结果
?
?每个排序都是算法的一种思想。重在理解其思想。
|