1
0
mirror of https://gitlab.com/magicalsoup/Highschool.git synced 2025-01-23 16:11:46 -05:00

Add new file

This commit is contained in:
James Su 2019-11-06 16:41:26 +00:00
parent 925e8a34c2
commit 2022c45e60

View File

@ -0,0 +1,31 @@
# Inssertion sort
## What is Insertion sort
- a sorting algorithm
- dividng an array into sroted and unsorted sections
- removes elements in idividually from un unosrted section of the list and inserts it into its correct position in a new sorted seqeuence
## Algorithm
- Set a key for the sections after the 1st element
- repeat the following
- select first unsorted element as the key
- move sorted elements to the right to create space
- shift unsorted elemnet into correct position
- advance the key to the right by 1 element
- Tests first unsorted element (key) to last element of sorted section
- If key is larger, insertion sort leaves the element in place and hte next index's element becomes the key
- Else it finds the correct position within the sorted list
- Duplicate the larger element in to one ahead of its current index each time the key is tested
- By looping this, it makes space and inserts the key into that correct position
## Pseudo0code
```
for x = 1 : n
key = list[x]
y = x-1
while y >= 0 && key < list[y]
insert list[x] into the sorted sequence list[1 .. x-1]
list[y+1] = list[y]
y--
list[y+1] = key
```