zoukankan      html  css  js  c++  java
  • Insertion Sort in Java, C,C++ and Python

    /**
     * @Author: Chintsai Hwo
     * @Date: Created on 5:40 AM 6/2/2019
     */
    
    import java.util.Scanner;
    
    public class InsertSort {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Input the size of the int array: ");
            int arraySize = scanner.nextInt();
            int[] intArray = new int[arraySize];
            System.out.println("Input the elements of the int array: ");
            for (int i = 0; i < intArray.length; i++) {
                intArray[i] = scanner.nextInt();
            }
            insertionSort(intArray);
        }
    
        static void insertionSort(int[] a) {
            int i, j;
            int k;
            for (j = 1; j < a.length; j++) {
                k = a[j];
                i = j - 1;
                while (i >= 0 && a[i] > k) {
                    a[i + 1] = a[i];
                    i--;
                }
                a[i + 1] = k;
            }
            System.out.println("The sorting result is: ");
            for (int arrayElement : a)
                System.out.print(arrayElement + " ");
        }
    }
    #include <stdio.h>
    
    void insertionSort(int array[]);
    
    int main() {
        int a[] = {5, 27, 5, 43, 4, 3, 2, 12};
        insertionSort(a);
        return 0;
    }
    
    void insertionSort(int array[]) {
        int length = sizeof(array);
        int i, j;
        int k;
        for (j = 1; j < length; j++) {
            k = array[j];
            i = j - 1;
            while (i >= 0 && array[i] > k) {
                array[i + 1] = array[i];
                i--;
            }
            array[i + 1] = k;
        }
        for (int index = 0; index < length; index++)
            printf("%d ", array[index]);
        return;
    }
    #include <iostream>
    
    using namespace std;
    
    void insertionSort(int a[]);
    
    int main() {
        int a[] = {4, 2, 63, 25, 6, 75, 1, 3};
        insertionSort(a);
        for (int index = 0; index < sizeof(a) / sizeof(int); index++)
            cout << a[index] << " ";
        return 0;
    }
    
    void insertionSort(int a[]) {
        int i, j;
        int k;
        int length = sizeof(a);
        for (j = 1; j < length; j++) {
            k = a[j];
            i = j - 1;
            while (i >= 0 && a[i] > k) {
                a[i + 1] = a[i];
                i--;
            }
            a[i + 1] = k;
        }
    }
    def insertion_sort(array):
        for j in range(1, len(array)):
            k = array[j]
            i = j - 1
            while i >= 0 and array[i] > k:
                array[i + 1] = array[i]
                i = i - 1
            array[i + 1] = k
        print(array)
    
    
    a = [32, 14, 1, 2, 4, 3, 23]
    if __name__ == "__main__":
        insertion_sort(a)
    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    [原创]平面机器人的避障策略思考
    做个快乐的程序员
    [知识]双音多频(DTMF)信号
    osg 关于LOD
    (3)vtkMapper
    关于坐标系,关于矩阵及线性相关和无关的关系
    osg找不到插件的解决办法
    逆风飞扬,吴仁宏
    整合qt设计师和vs2008出了点问题,记下来
    关于NodeVisitor访问者模式
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829242.html
Copyright © 2011-2022 走看看