From 9632cca17948d2c40e645df4da45e3cb49f62a8a Mon Sep 17 00:00:00 2001 From: Debdut Goswami Date: Mon, 27 Apr 2020 02:56:47 +0530 Subject: [PATCH 1/3] Insertion Sort #2148 --- Insertion_Sort/README.md | 130 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Insertion_Sort/README.md diff --git a/Insertion_Sort/README.md b/Insertion_Sort/README.md new file mode 100644 index 0000000000..d1add4837f --- /dev/null +++ b/Insertion_Sort/README.md @@ -0,0 +1,130 @@ +# INSERTION SORT + +[Insertion Sort](https://en.wikipedia.org/wiki/Insertion_Sort) is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. + +## EXAMPLE + +Given below is an unsorted array. `Insertion sort` takes O(n) time in Best Case and Ο(n2) time for Average and Worst Case. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/unsorted_array.jpg) + +`Insertion sort` compares the first two elements + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_1.jpg) + +It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_2.jpg) + +Insertion sort moves ahead and compares 33 with 27. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_3.jpg) + +And finds that 33 is not in the correct position. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_4.jpg) + +It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after swapping. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_5.jpg) + +By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_6.jpg) + +These values are not in a sorted order. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_7.jpg) + +So we swap them. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_8.jpg) + +However, swapping makes 27 and 10 unsorted. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_9.jpg) + +Hence, we swap them too. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_10.jpg) + +Again we find 14 and 10 in an unsorted order. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_11.jpg) + +We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items. + +![Insertion Sort](https://www.tutorialspoint.com/data_structures_algorithms/images/insertion_sort_12.jpg) + +This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see some programming aspects of insertion sort. + +## ALGORITHM + +``` +Step 1 − If it is the first element, it is already sorted. return 1; +Step 2 − Pick next element +Step 3 − Compare with all elements in the sorted sub-list +Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted +Step 5 − Insert the value +Step 6 − Repeat until list is sorted +``` + +## PSEUDOCODE + +Pseudocode of InsertionSort algorithm can be expressed as − + +``` +procedure insertionSort( A : array of items ) + int holePosition + int valueToInsert + + for i = 1 to length(A) inclusive do: + + /* select value to be inserted */ + valueToInsert = A[i] + holePosition = i + + /*locate hole position for the element to be inserted */ + + while holePosition > 0 and A[holePosition-1] > valueToInsert do: + A[holePosition] = A[holePosition-1] + holePosition = holePosition -1 + end while + + /* insert the number at hole position */ + A[holePosition] = valueToInsert + + end for + +end procedure +``` + +## COMPLEXITY + +**Time complexity** + +_Best Case_: O(n) + +_Average and Worst Case_: О(n2) + +where _n_ is the number of items being sorted. + +**Space complexity** - O(1), due to auxillary space only. + +## Implementation + +- [C Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.c) + +- [CoffeeScript Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.coffee) + +- [C++ Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.cpp) + +- [C# Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.cs) + +- [Java Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.java) + +- [JavaScript Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.js) + +- [PHP Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.php) + +- [Python Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Insertion_Sort/Insertion_Sort.py) From 8fdf9bd40c8e1874eed8934fd4310eadff65297c Mon Sep 17 00:00:00 2001 From: Debdut Goswami Date: Mon, 11 May 2020 14:22:10 +0530 Subject: [PATCH 2/3] Readme LIS --- Longest_Increasing_Subsequence/README.md | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Longest_Increasing_Subsequence/README.md diff --git a/Longest_Increasing_Subsequence/README.md b/Longest_Increasing_Subsequence/README.md new file mode 100644 index 0000000000..e4c8639d9d --- /dev/null +++ b/Longest_Increasing_Subsequence/README.md @@ -0,0 +1,55 @@ +# Longest Increasing Subsequence + +The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for `{10, 22, 9, 33, 21, 50, 41, 60, 80}` is `6` and LIS is `{10, 22, 33, 50, 60, 80}`. + +![longest-increasing-subsequence](https://media.geeksforgeeks.org/wp-content/cdn-uploads/Longest-Increasing-Subsequence.png) + +## Examples + +``` +Input : arr[] = {3, 10, 2, 1, 20} +Output : Length of LIS = 3 +The longest increasing subsequence is 3, 10, 20 + +Input : arr[] = {3, 2} +Output : Length of LIS = 1 +The longest increasing subsequences are {3} and {2} + +Input : arr[] = {50, 3, 10, 7, 40, 80} +Output : Length of LIS = 4 +The longest increasing subsequence is {3, 7, 40, 80} +``` + +## Recursive Approach + +``` +Let arr[0..n-1] be the input array and L(i) be the length of the LIS ending at index i such that arr[i] is the last element of the LIS. + +Then, L(i) can be recursively written as: + +L(i) = 1 + max( L(j) ) where 0 < j < i and arr[j] < arr[i]; or +L(i) = 1, if no such j exists. + +To find the LIS for a given array, we need to return max(L(i)) where 0 < i < n. + +Thus, we see the LIS problem satisfies the optimal substructure property as the main problem can be solved using solutions to subproblems. +``` + +## Overlapping Subproblems: + +Considering the above implementation, following is recursion tree for an array of size 4. lis(n) gives us the length of LIS for arr[]. + +``` + lis(4) + / | \ + lis(3) lis(2) lis(1) + / \ | + lis(2) lis(1) lis(1) + / +lis(1) +``` + + +# Time Complexity + +O ( n 2 ) \ No newline at end of file From 3b486bec01c7b68a715fa5c745e586d5fd738b18 Mon Sep 17 00:00:00 2001 From: Debdut Goswami Date: Mon, 18 May 2020 17:56:28 +0530 Subject: [PATCH 3/3] LIS Pseudo code --- Longest_Increasing_Subsequence/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Longest_Increasing_Subsequence/README.md b/Longest_Increasing_Subsequence/README.md index e4c8639d9d..3d1c08af7d 100644 --- a/Longest_Increasing_Subsequence/README.md +++ b/Longest_Increasing_Subsequence/README.md @@ -35,6 +35,24 @@ To find the LIS for a given array, we need to return max(L(i)) where 0 < i < n. Thus, we see the LIS problem satisfies the optimal substructure property as the main problem can be solved using solutions to subproblems. ``` +## Pseudo Code + +``` +LIS(A[1..n]): + Array L[1..n] + (* L[i] = value of LIS ending(A[1..i]) *) + for i = 1 to n do + L[i] = 1 + for j = 1 to i − 1 do + if (A[j] < A[i]) do + L[i] = max(L[i], 1 + L[j]) + return L + +MAIN(A[1..n]): + L = LIS(A[1..n]) + return the maximum value in L +``` + ## Overlapping Subproblems: Considering the above implementation, following is recursion tree for an array of size 4. lis(n) gives us the length of LIS for arr[].