# What are some good greedy algorithm lists

## Insertion Sort

You need a detailed one Insertion Sort Explanation? Then you are exactly right here. First we explain them to you functionality based on a detailed Example. Then we'll show you which one algorithm is behind the sorting process in general and how to use it as a Pseudocode or Structogram can represent. Based on this, you can then use a possible implementation as a Insertion Sort Java-Code see. At the end you will find out all the important points about the Insertion Sort Term.

### Insertion Sort Explanation

The insertion sort is one of the most stable in computer science Sorting algorithms and can be used as Sort by insert are described, therefore also Insert sorting method called. The whole thing can of course simply be derived from the English words insertion = insert and sort = sort, which is why the sorting algorithm is sometimes called Insert location referred to as. In general, it can also be said that the sorting algorithm is easy to implement and with small or partially pre-sorted input quantities are very efficient is working. Since the sorting process does not require any additional storage space, the algorithm works in-placewhich of course speaks for its storage space complexity.

### Principle: sorting by inserting

The Basic idea is actually quite simple, as it is probably the closest thing to how you would order the numbers yourself. Just imagine a deck of cards that you have to sort according to size. You already have the 5 of spades and 9 of spades in your hand. Next, you draw the 6 of spades.

So you put this card in the middle between the other two. This is exactly how the insertion sort works. It goes through an array step by step and removes thereby from the unsorted input sequence an element and puts it then at the accordingly right place again - "Sort by insert". The remaining elements of the array must then turn around moved behind the newly inserted value become.

### example

But let's just take a look at a more detailed example.

This array should be sorted in ascending order of size:



The first number is always specified directly as the sorted area in the Insertion Sort. The rest of the list is considered unsorted. So we can look directly at 7. The 7 should now be inserted in the correct place in the sorted area. The 7 is larger than the 4 and is therefore already correctly classified. Now we continue with 5. The 5 is smaller than the 7 and is therefore classified before the 7, but before that it is checked whether it is larger than 4. That is the case and therefore it is entered directly between 4 and 7.



It continues with the fourth position. The 3 is not only smaller than the 7, but also smaller than the 5 and the 4. So the 3 is put in the first place and the other numbers are shifted to the right accordingly.



Then it continues with the number 9. The 9 is the largest number among the numbers already sorted and is accordingly in its correct position for the time being. Therefore, we can continue with the 1, which turns out to be the smallest number and is sorted accordingly to the first position.



At the end we are still missing the 2. The 2 is smaller than the 9 and must be inserted accordingly in the correct position in the rest of the list. So between the 1 and the 3. At the end the sorted list looks like this:



Finished!

### algorithm

But how can the insertion sort be represented as pseudocode?

Let's take a look at how to do the general Describe the algorithm can.

First a list is passed. It always starts with the first element, so this represents the sorted area of ​​the list and the rest of the array is considered the unsorted area. The condition is that as long as the unsorted area has elements, the first element from the unsorted area should always be placed in the correct position in the sorted area.

At the end you get a sorted list.

### Insertion Sort Pseudocode

So that about the general algorithm. Now we are building ours on this knowledge Insertion Sort Pseudocode on. So, given a list, in this case we will simply call it l. Next we define the sorted area with the first element a. Then we designate the first element in the unsorted area as b. The current b is stored in our buffer memory tmp as long as the unsorted area has elements. We remove the element from the sorted area and put it from the cache in the sorted area.

### Insertion Sort Structogram

Based on the pseudocode, an associated Structogram (Nassi-Schneidermann diagram) illustrates:

### Insertion Sort Java

Below is a simulated Insertion Sort Java implementation of the general sort function.

### Insertion Sort Term

The insertion sort runtime depends on the number of shifts required, i.e. also on the arrangement of elements in the unsorted area. Both the average average case and the worst case have the complexity O (n2). The worst case occurs when the algorithm is sorted in descending order, for example, and it is then to be sorted in ascending order. In the event that the list is sorted from the start, the complexity is linear O (n), which is the best case. The sorting algorithm is even better than that Quicksort, Mergesort, Heapsort , etc.

The Insertion Sort Term again as overview:

• Worst case / worst case: • Average case / average number of comparisons: • Best case / best scenario: If the binary search is used for the sorting process, the number of comparisons and shifts due to sorting operations in the worst case can be estimated as follows: ### complexity

In the event that the array is already "almost sorted", i.e. each of the elements is almost in the right place, the runtime can also be O (n). In this case, and also in the best-case scenario, the insertion sort is pretty quick. However, in the other cases very slowly, which is why the sorting process only for smaller amounts of data or for inserting additional elements in an already ordered list.

In addition, it can be said that the sorting algorithm scores points in terms of storage space complexity. Since the algorithm is medium in-placeMethod works, no additional storage space is required.