zoukankan      html  css  js  c++  java
  • 2020.7.10

    学习内容:

    1.数字排序 

    (1)冒泡排序

     1 package day01;
     2 import java.util.Arrays;
     3 public class BubbleSortDemo{
     4     public static void main(String[] args) {
     5         int[]ary= {2,3,4,5,9,7,8};
     6         //遍历,依次相邻比较大小
     7         for(int i=0;i<ary.length;i++) {
     8             for(int j=0;j<ary.length-1;j++) {
     9                 if(ary[j]>ary[j+1]){
    10                     int t=ary[j];
    11                     ary[j]=ary[j+1];
    12                     ary[j+1]=t;
    13                 }
    14             }
    15         }
    16         //打印排序后的数组内容
    17         System.out.println("排序后的数组:"+Arrays.toString(ary));
    18     }
    19 }

    (2)选择排序

     1 package day01;
     2 import java.util.Arrays;
     3 public class SelectSortDemo{
     4     public static void main(String[] args) {
     5         int[]ary= {2,3,4,5,9,7,8};
     6         for(int i=0;i<ary.length-1;i++) {
     7             for(int j=i+1;j<ary.length;j++) {
     8                 if(ary[i]>ary[j]){
     9                     int t=ary[i];
    10                     ary[i]=ary[j];
    11                     ary[j]=t;
    12                 }
    13             }
    14         }
    15         //打印排序后的数组内容
    16         System.out.println("排序后的数组:"+Arrays.toString(ary));
    17     }
    18 }

    (3)插入排序

     1 package day01;
     2 import java.util.Arrays;
     3 public class InsertionSortDemo{
     4     public static void main(String[] args) {
     5         int[]ary= {2,3,4,5,9,7,8};
     6         int i,j,k;
     7         for(i=1;i<ary.length;i++) {
     8             k=ary[i];//取出待插入元素
     9             //找到插入位置
    10             for(j=i-1;j>=0&&k<ary[j];j--) {
    11                 ary[j+1]=ary[j];//移动元素
    12             }
    13             //插入元素
    14             ary[j+1]=k;
    15         }
    16         //打印排序后的数组内容
    17         System.out.println("排序后的数组:"+Arrays.toString(ary));
    18     }
    19 }

    (4)快速排序

     

     1 package day01;
     2 import java.util.Arrays;
     3 public class QuickSortDemo{
     4     public static void main(String[] args) {
     5         int vec[]= {37,47,23,100,19,56,56,99,9};
     6         QuickSortDemo q=new QuickSortDemo();
     7         q.quicksort(vec,0,vec.length-1);
     8         System.out.println("排序后:"+Arrays.toString(vec));
     9     }
    10     public void quicksort(int a[],int low,int high) {
    11         //假设传入low=0;high=a.length-1;
    12         if(low<high) {//条件判断
    13             int pivot,p_pos,i;//声明变量
    14             p_pos=low;//p_pos指向low,即位索引位0位置
    15             pivot=a[p_pos];//将0位置上的数值赋给pivot
    16             for(i=low+1;i<=high;i++) {//循环次数,i=1
    17                 if(a[i]>pivot) {//1位置的数与0位置数作比较:a[1]>a[0]
    18                     p_pos++;//2位与1位比较,3位与2位比较....
    19                     int tmp=a[p_pos];
    20                     a[p_pos]=a[i];
    21                     a[i]=tmp;
    22                 }
    23             }
    24             int tmp=a[low];
    25             a[low]=a[p_pos];
    26             a[p_pos]=tmp;
    27             quicksort(a,low,p_pos-1);//递归调用,排序左半区
    28             quicksort(a,p_pos+1,high);//递归调用,排序右半区
    29         }
    30     }
    31 } 

     遇到的问题:

    1、快速排序和插入排序理解不清

    https://blog.csdn.net/weixin_43956598/article/details/90181567

    https://blog.csdn.net/weixin_43956598/article/details/90215135

    2、括号数目总是混乱

    明天要学习的内容:

    3.3

  • 相关阅读:
    软件工程第一次实验——软件开发文档与工具的安装与使用
    软件工程第四次作业——ATM管理系统
    软件工程第三次作业——举例分析流程图与活动图的区别与联系
    软件工程第二次作业——小学四则运算题目生成程序
    mac终端进入mysql
    计算机网络
    类和对象(未完成)
    html_css
    Mac下使用C语言生成和使用动态链接库
    模运算
  • 原文地址:https://www.cnblogs.com/ltw222/p/13295220.html
Copyright © 2011-2022 走看看