• 常见的排序算法(直接插入&选择排序&二分查找排序)


    1、直接插入排序算法

    源码:

    package com.DiYiZhang;
    /* 插入排序算法
     * 如下进行的是插入,排序算法*/

    public class InsertionSort {
        public static void insertionSort(int[] a) {
            int tmp;
            for (int i = 1; i < a.length; i++) {
                for (int j = i; j >0; j--) {
                    if (a[j] < a[j-1]) {
                        tmp = a[j-1];
                        a[j-1] = a[j];
                        a[j] = tmp;
                    }
                }
    //            for(int j=i+1;j<a.length;j++){
    //            if(a[j]>a[j+1]){
    //                tmp=a[j+1];
    //                a[j+1]=a[j];
    //                a[j]=tmp;
    //            }
    //            }
            }
        }
        public static void main(String[] args) {
            int[] a = { 49, 38, 65, 97, 76, 13, 27, 50 };
            insertionSort(a);
            for (int i : a)
                System.out.print(i + " ");
        }
    }

    2、二分查找排序算法

    源码:

    package com.DiYiZhang;
    import java.security.*;
    import java.awt.dnd.*;
    import java.awt.*;
    import java.net.*;
    import java.sql.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Arrays;
    import java.util.Scanner;

    /*  本程序用来将乱序的一组数字先按照二分查找的方式进行排序*/
    public class MergeSort {
         public static void main(String[] args) {
    //         Scanner reader=new Scanner(System.in);
    //         reader.hasNextLine();
                int []a ={564,378,954,1255,684,57,15,389,687,45,98,125,348};  
                new MergeSort().MergeSort(a,0,a.length-1);  
                for(int i:a) {  
                    System.out.print(i+" ");  
                }  
            }  
            public void MergeSort(int []a,int low,int high) {  
                int mid=0;  
                while(low<high) {  
                    mid=low+(high-low)/2;  
                    MergeSort(a,low,mid);  
                    MergeSort(a,mid+1,high);  
                    Merge(a,low,mid,high);  
                    return;  
                }  
            }  
            private void Merge(int[] a, int low, int mid, int high) {  
                // TODO Auto-generated method stub  
                int i=low,j=mid+1,k=0;  
                int []b = new int[high-low+1];  
                while(i<=mid&&j<=high) {  
                    if(a[i]<=a[j]) {  
                        b[k++]=a[i++];  
                    }else {  
                        b[k++]=a[j++];  
                    }  
                }  
                while(i<=mid) {  
                    b[k++]=a[i++];  
                }  
                while(j<=high) {  
                    b[k++]=a[j++];  
                }  
                for(int x=0,m=low;m<=high;m++) {  
                    a[m]=b[x++];  
                }  
                  
            }  
        }  

    3、直接排序算法

    源码:

    package com.DiYiZhang;

    import java.util.Arrays;

    public class SelectSort {

        public static void selectSort(int[] a) {
            if (a == null || a.length <= 0) {
                return;
            }
            for (int i = 0; i < a.length; i++) {
    ////            int temp = a[i];
    //            int temp=i;
                int flag = i; // 将当前下标定义为最小值下标
                for (int j = i + 1; j < a.length; j++) {
                    if (a[j] < a[flag]) {// a[j] < temp 从小到大排序;a[j] > temp 从大到小排序
    //                    temp = a[j];
                        flag = j; // 如果有小于当前最小值的关键字将此关键字的下标赋值给flag
                    }    
                }
                if (flag != i) {
                    int temp=a[i];
                    a[i]=a[flag];
                    a[flag] = temp;
    //                a[i] = temp;
                }
            }
        }
        public static void main(String[] args) {
            int a[] = { 49,38,65,97,76,13,27,49 };
            System.out.println("交换之前的排序列表");
            for(int i:a){
                System.out.print(i+" ");
            }
            selectSort(a);
            System.out.println("交换之后的排序列表");
            System.out.println(Arrays.toString(a));
        }
    }
    //class A{
    //    int sys(int a){
    //        int w=a;
    //        return w;
    //    }
    //}
    //public class InsertionSort{
    //    public static void insrtionSort(int []a){
    //        int tmp;
    //        for(int i=1;i<a.length;i++){
    //            for(int j=i;j>0;j--){
    //                if(a[j]<a[j-1]){
    //                    tmp=a[j-1];
    //                    a[j-1]=a[j];
    //                    a[j]=tmp;
    //                }
    //            }
    //        }
    //    }
    //    public static void main(String [] args){
    //        int[]a={23,34,2,45,3,24,54,33};
    //        for(int i:a){
    //            System.out.println(i+" ");
    //        }
    //    }
    //}
    4、背包问题

    1. public class sf {  
    2.   
    3.     public static void main(String[] args) {  
    4.         // TODO Auto-generated method stub  
    5.         int[] weight = {3,5,2,6,4}; //物品重量  
    6.         int[] val = {4,4,3,5,3}; //物品价值  
    7.         int m = 12; //背包容量  
    8.         int n = val.length; //物品个数  
    9.           
    10.         int[][] f = new int[n+1][m+1]; //f[i][j]表示前i个物品能装入容量为j的背包中的最大价值  
    11.         int[][] path = new int[n+1][m+1];  
    12.         //初始化第一列和第一行  
    13.         for(int i=0;i<f.length;i++){  
    14.             f[i][0] = 0;  
    15.         }  
    16.         for(int i=0;i<f[0].length;i++){  
    17.             f[0][i] = 0;  
    18.         }  
    19.         //通过公式迭代计算  
    20.         for(int i=1;i<f.length;i++){  
    21.             for(int j=1;j<f[0].length;j++){  
    22.                 if(weight[i-1]>j)  
    23.                     f[i][j] = f[i-1][j];  
    24.                 else{  
    25.                     if(f[i-1][j]<f[i-1][j-weight[i-1]]+val[i-1]){  
    26.                         f[i][j] = f[i-1][j-weight[i-1]]+val[i-1];  
    27.                         path[i][j] = 1;  
    28.                     }else{  
    29.                         f[i][j] = f[i-1][j];  
    30.                     }  
    31.                     //f[i][j] = Math.max(f[i-1][j], f[i-1][j-weight[i-1]]+val[i-1]);  
    32.                 }  
    33.             }  
    34.         }  
    35.         for(int i=0;i<f.length;i++){  
    36.             for(int j=0;j<f[0].length;j++){  
    37.                 System.out.print(f[i][j]+" ");  
    38.             }  
    39.             System.out.println();  
    40.         }  
    41.           
    42.         int i=f.length-1;  
    43.         int j=f[0].length-1;  
    44.         while(i>0&&j>0){  
    45.             if(path[i][j] == 1){  
    46.                 System.out.print("第"+i+"个物品装入 ");  
    47.                 j -= weight[i-1];  
    48.             }  
    49.             i--;  
    50.         }  
    51.           
    52.     }  
    53.   





















  • 相关阅读:
    js/jquery键盘事件及keycode大全
    阿里巴巴首款商用字体免费开放:阿里巴巴普惠字体
    从kinit到kerberos安全机制
    react中dangerouslySetInnerHTML使用
    URLSearchParams
    React router的Route中component和render属性的使用
    SSH 命令的三种代理功能(-L/-R/-D)
    H5 直播的疯狂点赞动画是如何实现的?(附完整源码)
    CenterOS中安装Redis及开机启动设置
    使用require.context实现前端自动化
  • 原文地址:https://www.cnblogs.com/xinxianquan/p/9206282.html
Copyright © 2020-2023  润新知