As in selection sort, the items to the left of the current index are in sorted order during the sort, but they are not in their final position, as they may have to be moved to make room for smaller items encountered later. The array is, however, fully sorted when the index reaches the right end.
Unlike that of selection sort, the running time of insertion sort depends on the initial order of the items in the input. For example, if the array is large and its entries are already in order (or nearly in order), then insertion sort is much, much faster than if the entries are randomly ordered or in reverse order.
Time complexity:
Insertionsortuses ~N^2 /4 compares and ~N^2/4 exchanges to sort a randomly ordered array of length N with distinct keys, on the average. The worst case is ~N^2/2 compares and ~N^2/2 exchanges and the best case is N - 1 compares and 0 exchanges.
Insertion sort works well for certain types of nonrandom arrays that often arise in practice, even if they are huge. For example, as just mentioned, consider what happens when you use insertion sort on an array that is already sorted. Each item is immediately determined to be in its proper place in the array, and the total running time is linear. (The running time of selection sort is quadratic for such an array.)
public class Insertion { // use natural order and Comparable interface public static void sort(Comparable[] a) { int N = a.length; for (int i = 0; i < N; i++) { //showGUI(a); for (int j = i; j > 0 && less(a[j], a[j-1]); j--) { exch(a, j, j-1); // showGUI(a); } assert isSorted(a, 0, i); } assert isSorted(a); } // use a custom order and Comparator interface - see Section 3.5 public static void sort(Object[] a, Comparator c) { int N = a.length; for (int i = 0; i < N; i++) { for (int j = i; j > 0 && less(c, a[j], a[j-1]); j--) { exch(a, j, j-1); } assert isSorted(a, c, 0, i); } assert isSorted(a, c); } // return a permutation that gives the elements in a[] in ascending order // do not change the original array a[] public static int[] indexSort(Comparable[] a) { int N = a.length; int[] index = new int[N]; for (int i = 0; i < N; i++) index[i] = i; for (int i = 0; i < N; i++) for (int j = i; j > 0 && less(a[index[j]], a[index[j-1]]); j--) exch(index, j, j-1); return index; } /*********************************************************************** * Helper sorting functions ***********************************************************************/ // is v < w ? private static boolean less(Comparable v, Comparable w) { return (v.compareTo(w) < 0); } // is v < w ? private static boolean less(Comparator c, Object v, Object w) { return (c.compare(v, w) < 0); } // exchange a[i] and a[j] private static void exch(Object[] a, int i, int j) { Object swap = a[i]; a[i] = a[j]; a[j] = swap; } // exchange a[i] and a[j] (for indirect sort) private static void exch(int[] a, int i, int j) { int swap = a[i]; a[i] = a[j]; a[j] = swap; } /*********************************************************************** * Check if array is sorted - useful for debugging ***********************************************************************/ private static boolean isSorted(Comparable[] a) { return isSorted(a, 0, a.length - 1); } // is the array sorted from a[lo] to a[hi] private static boolean isSorted(Comparable[] a, int lo, int hi) { for (int i = lo + 1; i <= hi; i++) if (less(a[i], a[i-1])) return false; return true; } private static boolean isSorted(Object[] a, Comparator c) { return isSorted(a, c, 0, a.length - 1); } // is the array sorted from a[lo] to a[hi] private static boolean isSorted(Object[] a, Comparator c, int lo, int hi) { for (int i = lo + 1; i <= hi; i++) if (less(c, a[i], a[i-1])) return false; return true; } // print array to standard output private static void show(Comparable[] a) { for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
相关推荐
插入排序(Insertion Sort)是一种...总的来说,插入排序是一种基础的排序算法,适用于理解排序算法的基本原理和实现。在实际应用中,对于大数据量的排序,通常会选择更高效的算法,如快速排序、归并排序或堆排序等。
- **性能评估**:对不同的数据结构和算法进行时间和空间复杂度的分析,评估其效率。 - **团队合作**:在项目开发过程中,培养团队协作能力,共同完成一个大型项目的设计与实现。 通过本教程的学习,不仅能够掌握...
标题中提到的“Java数据结构和算法”是计算机科学领域中的基础内容,对于学习Java语言的开发者来说尤为重要。数据结构是组织和存储数据的方式,使得数据操作更加高效;算法则是解决问题的一系列步骤。在Java编程中,...
在IT领域,掌握数据结构和算法是至关重要的,特别是对于编程语言如C#的开发者来说。数据结构是存储和组织数据的方式,而算法是解决问题或执行任务的特定步骤。本资源"**c#数据结构和算法源码**"提供了一个实践学习的...
insertion sort 数据结构基础
JavaScript_資料結構與演算法_氣泡排序_Bubble_Sort、插入排序_Insertion_Sort_實作與分析_-
### JAVA数据结构和算法迷你电子书知识点概览 #### 一、数组与简单排序 **数组** 是一种基本的数据结构,用于存储同类型的元素。数组中的每个元素可以通过索引访问。 - **一维数组** - **声明**: `type var-name...
- **排序算法**:详细讨论了几种常见的排序算法,包括插入排序(Insertion Sort)、选择排序(Selection Sort)、冒泡排序(Bubble Sort)和归并排序(Merge Sort)。每种算法都有其特定的优势和适用场景。 ##### ...
在编程领域,掌握数据结构和算法是提升编程能力的关键步骤,尤其是在Java这样的高级语言中。本文将深入探讨四种简单的排序算法:插入排序、冒泡排序、选择排序。这些算法虽然在复杂度上不如高级排序算法如快速排序或...
C++数据结构排序算法总结将为您提供详细的排序算法知识,包括比较排序和非比较排序两大类。 比较排序 比较排序是指通过比较元素的大小来确定其在序列中的位置的排序算法。比较排序算法的时间复杂度通常高于非比较...
《数据结构算法与应用--C++语言描述》是一本深入探讨数据结构和算法的书籍,其源代码由学习者精心整理并优化,便于理解和实践。这个压缩包包含了多个章节的练习代码,每个文件都按照“章_节_编号”的规则进行命名,...
该文档提供了Acm竞赛中常用的算法与数据结构的知识点总结,包括算法、数据结构、算法与数据结构的结合和实践经验等方面的内容。该文档对Acm竞赛的参赛者和关心算法与数据结构的人来说具有重要的参考价值。
Java作为一种广泛应用的编程语言,提供了丰富的工具和库来实现各种数据结构和算法。本项目基于Java语言,深入探讨并实现了多种常用的数据结构与算法,旨在帮助开发者巩固理论知识,提高实际编程能力。 首先,我们要...
### 数据结构排序算法详解 #### 冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复进行的,直到没有...
通过以上对数据结构、算法以及C++实现方面的详细阐述,可以看出《数据结构与算法C版本》这本书不仅覆盖了理论基础,还深入到了具体的实现细节和技术要点,对于学习和掌握数据结构与算法具有很高的参考价值。...
通过这些实现,用户可以深入理解各种算法的原理和应用场景,同时也可以作为学习和实践数据结构与算法的参考。 项目的主要特性和功能 1. 排序算法 冒泡排序(Bubble Sort) 选择排序(Selection Sort) 插入...