From 2022c45e606ddc6996716a90b2d080bbc43a18c8 Mon Sep 17 00:00:00 2001 From: James Su Date: Wed, 6 Nov 2019 16:41:26 +0000 Subject: [PATCH] Add new file --- .../ICS4U1/Sorting Methods/Insertion Sort.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Grade 10/Computer Science/ICS4U1/Sorting Methods/Insertion Sort.md diff --git a/Grade 10/Computer Science/ICS4U1/Sorting Methods/Insertion Sort.md b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Insertion Sort.md new file mode 100644 index 0000000..c5f5013 --- /dev/null +++ b/Grade 10/Computer Science/ICS4U1/Sorting Methods/Insertion Sort.md @@ -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 +```