3, Insert sort

Posted by maxmjessop on Sun, 03 May 2020 07:17:23 +0200

1. Algorithm principle
Insertion sorting is to insert a data into the ordered data that has been arranged, so as to get a new ordered data with one number plus one. This algorithm is suitable for sorting a small amount of data. The time complexity is O(n^2) and the space complexity is O(1). Insertion sort is a stable sort algorithm.

2. Code implementation
(1) JAVA implementation
Note: This program design generates 10 random numbers, and then inserts and sorts them from small to large

/*
 * Sort algorithm: insert sort
 * By:
 * Date: March 17, 2016
 */
import java.util.Random;

public class InsertSort {
    public static void main(String[] args) {
        //Define an array that can hold 10 numbers
        int[] arr = new int[10];
        int[] rs = new int[10];
        init(arr);
        System.out.print("Before sorting:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ", ");
        }
        System.out.println();
        rs = insertSort(arr);
        System.out.print("After sorting:");
        for (int i = 0; i < rs.length; i++) {
            System.out.print(rs[i] + ", ");
        }
    }

    public static void init(int[] a) {
        Random r = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i] = r.nextInt(100);
        }
    }

    public static int[] insertSort(int[] a) {
        if (a == null || a.length < 2) {
            return a;
        }
        int tmp;
        for (int i = 1; i < a.length; i++) {
            //Traverse and compare the first i-1 data one by one, and insert the ith number into the appropriate position
            for (int j = i; j > 0; j--) {
                if (a[j] < a[j - 1]) {
                    tmp = a[j];
                    a[j] = a[j - 1];
                    a[j - 1] = tmp;
                } else {
                    break;
                }
            }
        }
        return a;
    }
}

Operation result:

(2) PHP implementation
Note: the program design randomly generates 10 ~ 20 numbers of 1 ~ 100, and then inserts and sorts these numbers from small to large

<!--
Method:Insert sort
Author: xiaoxiao
Date: 2016-03-17
-->

<?php
        echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";//If the PHP page is involved in Chinese scrambling, add the line

        function InsertSort($a){
                $length = count($a);
                for($i = 1; $i < $length; $i++){
                        //Search the number i+1~$length to find the coordinate corresponding to the smallest number
                        for ($j = $i; $j > 0; $j--) {
                                if ($a[$j] < $a[$j-1]) {
                                        $temp = $a[$j];
                                        $a[$j] = $a[$j-1];
                                        $a[$j-1] = $temp;
                                }
                                else{
                                        break;
                                }
                        }
                }
                return $a;
        }

        $a = array();
        $nums = mt_rand(10,20);
        for ($i = 0; $i < $nums; $i++) { 
                array_push($a, mt_rand(1,100));
        }

        echo "The numbers to be sorted are".$nums."individual!<br />";
        echo "Before sorting: ";
        for($i = 0; $i < count($a); $i++){
                echo $a[$i];
                echo ", ";
        }
        echo "<br />";
        echo "After sorting:  ";
        $b = InsertSort($a);

        for($i = 0; $i < count($b); $i++){
                echo $b[$i];
                echo ", ";
        }
?>

Operation result:

Topics: PHP Java