From 6a0d858f046ceb1b86bcefe0cc5f210ed07a8469 Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Wed, 4 Jul 2018 21:08:19 +0300 Subject: [PATCH 1/7] insertion-sort-chapter --- .../insertion_sort/insertion_sort.md | 50 +++++++++++++++++++ .../insertion_sort/python/insertion_sort.py | 24 +++++++++ 2 files changed, 74 insertions(+) create mode 100644 chapters/sorting_searching/insertion_sort/insertion_sort.md create mode 100644 chapters/sorting_searching/insertion_sort/python/insertion_sort.py diff --git a/chapters/sorting_searching/insertion_sort/insertion_sort.md b/chapters/sorting_searching/insertion_sort/insertion_sort.md new file mode 100644 index 000000000..1feaeddc0 --- /dev/null +++ b/chapters/sorting_searching/insertion_sort/insertion_sort.md @@ -0,0 +1,50 @@ +# Insertion Sort +Insertion sort is the first algorithm usually taught in an introductory algorithms course since it is simple to understand as it is used regulary to sort decks of cards. Insertion sort has one important rule which is helpful to keep in mind, in the j-th iteration the subarray A[1...j-1] is sorted, it means that all the elements which the algorithm iterated over, are sorted. + +The algorithm starts from the second element in the array, in this point the subarray A[1...j-1], which is A[1], is obviously sorted, since it holds only one element. + +{% method %} +{% sample lang="c" %} +[import:5-21, lang:"c"](code/c/insertion_sort.c) +{% sample lang="py" %} +[import:5-6, lang:"python"](code/python/insertion_sort.py) +{% endmethod %} + +In each iteration the current , j-th, element of A, A[j], is inserted into the correct position in the subarray A[1...j], moving each element that is bigger that A[j] one position to the right, leaving room for A[j]. + +{% method %} +{% sample lang="py" %} +[import:10-15, lang:"python"](code/python/insertion_sort.py) +{% endmethod %} + + +The worst input for insertion sort is the reverse sorted array, since in each iteration of the inner while loop will iterate over the entire A[1...j-1] array, moving each element one position to the right, this is why it has time complexity of $$\mathcal{O}(n^2)$$. + +And the full code: +{% method %} +{% sample lang="py" %} +[import:1-24, lang:"python"](code/python/insertion_sort.py) +{% endmethod %} + + +$$ +\newcommand{\d}{\mathrm{d}} +\newcommand{\bff}{\boldsymbol{f}} +\newcommand{\bfg}{\boldsymbol{g}} +\newcommand{\bfp}{\boldsymbol{p}} +\newcommand{\bfq}{\boldsymbol{q}} +\newcommand{\bfx}{\boldsymbol{x}} +\newcommand{\bfu}{\boldsymbol{u}} +\newcommand{\bfv}{\boldsymbol{v}} +\newcommand{\bfA}{\boldsymbol{A}} +\newcommand{\bfB}{\boldsymbol{B}} +\newcommand{\bfC}{\boldsymbol{C}} +\newcommand{\bfM}{\boldsymbol{M}} +\newcommand{\bfJ}{\boldsymbol{J}} +\newcommand{\bfR}{\boldsymbol{R}} +\newcommand{\bfT}{\boldsymbol{T}} +\newcommand{\bfomega}{\boldsymbol{\omega}} +\newcommand{\bftau}{\boldsymbol{\tau}} +$$ \ No newline at end of file diff --git a/chapters/sorting_searching/insertion_sort/python/insertion_sort.py b/chapters/sorting_searching/insertion_sort/python/insertion_sort.py new file mode 100644 index 000000000..37717cf06 --- /dev/null +++ b/chapters/sorting_searching/insertion_sort/python/insertion_sort.py @@ -0,0 +1,24 @@ +def insertion_sort(array): + + array_length = len(array) + # loop through array[1:n], array[0] is already sorted + for j in range(1, array_length): + current_element = array[j] + + # Place the j-th element to the correct position in the sub array array[0...j] + # Keeping array[0...j] sorted + i = j - 1 + while((i >= 0) and (array[i] > current_element)): + array[i + 1] = array[i] + i -= 1 + + array[i + 1] = current_element + + return array + + +if __name__ == '__main__': + array = [10, 1, 3, 4, 7, 2, 5, 9, 6, 8] + sorted_array = insertion_sort(array) + + print("This is the array of sorting: " + str(sorted_array)) \ No newline at end of file From d479cecfce121a205f648e8a57e3c19c3175d26c Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Wed, 4 Jul 2018 21:37:19 +0300 Subject: [PATCH 2/7] Added a c implementation --- .../insertion_sort/c/insertion_sort.c | 44 +++++++++++++++++++ .../insertion_sort/insertion_sort.md | 6 ++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 chapters/sorting_searching/insertion_sort/c/insertion_sort.c diff --git a/chapters/sorting_searching/insertion_sort/c/insertion_sort.c b/chapters/sorting_searching/insertion_sort/c/insertion_sort.c new file mode 100644 index 000000000..f96bf1f7a --- /dev/null +++ b/chapters/sorting_searching/insertion_sort/c/insertion_sort.c @@ -0,0 +1,44 @@ +#include + +void print_array(int *array, int arr_len){ + int i = 0; + putchar('['); + + for(i = 0; i < arr_len; i++){ + if(i < (arr_len - 1)) + printf("%d, ", *(array + i)); + else + printf("%d]\n", *(array + i)); + } +} + +/*Insertion sort sorts the array inplace*/ +void insertion_sort(int *array, int arr_len){ + int j, i; + int current_element; + + /*loop through array[1:n], array[0] is already sorted*/ + for(j = 1; j < arr_len; j++){ + current_element = *(array + j); + + /*Place the j-th element to the correct position in the sub array array[0...j] + Keeping array[0...j] sorted*/ + i = j - 1; + while((i >= 0) && (*(array + i) > current_element)){ + *(array + i + 1) = *(array + i); + i -= 1; + } + *(array + i + 1) = current_element; + } +} + +int main(){ + int arr_len = 10; + int array[] = {10, 1, 3, 4, 7, 2, 5, 9, 6, 8}; + + printf("This is the array after sorting: "); + insertion_sort(array, arr_len); + print_array(array, arr_len); + + return 0; +} diff --git a/chapters/sorting_searching/insertion_sort/insertion_sort.md b/chapters/sorting_searching/insertion_sort/insertion_sort.md index 1feaeddc0..c6f4e7c48 100644 --- a/chapters/sorting_searching/insertion_sort/insertion_sort.md +++ b/chapters/sorting_searching/insertion_sort/insertion_sort.md @@ -5,7 +5,7 @@ The algorithm starts from the second element in the array, in this point the sub {% method %} {% sample lang="c" %} -[import:5-21, lang:"c"](code/c/insertion_sort.c) +[import:21-22, lang:"c"](code/c/insertion_sort.c) {% sample lang="py" %} [import:5-6, lang:"python"](code/python/insertion_sort.py) {% endmethod %} @@ -13,6 +13,8 @@ The algorithm starts from the second element in the array, in this point the sub In each iteration the current , j-th, element of A, A[j], is inserted into the correct position in the subarray A[1...j], moving each element that is bigger that A[j] one position to the right, leaving room for A[j]. {% method %} +{% sample lang="c" %} +[import:26-31, lang:"c"](code/c/insertion_sort.c) {% sample lang="py" %} [import:10-15, lang:"python"](code/python/insertion_sort.py) {% endmethod %} @@ -22,6 +24,8 @@ The worst input for insertion sort is the reverse sorted array, since in each it And the full code: {% method %} +{% sample lang="c" %} +[import:15-44, lang:"c"](code/python/insertion_sort.c) {% sample lang="py" %} [import:1-24, lang:"python"](code/python/insertion_sort.py) {% endmethod %} From 9fce7699daccaad42f7c0ca5947fcb2961e73a40 Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Wed, 4 Jul 2018 21:47:57 +0300 Subject: [PATCH 3/7] fixed misspellings in md --- chapters/sorting_searching/insertion_sort/insertion_sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapters/sorting_searching/insertion_sort/insertion_sort.md b/chapters/sorting_searching/insertion_sort/insertion_sort.md index c6f4e7c48..08ac882b2 100644 --- a/chapters/sorting_searching/insertion_sort/insertion_sort.md +++ b/chapters/sorting_searching/insertion_sort/insertion_sort.md @@ -10,7 +10,7 @@ The algorithm starts from the second element in the array, in this point the sub [import:5-6, lang:"python"](code/python/insertion_sort.py) {% endmethod %} -In each iteration the current , j-th, element of A, A[j], is inserted into the correct position in the subarray A[1...j], moving each element that is bigger that A[j] one position to the right, leaving room for A[j]. +In each iteration the current , j-th, element of A, A[j], is inserted into the correct position in the subarray A[1...j], moving each element that is bigger than A[j] one position to the right, leaving room for A[j]. {% method %} {% sample lang="c" %} From 598f4ba02d36f72b086f7ab8e5debe642f4791ca Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Wed, 4 Jul 2018 22:10:43 +0300 Subject: [PATCH 4/7] changed to array subscription and C99 --- .../insertion_sort/c/insertion_sort.c | 22 ++++---- .../insertion_sort/sorting_searching.md | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 chapters/sorting_searching/insertion_sort/sorting_searching.md diff --git a/chapters/sorting_searching/insertion_sort/c/insertion_sort.c b/chapters/sorting_searching/insertion_sort/c/insertion_sort.c index f96bf1f7a..7421317f5 100644 --- a/chapters/sorting_searching/insertion_sort/c/insertion_sort.c +++ b/chapters/sorting_searching/insertion_sort/c/insertion_sort.c @@ -1,34 +1,32 @@ #include void print_array(int *array, int arr_len){ - int i = 0; - putchar('['); - for(i = 0; i < arr_len; i++){ + putchar('['); + for(int i = 0; i < arr_len; i++){ if(i < (arr_len - 1)) - printf("%d, ", *(array + i)); + printf("%d, ", array[i]); else - printf("%d]\n", *(array + i)); + printf("%d]\n", array[i]); } } /*Insertion sort sorts the array inplace*/ void insertion_sort(int *array, int arr_len){ - int j, i; - int current_element; + int i; /*loop through array[1:n], array[0] is already sorted*/ - for(j = 1; j < arr_len; j++){ - current_element = *(array + j); + for(int j = 1; j < arr_len; ++j){ + int current_element = array[j]; /*Place the j-th element to the correct position in the sub array array[0...j] Keeping array[0...j] sorted*/ i = j - 1; - while((i >= 0) && (*(array + i) > current_element)){ - *(array + i + 1) = *(array + i); + while((i >= 0) && (array[i] > current_element)){ + array[i + 1] = array[i]; i -= 1; } - *(array + i + 1) = current_element; + array[i + 1] = current_element; } } diff --git a/chapters/sorting_searching/insertion_sort/sorting_searching.md b/chapters/sorting_searching/insertion_sort/sorting_searching.md new file mode 100644 index 000000000..54292da82 --- /dev/null +++ b/chapters/sorting_searching/insertion_sort/sorting_searching.md @@ -0,0 +1,54 @@ +# Insertion Sort +Insertion sort is the first algorithm usually taught in an introductory algorithms course since it is simple to understand as it is used regulary to sort decks of cards. Insertion sort has one important rule which is helpful to keep in mind, in the j-th iteration the subarray A[1...j-1] is sorted, it means that all the elements which the algorithm iterated over, are sorted. + +The algorithm starts from the second element in the array, in this point the subarray A[1...j-1], which is A[1], is obviously sorted, since it holds only one element. + +{% method %} +{% sample lang="c" %} +[import:19-20, lang:"c"](code/c/insertion_sort.c) +{% sample lang="py" %} +[import:5-6, lang:"python"](code/python/insertion_sort.py) +{% endmethod %} + +In each iteration the current , j-th, element of A, A[j], is inserted into the correct position in the subarray A[1...j], moving each element that is bigger than A[j] one position to the right, leaving room for A[j]. + +{% method %} +{% sample lang="c" %} +[import:24-29, lang:"c"](code/c/insertion_sort.c) +{% sample lang="py" %} +[import:10-15, lang:"python"](code/python/insertion_sort.py) +{% endmethod %} + + +The worst input for insertion sort is the reverse sorted array, since in each iteration of the inner while loop will iterate over the entire A[1...j-1] array, moving each element one position to the right, this is why it has time complexity of $$\mathcal{O}(n^2)$$. + +And the full code: +{% method %} +{% sample lang="c" %} +[import:14-42, lang:"c"](code/python/insertion_sort.c) +{% sample lang="py" %} +[import:1-24, lang:"python"](code/python/insertion_sort.py) +{% endmethod %} + + +$$ +\newcommand{\d}{\mathrm{d}} +\newcommand{\bff}{\boldsymbol{f}} +\newcommand{\bfg}{\boldsymbol{g}} +\newcommand{\bfp}{\boldsymbol{p}} +\newcommand{\bfq}{\boldsymbol{q}} +\newcommand{\bfx}{\boldsymbol{x}} +\newcommand{\bfu}{\boldsymbol{u}} +\newcommand{\bfv}{\boldsymbol{v}} +\newcommand{\bfA}{\boldsymbol{A}} +\newcommand{\bfB}{\boldsymbol{B}} +\newcommand{\bfC}{\boldsymbol{C}} +\newcommand{\bfM}{\boldsymbol{M}} +\newcommand{\bfJ}{\boldsymbol{J}} +\newcommand{\bfR}{\boldsymbol{R}} +\newcommand{\bfT}{\boldsymbol{T}} +\newcommand{\bfomega}{\boldsymbol{\omega}} +\newcommand{\bftau}{\boldsymbol{\tau}} +$$ \ No newline at end of file From f20099ed5ca55fa06e8c92d4f721c4a4850df083 Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Wed, 4 Jul 2018 22:15:06 +0300 Subject: [PATCH 5/7] moved the i decleration into the for loop --- chapters/sorting_searching/insertion_sort/c/insertion_sort.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chapters/sorting_searching/insertion_sort/c/insertion_sort.c b/chapters/sorting_searching/insertion_sort/c/insertion_sort.c index 7421317f5..2d06469f9 100644 --- a/chapters/sorting_searching/insertion_sort/c/insertion_sort.c +++ b/chapters/sorting_searching/insertion_sort/c/insertion_sort.c @@ -13,7 +13,6 @@ void print_array(int *array, int arr_len){ /*Insertion sort sorts the array inplace*/ void insertion_sort(int *array, int arr_len){ - int i; /*loop through array[1:n], array[0] is already sorted*/ for(int j = 1; j < arr_len; ++j){ @@ -21,7 +20,7 @@ void insertion_sort(int *array, int arr_len){ /*Place the j-th element to the correct position in the sub array array[0...j] Keeping array[0...j] sorted*/ - i = j - 1; + int i = j - 1; while((i >= 0) && (array[i] > current_element)){ array[i + 1] = array[i]; i -= 1; From 631e2f839771e5d41d0b741488fee3bcf3caf9a7 Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Wed, 4 Jul 2018 22:18:55 +0300 Subject: [PATCH 6/7] changed md to fit c code --- chapters/sorting_searching/insertion_sort/insertion_sort.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chapters/sorting_searching/insertion_sort/insertion_sort.md b/chapters/sorting_searching/insertion_sort/insertion_sort.md index 08ac882b2..1461a6892 100644 --- a/chapters/sorting_searching/insertion_sort/insertion_sort.md +++ b/chapters/sorting_searching/insertion_sort/insertion_sort.md @@ -5,7 +5,7 @@ The algorithm starts from the second element in the array, in this point the sub {% method %} {% sample lang="c" %} -[import:21-22, lang:"c"](code/c/insertion_sort.c) +[import:18-19, lang:"c"](code/c/insertion_sort.c) {% sample lang="py" %} [import:5-6, lang:"python"](code/python/insertion_sort.py) {% endmethod %} @@ -14,7 +14,7 @@ In each iteration the current , j-th, element of A, A[j], is inserted into the c {% method %} {% sample lang="c" %} -[import:26-31, lang:"c"](code/c/insertion_sort.c) +[import:23-28, lang:"c"](code/c/insertion_sort.c) {% sample lang="py" %} [import:10-15, lang:"python"](code/python/insertion_sort.py) {% endmethod %} @@ -25,7 +25,7 @@ The worst input for insertion sort is the reverse sorted array, since in each it And the full code: {% method %} {% sample lang="c" %} -[import:15-44, lang:"c"](code/python/insertion_sort.c) +[import:14-41, lang:"c"](code/python/insertion_sort.c) {% sample lang="py" %} [import:1-24, lang:"python"](code/python/insertion_sort.py) {% endmethod %} From 0ffccccbc24db0386f74177068f8fd742e74ce8a Mon Sep 17 00:00:00 2001 From: GuyPozner Date: Thu, 5 Jul 2018 09:11:06 +0300 Subject: [PATCH 7/7] deletion of searching_sorting.md in wrong dir --- .../insertion_sort/sorting_searching.md | 54 ------------------- 1 file changed, 54 deletions(-) delete mode 100644 chapters/sorting_searching/insertion_sort/sorting_searching.md diff --git a/chapters/sorting_searching/insertion_sort/sorting_searching.md b/chapters/sorting_searching/insertion_sort/sorting_searching.md deleted file mode 100644 index 54292da82..000000000 --- a/chapters/sorting_searching/insertion_sort/sorting_searching.md +++ /dev/null @@ -1,54 +0,0 @@ -# Insertion Sort -Insertion sort is the first algorithm usually taught in an introductory algorithms course since it is simple to understand as it is used regulary to sort decks of cards. Insertion sort has one important rule which is helpful to keep in mind, in the j-th iteration the subarray A[1...j-1] is sorted, it means that all the elements which the algorithm iterated over, are sorted. - -The algorithm starts from the second element in the array, in this point the subarray A[1...j-1], which is A[1], is obviously sorted, since it holds only one element. - -{% method %} -{% sample lang="c" %} -[import:19-20, lang:"c"](code/c/insertion_sort.c) -{% sample lang="py" %} -[import:5-6, lang:"python"](code/python/insertion_sort.py) -{% endmethod %} - -In each iteration the current , j-th, element of A, A[j], is inserted into the correct position in the subarray A[1...j], moving each element that is bigger than A[j] one position to the right, leaving room for A[j]. - -{% method %} -{% sample lang="c" %} -[import:24-29, lang:"c"](code/c/insertion_sort.c) -{% sample lang="py" %} -[import:10-15, lang:"python"](code/python/insertion_sort.py) -{% endmethod %} - - -The worst input for insertion sort is the reverse sorted array, since in each iteration of the inner while loop will iterate over the entire A[1...j-1] array, moving each element one position to the right, this is why it has time complexity of $$\mathcal{O}(n^2)$$. - -And the full code: -{% method %} -{% sample lang="c" %} -[import:14-42, lang:"c"](code/python/insertion_sort.c) -{% sample lang="py" %} -[import:1-24, lang:"python"](code/python/insertion_sort.py) -{% endmethod %} - - -$$ -\newcommand{\d}{\mathrm{d}} -\newcommand{\bff}{\boldsymbol{f}} -\newcommand{\bfg}{\boldsymbol{g}} -\newcommand{\bfp}{\boldsymbol{p}} -\newcommand{\bfq}{\boldsymbol{q}} -\newcommand{\bfx}{\boldsymbol{x}} -\newcommand{\bfu}{\boldsymbol{u}} -\newcommand{\bfv}{\boldsymbol{v}} -\newcommand{\bfA}{\boldsymbol{A}} -\newcommand{\bfB}{\boldsymbol{B}} -\newcommand{\bfC}{\boldsymbol{C}} -\newcommand{\bfM}{\boldsymbol{M}} -\newcommand{\bfJ}{\boldsymbol{J}} -\newcommand{\bfR}{\boldsymbol{R}} -\newcommand{\bfT}{\boldsymbol{T}} -\newcommand{\bfomega}{\boldsymbol{\omega}} -\newcommand{\bftau}{\boldsymbol{\tau}} -$$ \ No newline at end of file