zoukankan      html  css  js  c++  java
  • 基本排序算法java实现

    1、直接插入排序

      基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。

           如此反复循环,直到全部排好顺序。

    public class InsertSort {
        public static void main(String[] args) {
            int[] a = new int[args.length];
            for (int i=0;i<args.length;i++) {
                a[i] = Integer.parseInt(args[i]);
            }
            print(a);
            InsertSort(a);
            print(a);
        }
    
        private static void InsertSort(int[] a) {
            int temp = 0;
               for(int i = 1; i < a.length; i++){
                int j = i-1;
                temp = a[i];
                for(; j >= 0 && temp < a[j]; j--){
                   a[j+1] = a[j]; 
                }
                a[j+1] = temp;
            }
        }
    
        private static void print(int[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }

    2、简单选择排序

      基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;然后在剩下的数当中再找最小的与第二个位置的数交换,

           如此循环到倒数第二个数和最后一个数比较为止。

    public class selectionSort {
        public static void main(String[] args) {
            int[] a = new int[args.length];
            for (int i = 0; i < args.length; i++) {
                a[i] = Integer.parseInt(args[i]);
            }
            print(a);
            selectionSort(a);
            print(a);
        }
    
        private static void selectionSort(int[] a) {
            int k,temp;
            for (int i = 0; i < a.length; i++) {
                k = i;
                for (int j = i+1; j < a.length; j++) {
                    if (a[j] < a[k]) {
                        k = j;                    
                    }
                }
    
                if (k != i) {
                    temp = a[i];
                    a[i] = a[k];
                    a[k] = temp;            
                }
                    
            }
                
        }
    
        private static void print(int[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }

    3、冒泡排序

      基本思想:在要排序的组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。

           即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

    public class bubbleSort {
        public static void main(String[] args) {
            int[] a = new int[args.length];
            for (int i=0;i<args.length;i++) {
                a[i] = Integer.parseInt(args[i]);
            }
            print(a);
            bubbleSort(a);
            print(a);
        }
    
        private static void bubbleSort(int[] a) {
            int temp = 0;
               for(int i = 0; i < a.length-1; i++) {
                for(int j = 0; j < a.length-1-i; j++) {
                    if(a[j] > a[j+1]) {
                        temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                    }
                }
            }
        }
    
        private static void print(int[] a) {
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }
  • 相关阅读:
    【crontab】误删crontab及其恢复
    New Concept English there (7)
    New Concept English there (6)
    New Concept English there (5)
    New Concept English there (4)
    New Concept English there (3)
    New Concept English there (2)Typing speed exercise
    New Concept English there (1)Typing speed exercise
    New Concept English Two 34 game over
    New Concept English Two 33 94
  • 原文地址:https://www.cnblogs.com/lea-fu/p/3261751.html
Copyright © 2011-2022 走看看