From 3d0db49a09d94bb9abe847fa77dcf5100b90e1dd Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Wed, 1 Jun 2016 19:13:33 +0530 Subject: [PATCH 1/5] added insertion-sort.md --- insertion-sort.md | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 insertion-sort.md diff --git a/insertion-sort.md b/insertion-sort.md new file mode 100644 index 000000000..885fa1e01 --- /dev/null +++ b/insertion-sort.md @@ -0,0 +1,113 @@ +# Insertion Sort + +Insertion sort is a [comparision based sorting](https://en.wikipedia.org/wiki/Comparison_sort). A sorting algorithm is comparison based, if it uses comparison operators (such as `less than` and `greated than`) to find the order between two numbers. + +In this sorting technique, we always maintain a sorted sublist in lower position of list and then we take one element from the rest of list and insert it at it's correct place. We does so till all elements are inserted into sublist. For example, while playing cards we sort cards in our hand. Starting from left and moving to right, we keep on inserting the card at it's right place till end. + +## Example + +![Insertion Sort](https://cloud.githubusercontent.com/assets/13117482/15633518/04ca4468-25cc-11e6-96af-feb395b456e0.png) + +In the above example, `grey shaded` sublist is always sorted. Please note that in the beginning, sublist contains ony one element, and *trivially* sorted. Then at each step we are inserting leftmost element of `white shaded` sublist at it's correct position. + +Hence, we have sorted the complete list in this way. + +## Algorithm + +``` + +Loop for i=0 to N-1: +* Pick element array[i] and insert it into sorted sublist array[0...i-1] + +``` + +## Complexity + +``` + +Space complexity: O(1) // Auxillary/temporary space is used. + +Time complexity: O(n*n) // Two nested for loops are used. + +``` + +## C++ Implementation + +```cpp + +// Function to sort an array using insertion sort +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i-1; + + /* Move elements of arr[0..i-1], that are greater than key, + to one position ahead of their current position */ + while (j >= 0 && arr[j] > key) + { + arr[j+1] = arr[j]; + j = j-1; + } + arr[j+1] = key; // place element key at it's correct place + } +} + +int main() +{ + // array to be sorted + int arr[5] = {12, 11, 13, 5, 6}; + + // call the insertion sort + insertionSort(arr, 5); + + // prints sorted array i.e. 5 6 11 12 13 + for(int i=0; i<5; i++) + std::cout << arr[i] << " "; + return 0; +} + +``` + +:rocket: [Run Code](https://repl.it/CWZq) + +## Python Implementation + +```python + +# Function to perform insertion sort +def insertionSort(arr): + # Traverse through array + for i in range(1, len(arr)): + key = arr[i] + # Move elements of arr[0..i-1], that are greater than key, + # to one position ahead of their current position + j = i-1 + while j>=0 and key < arr[j] : + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key # place element key at it's correct place + +# array to be sorted +arr = [12, 11, 13, 5, 6] +# call the insertion sort +insertionSort(arr) +# prints sorted array i.e. 5 6 11 12 13 +for i in range(len(arr)): + print(arr[i],end = ' ') + +``` + +:rocket: [Run Code](https://repl.it/CWZi) + +## Advantages + +1. Efficient for small set of data and data set that are almost sorted. +2. Simply implemented. +3. Mostly better than bubble sort and selection sort & generally used with merge sort. + +## Disadvantages + +1. It's less efficient on large set of data than merge sort, heap sort and quick sort. From 6fa263e8564f716f4c8c210bf10c5f1533bcbee7 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Wed, 1 Jun 2016 19:44:21 +0530 Subject: [PATCH 2/5] updated filename --- insertion-sort.md => Algorithms-Insertion-Sort.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename insertion-sort.md => Algorithms-Insertion-Sort.md (100%) diff --git a/insertion-sort.md b/Algorithms-Insertion-Sort.md similarity index 100% rename from insertion-sort.md rename to Algorithms-Insertion-Sort.md From 8b705d61f56fdb7ba0e7ae9fec6e2d1ede10af0c Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 17:47:44 +0530 Subject: [PATCH 3/5] Some implementation --- Algorithms-Insertion-Sort.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Algorithms-Insertion-Sort.md b/Algorithms-Insertion-Sort.md index 885fa1e01..cfd269348 100644 --- a/Algorithms-Insertion-Sort.md +++ b/Algorithms-Insertion-Sort.md @@ -1,14 +1,14 @@ # Insertion Sort -Insertion sort is a [comparision based sorting](https://en.wikipedia.org/wiki/Comparison_sort). A sorting algorithm is comparison based, if it uses comparison operators (such as `less than` and `greated than`) to find the order between two numbers. +Insertion sort is a [*comparison* based sorting](https://en.wikipedia.org/wiki/Comparison_sort). A sorting algorithm is comparison based, if it uses comparison operators (such as `less than` and `greated than`) to find the order between two numbers. -In this sorting technique, we always maintain a sorted sublist in lower position of list and then we take one element from the rest of list and insert it at it's correct place. We does so till all elements are inserted into sublist. For example, while playing cards we sort cards in our hand. Starting from left and moving to right, we keep on inserting the card at it's right place till end. +In this sorting technique, we always maintain a sorted sublist in lower position of list and then we take one element from the rest of list and insert it at it's correct place. We does so untill all of the elements are inserted into sublist. For example, while playing cards we sort cards in our hand. Starting from left and moving to right, we keep on inserting the card at it's right place till end. ## Example ![Insertion Sort](https://cloud.githubusercontent.com/assets/13117482/15633518/04ca4468-25cc-11e6-96af-feb395b456e0.png) -In the above example, `grey shaded` sublist is always sorted. Please note that in the beginning, sublist contains ony one element, and *trivially* sorted. Then at each step we are inserting leftmost element of `white shaded` sublist at it's correct position. +In the above example, the `grey shaded` sublist is always sorted. Please note that in the beginning, sublist contains ony one element, and *trivially* sorted. Then at each step we are inserting the leftmost element of `white shaded` sublist at it's correct position. Hence, we have sorted the complete list in this way. From 6e47e0a3ef54bab2509d6de186d1ca7ad1a06a99 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 18:04:01 +0530 Subject: [PATCH 4/5] Update --- Algorithms-Insertion-Sort.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Algorithms-Insertion-Sort.md b/Algorithms-Insertion-Sort.md index cfd269348..8c14378ea 100644 --- a/Algorithms-Insertion-Sort.md +++ b/Algorithms-Insertion-Sort.md @@ -15,25 +15,21 @@ Hence, we have sorted the complete list in this way. ## Algorithm ``` - Loop for i=0 to N-1: * Pick element array[i] and insert it into sorted sublist array[0...i-1] - ``` ## Complexity ``` - Space complexity: O(1) // Auxillary/temporary space is used. Time complexity: O(n*n) // Two nested for loops are used. - ``` ## C++ Implementation -```cpp +```c++ // Function to sort an array using insertion sort void insertionSort(int arr[], int n) @@ -68,7 +64,6 @@ int main() std::cout << arr[i] << " "; return 0; } - ``` :rocket: [Run Code](https://repl.it/CWZq) @@ -97,7 +92,6 @@ insertionSort(arr) # prints sorted array i.e. 5 6 11 12 13 for i in range(len(arr)): print(arr[i],end = ' ') - ``` :rocket: [Run Code](https://repl.it/CWZi) From 2aec179b770b728870f8549693278b069f9c752f Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 18:05:18 +0530 Subject: [PATCH 5/5] Update --- Algorithms-Insertion-Sort.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/Algorithms-Insertion-Sort.md b/Algorithms-Insertion-Sort.md index 8c14378ea..1d9f00507 100644 --- a/Algorithms-Insertion-Sort.md +++ b/Algorithms-Insertion-Sort.md @@ -30,7 +30,6 @@ Time complexity: O(n*n) // Two nested for loops are used. ## C++ Implementation ```c++ - // Function to sort an array using insertion sort void insertionSort(int arr[], int n) { @@ -71,7 +70,6 @@ int main() ## Python Implementation ```python - # Function to perform insertion sort def insertionSort(arr): # Traverse through array