zoukankan      html  css  js  c++  java
  • 数组排序 (选择排序、冒泡排序、插入排序、希尔排序)

    选择排序

    package com.Java;
    public class ArraySortSelect {
    public void selectSort(int [] arr){//定义一个选择排序的方法
      for (int i = 0; i < arr.length-1; i++) {
        for (int j = i+1; j < arr.length; j++) {
          if (arr[i]>arr[j]) {
            int temp = arr[i] ;
            arr[i] = arr[j];
            arr[j] = temp ;
          }
        }
      }
    }
    public static void main(String[] args) {
      ArraySortSelect a = new ArraySortSelect() ;//创建一个对象,对象名为a
      int [] arr2 = {9,6,3,8,5,2,7,4,1,0};
      a.selectSort(arr2) ;//对象a调用冒泡排序的方法对arr2排序
      for (int i = 0; i < arr2.length; i++) {
      System.out.print(arr2[i]+" ");
      }
    }
    }

    冒泡排序

    package com.Java;
    public class ArraySortBubble {
      public void bubbleSort(int arr []){
        for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length-i-1; j++) {
          if (arr[j]>arr[j+1]) {
            int temp = arr[j];
            arr[j] = arr[j+1];
            arr[j+1] = temp ;
          }
        }
      }
    }
    public static void main(String[] args) {
      int [] arr2 = {9,6,3,8,5,2,7,4,1,0} ;
      ArraySortBubble m = new ArraySortBubble();
      m.bubbleSort(arr2);
      for (int i = 0; i < arr2.length; i++) {
        System.out.print(arr2[i]+" ");
      }
    }
    }

    插入排序

    package com.Java;
    public class ArraySortInsert {
    public void insertSort(int [] arr){
      for(int i=1;i<arr.length;i++){
        for(int j=i;j>0;j--){
          if (arr[j-1]>arr[j]){
            int temp=arr[j-1];
            arr[j-1]=arr[j];
            arr[j]=temp;
          }
        }
      }
    }
    public static void main(String[] args) {
      int [] arr2 = {9,6,3,8,5,2,7,4,1,0} ;
      ArraySortInsert m = new ArraySortInsert();
      m.insertSort(arr2);
      for (int i = 0; i < arr2.length; i++) {
        System.out.print(arr2[i]+" ");
       }
    }
    }

    希尔排序

    package com.Java;
    public class ArraySortShell {
    public static void main(String[] args) {
      int[] i = { 10, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };

      System.out.println("----希尔(Shell)排序的结果:"); 

      shell(i);

    }

    public static void shell(int[] x) {
      for (int increment = x.length / 2; increment > 0; increment /= 2) {   // 分组 
        for (int i = increment; i < x.length; i++) {   // 每个组内排序 
          int temp = x[i];
          int j = 0;
          for (j = i; j >= increment; j -= increment) {
            if (temp < x[j - increment]) {
            x[j] = x[j - increment];
          } else {
            break;
            }
          }
          x[j] = temp;
        }
      }
      for (int i : x) {
      System.out.print(i + " ");
      }
    }
    }

  • 相关阅读:
    web前端开发常用链接
    Restful API 设计原则
    web最佳实践
    MySQL 命令
    Mac MySQL安装
    IntelliJ IDEA 创建Maven项目及tomcat配置
    Mac安装和配置Maven
    Mac安装和配置Tomcat
    IntelliJ IDEA 创建java项目及字体设置
    Java-GUI
  • 原文地址:https://www.cnblogs.com/ljwa/p/6082468.html
Copyright © 2011-2022 走看看