-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from VAR-solutions/master
update
- Loading branch information
Showing
9 changed files
with
357 additions
and
8 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Dynamic Programming/Longest Increasing Subsequence/cpp/longest_increasing_subsequence.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <bits/stdc++.h> | ||
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; | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
Dynamic Programming/Longest Increasing Subsequence/java/LongestIncreasingSubsequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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..<n{ | ||
|
||
fact = fact * i | ||
f = f * i | ||
|
||
} | ||
|
||
|
||
|
||
print("Factorial of ",number," is: ", fact) | ||
print("Factorial of ",num," is: ", f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |