diff --git a/Dynamic Programming/Longest Increasing Subsequence/cpp/longest_increasing_subsequence.cpp b/Dynamic Programming/Longest Increasing Subsequence/cpp/longest_increasing_subsequence.cpp new file mode 100644 index 000000000..b47431566 --- /dev/null +++ b/Dynamic Programming/Longest Increasing Subsequence/cpp/longest_increasing_subsequence.cpp @@ -0,0 +1,45 @@ +/** + * The program finds the length of the Longest subsequence (may not be + * continuous) such that the subsequence is in increasing order + */ +#include +using namespace std; + + + +int findMax(int arr[], int length) { + int max = 0; + for (int i = 0; i < length; i++) { + if (max < arr[i]) { + max = arr[i]; + } + } + return max; +} + +int findLongestIncSubLength(int arr[],int length) { + int dp[length]; + int i, j, max = 0; + + // Initialize Longest Increasing Subsequence values + for (i = 0; i < length; i++) { + dp[i] = 1; + } + + for (i = 1; i < length; i++) { + for (j = 0; j < i; j++) { + if (arr[i] > arr[j] && dp[i] < dp[j] + 1) { + dp[i] = dp[j] + 1; + } + } + } + max = findMax(dp, length); + return max; +} + +int main() { + int arr[] = { 1, 4, 2, 10, 8 }; + int lisLength = findLongestIncSubLength(arr,5); + cout << "Longest Increasing Subsequence Length is : " << lisLength << endl; + +} diff --git a/Dynamic Programming/Longest Increasing Subsequence/java/LongestIncreasingSubsequence.java b/Dynamic Programming/Longest Increasing Subsequence/java/LongestIncreasingSubsequence.java new file mode 100644 index 000000000..33bfdff59 --- /dev/null +++ b/Dynamic Programming/Longest Increasing Subsequence/java/LongestIncreasingSubsequence.java @@ -0,0 +1,44 @@ +/** + * The program finds the length of the Longest subsequence (may not be + * continuous) such that the subsequence is in increasing order + */ +public class LongestIncreasingSubsequence { + public static void main(String args[]) { + int arr[] = { 1, 4, 2, 10, 8 }; + int lisLength = findLongestIncSubLength(arr); + System.out.println("Longest Increasing Subsequence Length is : "+lisLength); + + } + + static int findLongestIncSubLength(int arr[]) { + int length = arr.length; + int dp[] = new int[length]; + int i, j, max = 0; + + // Initialize Longest Increasing Subsequence values + for (i = 0; i < length; i++) { + dp[i] = 1; + } + + for (i = 1; i < length; i++) { + for (j = 0; j < i; j++) { + if (arr[i] > arr[j] && dp[i] < dp[j] + 1) { + dp[i] = dp[j] + 1; + } + } + } + max = findMax(dp); + return max; + } + + public static int findMax(int arr[]) { + int length = arr.length; + int max = 0; + for (int i = 0; i < length; i++) { + if (max < arr[i]) { + max = arr[i]; + } + } + return max; + } +} \ No newline at end of file diff --git a/Mathematics/factorial/swift/factorial.swift b/Mathematics/factorial/swift/factorial.swift index 448e011d4..348f0fa96 100644 --- a/Mathematics/factorial/swift/factorial.swift +++ b/Mathematics/factorial/swift/factorial.swift @@ -1,15 +1,15 @@ -var number: Int = 5 // Change the number here +var num: Int = 6 // You can change the number here -var fact: Int = 1 +var f: Int = 1 -var n: Int = number + 1 +var n: Int = num + 1 for i in 1.. -[Ravi Varshney](https://github.com/ravivarshney01):tada:
-[Ananya Tewari](https://github.com/antew7):tada:
+* [Vishal Gaur](https://github.com/i-vishi):tada:
+* [Ravi Varshney](https://github.com/ravivarshney01):tada:
+* [Ananya Tewari](https://github.com/antew7):tada:
diff --git a/Sorting/Radix Sort/golang/radixsort.go b/Sorting/Radix Sort/golang/radixsort.go new file mode 100644 index 000000000..b7d5f9ff9 --- /dev/null +++ b/Sorting/Radix Sort/golang/radixsort.go @@ -0,0 +1,49 @@ +// Radix Sort in Golang +package main + +import ( + "bytes" + "encoding/binary" + "fmt" +) + +const digit = 4 +const maxbit = -1 << 31 + + +func main() { + + var data = []int32{421, 15, -175, 90, -2, 214, -52, -166} + fmt.Println("\n--- Unsorted --- \n\n", data) + radixsort(data) + fmt.Println("\n--- Sorted ---\n\n", data, "\n") +} + +func radixsort(data []int32) { + buf := bytes.NewBuffer(nil) + ds := make([][]byte, len(data)) + for i, e := range data { + binary.Write(buf, binary.LittleEndian, e^maxbit) + b := make([]byte, digit) + buf.Read(b) + ds[i] = b + } + countingSort := make([][][]byte, 256) + for i := 0; i < digit; i++ { + for _, b := range ds { + countingSort[b[i]] = append(countingSort[b[i]], b) + } + j := 0 + for k, bs := range countingSort { + copy(ds[j:], bs) + j += len(bs) + countingSort[k] = bs[:0] + } + } + var w int32 + for i, b := range ds { + buf.Write(b) + binary.Read(buf, binary.LittleEndian, &w) + data[i] = w^maxbit + } +} \ No newline at end of file diff --git a/Sorting/Selection Sort/golang/selectionSort.go b/Sorting/Selection Sort/golang/selectionSort.go new file mode 100644 index 000000000..80dc05e6e --- /dev/null +++ b/Sorting/Selection Sort/golang/selectionSort.go @@ -0,0 +1,40 @@ +// Selection Sort in Golang +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func main() { + + slice := generateSlice(20) + fmt.Println("\n--- Unsorted --- \n\n", slice) + selectionsort(slice) + fmt.Println("\n--- Sorted ---\n\n", slice, "\n") +} + +// Generates a slice of size, size filled with random numbers +func generateSlice(size int) []int { + + slice := make([]int, size, size) + rand.Seed(time.Now().UnixNano()) + for i := 0; i < size; i++ { + slice[i] = rand.Intn(999) - rand.Intn(999) + } + return slice +} + +func selectionsort(items []int) { + var n = len(items) + for i := 0; i < n; i++ { + var minIdx = i + for j := i; j < n; j++ { + if items[j] < items[minIdx] { + minIdx = j + } + } + items[i], items[minIdx] = items[minIdx], items[i] + } +} \ No newline at end of file diff --git a/Sorting/bubbleSort/golang/bubbleSort.go b/Sorting/bubbleSort/golang/bubbleSort.go new file mode 100644 index 000000000..d259c896c --- /dev/null +++ b/Sorting/bubbleSort/golang/bubbleSort.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" +) + +var toBeSorted [10]int = [10]int{1,3,2,4,8,6,7,2,3,0} + +func bubbleSort(input [10]int) { + // n is the number of items in our list + n := 10 + swapped := true + for swapped { + swapped = false + for i := 1; i < n-1; i++ { + if input[i-1] > input[i] { + fmt.Println("Swapping") + // swap values using Go's tuple assignment + input[i], input[i-1] = input[i-1], input[i] + swapped = true + } + } + } + fmt.Println(input) +} + + +func main() { + fmt.Println("Hello World") + bubbleSort(toBeSorted) + +} \ No newline at end of file diff --git a/Sorting/insertionSort/golang/insertionSort.go b/Sorting/insertionSort/golang/insertionSort.go new file mode 100644 index 000000000..f67eaae6b --- /dev/null +++ b/Sorting/insertionSort/golang/insertionSort.go @@ -0,0 +1,40 @@ +/ Selection Sort in Golang +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func main() { + + slice := generateSlice(20) + fmt.Println("\n--- Unsorted --- \n\n", slice) + selectionsort(slice) + fmt.Println("\n--- Sorted ---\n\n", slice, "\n") +} + +// Generates a slice of size, size filled with random numbers +func generateSlice(size int) []int { + + slice := make([]int, size, size) + rand.Seed(time.Now().UnixNano()) + for i := 0; i < size; i++ { + slice[i] = rand.Intn(999) - rand.Intn(999) + } + return slice +} + +func selectionsort(items []int) { + var n = len(items) + for i := 0; i < n; i++ { + var minIdx = i + for j := i; j < n; j++ { + if items[j] < items[minIdx] { + minIdx = j + } + } + items[i], items[minIdx] = items[minIdx], items[i] + } +} \ No newline at end of file diff --git a/Sorting/timSort/c/tim-sort.c b/Sorting/timSort/c/tim-sort.c new file mode 100644 index 000000000..c88f965d5 --- /dev/null +++ b/Sorting/timSort/c/tim-sort.c @@ -0,0 +1,99 @@ +#include +#include + +const int RUN = 32; + +int MIN(int a ,int b) { + return (a > b) ? b : a; +} +void insertionSort(int arr[], int left, int right) +{ + for (int i = left + 1; i <= right; i++) + { + int temp = arr[i]; + int j = i - 1; + while (arr[j] > temp && j >= left) + { + arr[j+1] = arr[j]; + j--; + } + arr[j+1] = temp; + } +} + +void merge(int arr[], int l, int m, int r) +{ + int len1 = m - l + 1, len2 = r - m; + int left[len1], right[len2]; + for (int i = 0; i < len1; i++) + left[i] = arr[l + i]; + for (int i = 0; i < len2; i++) + right[i] = arr[m + 1 + i]; + + int i = 0; + int j = 0; + int k = l; + while (i < len1 && j < len2) + { + if (left[i] <= right[j]) + { + arr[k] = left[i]; + i++; + } + else + { + arr[k] = right[j]; + j++; + } + k++; + } + + while (i < len1) + { + arr[k] = left[i]; + k++; + i++; + } + + while (j < len2) + { + arr[k] = right[j]; + k++; + j++; + } +} + +void timSort(int arr[], int n) +{ + for (int i = 0; i < n; i+=RUN) + insertionSort(arr, i, MIN((i+31), (n-1))); + for (int size = RUN; size < n; size = 2*size) + { + for (int left = 0; left < n; left += 2*size) + { + int mid = left + size - 1; + int right = MIN((left + 2*size - 1), (n-1)); + merge(arr, left, mid, right); + } + } +} +void printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + int arr[] = {5, 21, 7, 23, 19}; + int n = sizeof(arr)/sizeof(arr[0]); + printf("Given Array is\n"); + printArray(arr, n); + + timSort(arr, n); + + printf("After Sorting Array is\n"); + printArray(arr, n); + return 0; +}