2020软件工程04

作业课程 https://edu.cnblogs.com/campus/zswxy/2018SE
作业要求 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
作业目标 排序算法和二叉树学习
学号 <20189639>

第一题 排序算法
解题思路:首先仔细阅读题目,选择使用自己最熟悉的java语言进行实现。选择快速排序算法。首先给定需要的序列,设置长度,再排序。主要是通道数组和for循环的遍历来实现。

代码及实现结果
import java.util.Scanner;

public class Homework {
private static int[] arrwork;
private static int[][] workout;
private static int[] arrCopy;
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("输入正确的数据格式");
arrwork=new int[sc.nextInt()];
sc.nextLine();
for(int i=0;i<arrwork.length;i++){
arrwork[i]=sc.nextInt();
}
sc.nextLine();
workout=new int[sc.nextInt()][3];
for(int i=0;i<workout.length;i++){
if(i==(workout.length-1)){
}else{
sc.nextLine();
}
for(int j=0;j<3;j++){
workout[i][j]=sc.nextInt();
}
if(workout[i][2]>(workout[i][1]-workout[i][0]+1)){
System.out.println("输入格式错误");
return;
}
}
for(int i=0;i<workout.length;i++){
arrCopy=new int[workout[i][1]-workout[i][0]+1];
System.arraycopy(arrwork,workout[i][0]-1, arrCopy,0, workout[i][1]-workout[i][0]+1);
fastWork(arrCopy,0,arrCopy.length-1);
System.out.println(arrCopy[workout[i][1]-workout[i][0]+1-workout[i][2]]);
}
}
public static void fastWork(int[] arr, int left, int right) {
if(left > right) {
return;
}
int base = arr[left];
int i = left, j = right;
while(i != j) {
while(arr[j] >= base && i < j) {
j--;
}

    while(arr[i] <= base && i < j) {
        i++;
    }
    if(i < j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

arr[left] = arr[i];
arr[i] = base;
fastWork(arr, left, i - 1);
fastWork(arr, i + 1, right);

}
}
public class check {

public static void main(String[] args){
int testtime =1000;
int MaxSize =10;
int MaxValue =1000;
boolean isSucceed=true;
int[] arr1;
int[] arr2;
int[] arr3;
for(int i=1;i<=testtime;i++){
arr1=getRedomArray(MaxSize,MaxValue);
arr2=copyArray(arr1);
arr3=copyArray(arr2);//如果失败则打印该样本
systemSort(arr1);
quickSort(arr2,0,arr2.length-1);
if(!isEqual(arr1,arr2)){
isSucceed=false;
System.out.println(Arrays.toString(arr3));
break;
}
}
System.out.println(isSucceed?"该算法正确":"该算法不正确");
int [] arr=getRedomArray(MaxSize,MaxValue);
System.out.println(Arrays.toString(arr));
quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));

}
//数组复制
public static int[] copyArray(int[] arr1){
int[] arr2=new int[arr1.length];
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
return arr2;
}
//随机随机样本产生器
public static int[] getRedomArray(int MaxSize,int MaxValue){
int[] temp=new int[(int) ((MaxSize+1)Math.random())];
for(int i=0;i<temp.length;i++){
temp[i]=(int)((MaxValue+1)
Math.random()-(int)(MaxValue*Math.random()));
}
return temp;
}
//对比方法
public static boolean isEqual(int[] arr1,int[] arr2){
if((arr1null&&arr2!=null)||(arr1!=null&&arr2null)){
return false;
}
if(arr1null&&arr2null){
return true;
}
if(arr1.length!=arr2.length){
return false;
}
for(int i=0;i<arr1.length;i++){
if(arr1[i]!=arr2[i]){
return false;
}
}
return true;
}
//系统排序
public static void systemSort(int[] arr){
Arrays.sort(arr);
}
//快速排序
public static void quickSort(int[] arr, int low, int high) {
if (low>=high){
return;
}
//找出基准值temp
int index = getIndex(arr, low, high);
//左排序
quickSort(arr,0,index-1);
//右排序
quickSort(arr,index+1,high);
}
public static int getIndex(int[] arr, int low, int high) {
int temp=arr[low];
while (low<high){
//右排序
while (low<high&&arr[high]>=temp){
//如果arr[high]>=temp,high--
high--;
}
//把arr[high]的值赋给arr[low]
arr[low]=arr[high];
//左排序
while (low<high&&arr[low]<temp){
//如果arr[low]<temp,low++
low++;
}
//把arr[low]的值赋给arr[high]
arr[high]=arr[low];
}
//复位
arr[low]=temp;
return low;
}
}

第二题 二叉树遍历算法
解题思路:主要是使用java中的while循环和LinkedList来实现,主要的做题过程是复制来时的代码,进行修改运行
运行代码及结果
import java.util.LinkedList;

public class Treework {
public static void main(String[] args) {

Node root = into();
// 先序遍历
System.out.println("\n"+"前序遍历");
A(root);
// 中序遍历
System.out.println("\n"+"中序遍历");
B(root);
// 后续遍历
System.out.println("\n"+"后序遍历");
C(root);
// 层级遍历
System.out.println("\n"+"层序遍历");
D(root);

}

private static void A(Node root) {
// TODO 先序遍历
if (root == null) return;
System.out.print(root.data +" ");
A(root.l);
A(root.r);
}
private static void B(Node root) {
// TODO 中序遍历
if (root == null) return;
B(root.l);
System.out.print(root.data+" ");
B(root.r);
}
private static void C(Node root) {
// TODO 后续遍历
if (root == null) return;
C(root.l);
C(root.r);
System.out.print(root.data +" ");
}

private static void D(Node root) {
// TODO 层级遍历
LinkedList list= new LinkedList();
list.add(root);
while(! list.isEmpty()) {
Node ww=(Node) list.pop();
if(ww.l !=null) {
list.offer(ww.l);
}
if(ww.r !=null) {
list.offer(ww.r);
}
System.out.print(ww.data +" ");
}
}

// 构建一颗树,返回根节点
private static Node into(){
Node root = new Node("A");
Node node1 = new Node("T");
Node node2 = new Node("D");
Node node3 = new Node("N");
Node node4 = new Node("B");
Node node5 = new Node("6");
Node node6 = new Node("5");
Node node7 = new Node("4");
Node node8 = new Node("9");
Node node9 = new Node("1");
root.l = node1;
node1.l = node2;
node2.l = node3;
node2.r = node6;
node3.r = node7;
node7.r = node8;
node6.l = node9;
node3.l = node4;
root.r = node5;
return root;
}

// 节点
static class Node{
// 数据
Object data;
// 左孩子
Node l;
// 右孩子
Node r;

  public Node(){}

  public Node(Object data) {
      this.data = data;
      this.l = null;
      this.r = null;
  }

  public Node(Object data, Node l, Node r) {
      this.data = data;
      this.l = l;
      this.r = r;
  }

}
}

作业次数

次数 第一周 第二周 第三周 第四周
作业 自我介绍 作业02 作业03 作业04
posted @ 2020-10-29 19:47  first_leiyucai  阅读(97)  评论(0编辑  收藏  举报