java 插入排序算法实现_C程序实现插入排序算法

news/2024/5/19 22:49:57 标签: 算法, 排序算法, 数据结构, java, 快速排序

java 插入排序算法实现

Insertion sort is a simple sorting algorithm that is online, stable, and in-place.

插入排序是一种简单的排序算法,可在线,稳定且就地进行

  1. A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is present in the input unsorted array.

    一种稳定的排序算法是一种具有相等值的键在排序后的输出数组中以与输入未排序数组中存在的键相同的顺序出现的算法

  2. An in-place sorting algorithm has various definitions but a more used one is –

    就地排序算法具有多种定义,但使用更广泛的是–

    "An in-place sorting algorithm does not need extra space and uses the constant memory for manipulation of the input in-place. Although, it may require some extra constant space allowed for variables."

    “就地排序算法不需要额外的空间,而是使用常量内存来就地输入的操作。尽管如此,它可能需要一些额外的常量空间来允许变量使用。”

The basic principle followed in Insertion sort is that the key element is placed at its position in the sorted array in every iteration.

插入排序中遵循的基本原理是,每次迭代中,键元素均位于其在已排序数组中的位置。

Pseudo code:

伪代码:

    for i = 1 to n
          key = arr[i]
          j = i-1
          while  j >= 0 and key < arr[j]
                   arr[j + 1] = arr[j]
                   j = j – 1
          end while
      
          arr[ j + 1 ] = key
    end for

Example:

例:

    Array = 12, 14, 11, 5, 6
    Initially, 
    we run the loop from index = 1 (14) till index = 4 (last value)

    For i = 1, 
    compare 14 with 12 and we see, 12< 14, so no change.
    
    For i = 2, 
    compare 11 with 14, and we see, 11<14, 
    shift 14 to next place. 
    Now, compare 11 with 12, we see that 11<12, 
    shift 12 by one place. 
    Now, put 11 at index 0.
    Array: 11, 12, 14, 5, 6
    
    For i = 3, 
    compare 5 and keep shifting the values.
    Array: 5, 11, 12, 14, 6
    
    For i = 4, 
    compare with all the previous elements and 
    keep shifting the values accordingly.

Time Complexity: The time complexity of Insertion Sort can be described as: T(n) = T(n/2) + C

时间复杂度:插入排序的时间复杂度可以描述为: T(n)= T(n / 2)+ C

  • Worst case: O(n^2)

    最坏的情况:O(n ^ 2)

  • Average Case: O(n^2)

    平均情况:O(n ^ 2)

  • Best case: O(n), when the array is already sorted

    最好的情况:O(n),当数组已经排序时

  • Space Complexity: O(1)

    空间复杂度:O(1)

C Implementation:

C实现:

#include <stdio.h>

void insertion(int arr[], int n)
{
    int i, j;
    for (i = 1; i < n; i++) {
        int key = arr[i];
        j = i - 1;
        while (key < arr[j] && j >= 0) {
            arr[j + 1] = arr[j];
            j--;
        }

        arr[j + 1] = key;
    }
    printf("\nAfter performing Insertion sort:\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
}

int main()
{

    int arr[] = { 10, 14, 3, 8, 5, 12, 16, 13 };
    int n = sizeof(arr) / sizeof(arr[0]);

    insertion(arr, n);

    return 0;
}

Output

输出量

After performing Insertion sort:
3 5 8 10 12 13 14 16 

Insertion Sort can be optimized using Binary Insertion sorting. We must use Insertion sort where the elements are mostly sorted and only some elements are misplaced. Also, it should be used where there is lesser number of data points due to its large time complexity.

插入排序可以用二进制插入排序进行优化。 我们必须使用插入排序,其中元素大多是排序的,只有一些元素放错了位置。 另外,由于时间复杂度高,应在数据点数量较少的地方使用。

翻译自: https://www.includehelp.com/c-programs/implement-insertion-sort-algorithm.aspx

java 插入排序算法实现


http://www.niftyadmin.cn/n/1255297.html

相关文章

hive 学习系列六 hive 去重办法的思考

方法1&#xff0c;建立临时表&#xff0c;利用hive的collect_set 进行去重。 create table if not exists tubutest (name1 string,name2 string ) ROW FORMAT DELIMITED FIELDS TERMINATED BY , STORED AS TEXTFILE;select * from ods.wdtest; 1 1 1 1 1 2 1 2 1 3 …

#program once 和 #ifndef

#program once 和 #ifndef 在写小demo的时候&#xff0c;注意到vs中会自动生成#program once&#xff1b;看别人写的代码的时候见到比较多的反而是#ifndef—#define—#endif&#xff1b;从字面上看两种方式会产生相同的效果&#xff1a;避免同一个文件重复包含多次&#xff0c…

java 根据类名示例化类_Java即时类| isBefore()方法与示例

java 根据类名示例化类即时类isBefore()方法 (Instant Class isBefore() method) isBefore() method is available in java.time package. isBefore()方法在java.time包中可用。 isBefore() method is used to check whether this Instant value comes before the given Instan…

安卓数据转移到iphone老是中断_一点换机开启智能换机时代 安卓数据无缝转移...

手机换机功能就是把旧手机的资料&#xff0c;全部导入到新手机中。随着用户更换手机的速度越来越频繁&#xff0c;换机功能已经成为手机不可或缺的一部分&#xff0c;它能够实现新旧手机资料快速迁移&#xff0c;非常便利。从旧手机换到新手机首先要过数据传输这一关&#xff0…

c++stl和std_std :: replace_if()函数以及C ++ STL中的示例

cstl和stdC STL std :: replace_if()函数 (C STL std::replace_if() function) replace_if() function is a library function of algorithm header, it is used to replace the value in a given range based on the given unary function that should accept an element in …

汇编——段寄存器

CS段只有16位&#xff0c;8086CPU有20根地址线&#xff0c;地址该如何存储呢&#xff1f; 答&#xff1a;地址除以16&#xff08;十进制&#xff09;&#xff0c;此时地址以0结尾&#xff08;十六进制&#xff09;作为段地址。IP&#xff1a;指令指针寄存器&#xff0c;存储了指…

java源码-HashMap类设计

map(内部interface Entry<K,V>)->abstractMap(定义视图 entrySet抽象方法)->hashMap(静态内部类Node(继承Entry<K,V>)) TreeNode 继承 LinkedHashMap.Entry 继承 HashMap.Node转载于:https://www.cnblogs.com/1ssqq1lxr/p/9480166.html

汇编——16位汇编调试一些练习代码

编译&#xff1a;ml 1.asm调试&#xff1a;debug 1.exe调试命令&#xff1a; r 查看当前位置代码&#xff1b;p 单步步过&#xff1b;t 单步步入&#xff1b;u 查看附近代码 d 0b3b 查看0b3b位置内存&#xff1b;g 运行程序16位汇编代码基本结构 assume cs:code,ds:datadata s…