Skip to content

Commit

Permalink
Improvement (jainaman224#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jainaman224 authored Jul 10, 2016
1 parent 6137352 commit 12182d9
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 83 deletions.
13 changes: 7 additions & 6 deletions Binary_Search/Binary_Search.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;

//The namespace refers to the project name you are working on.
// The namespace refers to the project name you are working on.
namespace BinarySearch
{
class Program
{
public static int Binary_Search(int[] array, int size, int desired)
{
int left = 0, right = size - 1, middle;
while(left<=right)
while(left <= right)
{
middle = left + (right - left) / 2;
if(array[middle] == desired)
Expand All @@ -28,19 +28,20 @@ static void Main(string[] args)
Console.WriteLine("Found");
else
Console.WriteLine("Not Found");
//Element 9 to be searched
// Element 9 to be searched
if(Binary_Search(array, 7, 9) != -1)
Console.WriteLine("Found");
else
Console.WriteLine("Not Found");
Console.WriteLine();
Console.ReadLine(); //To hold output (optional)
Console.ReadLine(); // To hold output (optional)
}
}
}

/*
output:
/* Output
Found
Not Found
*/
4 changes: 2 additions & 2 deletions Bubble_Sort/Bubble_Sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Bubble_Sort
{
class Program
{
// function for bubble sort
// Function for bubble sort
public static void BubbleSort(int[] array, int size)
{
int temp;
Expand All @@ -24,7 +24,7 @@ public static void BubbleSort(int[] array, int size)

}

// function to print array
// Function to print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
Expand Down
9 changes: 5 additions & 4 deletions Chinese_Remainder_Theorem/Chinese_Remainder_Theorem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public static int findMinimumDividend(int[] divisor, int[] remainder)
int i, result, partialProduct;
int len = divisor.GetLength(0);

for(i=0; i<len; i++)
for(i = 0; i < len; i++)
product *= divisor[i];

result = 0;

for(i=0; i<len; i++)
for(i = 0; i < len; i++)
{
partialProduct = product / divisor[i];
result += remainder[i] * inverse(partialProduct, divisor[i]) * partialProduct;
Expand All @@ -59,5 +59,6 @@ static void Main(String[] args)
}
}
}
//Output
//Minimum value of dividend is : 3371

// Output
// Minimum value of dividend is : 3371
4 changes: 2 additions & 2 deletions Counting_Sort/Counting_Sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static void CountingSort(int[] input)
for(int i = 0; i < n; i++)
input[i] = output[i]; // Copy the output array to input, so that input now contains sorted values
}
// function for bubble sort
// function ro print array
// Function for bubble sort
// Function ro print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
Expand Down
45 changes: 23 additions & 22 deletions Euclidean_Algorithm/Euclidean_Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,29 @@ public static int EuclidGCD(int a, int b)
return EuclidGCD(b, a % b);
}

public static void Main (String[] args)
{
int a = 315;
int b = 105;
int c = EuclidGCD(a,b);
Console.WriteLine(c);
// prints 105
// GCD of 315 and 105 is 105
a = 30;
b = 105;
c = EuclidGCD(a,b);
Console.WriteLine(c);
// prints 15
// GCD of 30 and 105 is 15
Console.WriteLine();
Console.ReadLine();
}
}
public static void Main(String[] args)
{
int a = 315;
int b = 105;
int c = EuclidGCD(a, b);
Console.WriteLine(c);
// Prints 105
// GCD of 315 and 105 is 105
a = 30;
b = 105;
c = EuclidGCD(a, b);
Console.WriteLine(c);
// Prints 15
// GCD of 30 and 105 is 15
Console.WriteLine();
Console.ReadLine();
}
}
}

/*
Output:
105
15
/* Output
105
15
*/
6 changes: 3 additions & 3 deletions Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static int gcdFunction(int a, int b, int x, int y)
int x1 = 0;
int y1 = 0;
int gcd = gcdFunction(b % a, a, x1, y1);
x = y1 - b / a * x1;
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
Expand All @@ -34,5 +34,5 @@ public static void Main(String[] args)
}
}

//output
//GCD of numbers 98 and 21 is 7
// Output
// GCD of numbers 98 and 21 is 7
4 changes: 2 additions & 2 deletions Heap_Sort/Heap_Sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void HeapSort(int[] array, int size)
}
}

// function ro print array
// Function ro print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
Expand All @@ -67,5 +67,5 @@ public static void Main(String[] args)
}
}

//output
// Output
// 1 2 3 4 4 6 8
4 changes: 2 additions & 2 deletions Insertion_Sort/Insertion_Sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Insertion_Sort
{
class Program
{
// function for insertion sort
// Function for insertion sort
public static void InsertionSort(int[] array, int size)
{
int temp, j;
Expand All @@ -22,7 +22,7 @@ public static void InsertionSort(int[] array, int size)
}
}

// function to print array
// Function to print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
Expand Down
9 changes: 5 additions & 4 deletions Interpolation_Search/Interpolation_Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Program
{
public static int interpolation(int[] a, int n, int search_item) // Function implementing Interpolation_Search
{
int high = n-1;
int high = n - 1;
int low = 0;
int pos;

Expand Down Expand Up @@ -44,7 +44,8 @@ public static void Main(String[] args)
}
}

/*
Output:
Found at position7
/* Output
Found at position 7
*/
30 changes: 16 additions & 14 deletions Kadane_Algorithm/Kadane_Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static int max(int a, int b)
else
return b;
}
// function implementing Kadane's Algorithm (array contains at least one positive number)

// Function implementing Kadane's Algorithm (array contains at least one positive number)
public static int kadane(int[] input, int size)
{
int current_max = 0;
Expand All @@ -22,41 +23,42 @@ public static int kadane(int[] input, int size)
current_max = max(0, current_max + input[i]);
max_so_far = max(max_so_far, current_max);
}
return max_so_far;// maximum subarray sum
return max_so_far; // Maximum subarray sum
}

static void Main(String[] args)
{
int size, max_subarray_sum;

int[] input = { -2, 1, -6, 4, -1, 2, 1, -5, 4 }; //input array
int[] input = { -2, 1, -6, 4, -1, 2, 1, -5, 4 }; // Input array

size = 9;//size of array
size = 9; // Size of array

int flag = 0;//flag variable to check if all the numbers in array are negative or not
int flag = 0; // Flag variable to check if all the numbers in array are negative or not

int largest_in_negative = input[0];//smallest_negative variable will store the maximum subarray sum if all the numbers are negative in array
int largest_in_negative = input[0]; // Smallest_negative variable will store the maximum subarray sum if all the numbers are negative in array

for(int i = 0; i < size; i++) // scanning each element in array
for(int i = 0; i < size; i++) // Scanning each element in array
{
if(input[i] >= 0)// if any element is positive, kadane's algo can be applied
if(input[i] >= 0) // If any element is positive, kadane's algo can be applied
{
flag = 1;
break;
}
else if(input[i] > largest_in_negative) // if all the elements are negative, find the largest in them
else if(input[i] > largest_in_negative) // If all the elements are negative, find the largest in them
largest_in_negative = input[i];
}

if(flag == 1)// kadane's algo applicable
if(flag == 1) // Kadane's algo applicable
max_subarray_sum = kadane(input, size);
else
max_subarray_sum = largest_in_negative;// kadane 's algo not applicable,
//hence the max_subarray_sum will be the largest number in array itself
Console.WriteLine("Maximum Subarray Sum is "+ Convert.ToString(max_subarray_sum));
max_subarray_sum = largest_in_negative; // Kadane 's algo not applicable,
// hence the max_subarray_sum will be the largest number in array itself
Console.WriteLine("Maximum Subarray Sum is " + Convert.ToString(max_subarray_sum));
Console.ReadLine();
}
}
}

//output : Maximum Subarray Sum is 6
// Output
// Maximum Subarray Sum is 6
2 changes: 1 addition & 1 deletion Knuth_Morris_Pratt_Algorithm/KMP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Program
{
public static void calculateLps(string pattern, int[] lps)
{
int length = 0; // Length of the previous longest prefix suffix
int length = 0; // Length of the previous longest prefix suffix
int i;

lps[0] = 0;
Expand Down
9 changes: 5 additions & 4 deletions Linear_Search/Linear_Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static int LinearSearch(int[] array, int size, int desired)
{
for (int i = 0; i < size; i++)
{
// return position if element is found
// Return position if element is found
if (array[i] == desired)
return i;
}
Expand All @@ -27,7 +27,7 @@ public static void Main(String[] args)
else
Console.WriteLine("Not Found");

//Element 9 to be searched
// Element 9 to be searched
if(LinearSearch(array, 7, 9) != -1)
Console.WriteLine("Found");
else
Expand All @@ -37,8 +37,9 @@ public static void Main(String[] args)
}
}

/*
Output:
/* Output
Found
Not Found
*/
8 changes: 4 additions & 4 deletions Naive_String_Matching/Naive_Approach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void search(string text, string pattern)
break;

if(j == lengthPattern)
cout << "Pattern found at " << i << endl;
cout << "Pattern found at " << i + 1 << endl;
}
}

Expand All @@ -30,8 +30,8 @@ int main()

/* Output
Pattern found at 1
Pattern found at 7
Pattern found at 16
Pattern found at 2
Pattern found at 8
Pattern found at 17
*/
10 changes: 5 additions & 5 deletions Naive_String_Matching/Naive_Approach.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Naive_Approach
class Main
{
public static void search(String text, String pattern)
{
Expand All @@ -14,7 +14,7 @@ public static void search(String text, String pattern)
break;

if(j == lengthPattern)
System.out.println("Pattern found at " + i);
System.out.println("Pattern found at " + (i + 1));
}
}

Expand All @@ -28,8 +28,8 @@ public static void main(String[] args)

/* Output
Pattern found at 1
Pattern found at 7
Pattern found at 16
Pattern found at 2
Pattern found at 8
Pattern found at 17
*/
8 changes: 4 additions & 4 deletions Naive_String_Matching/Naive_Approach.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ def search(text, pattern):
j += 1

if j == lengthPattern:
print("Pattern found at " + str(i))
print("Pattern found at " + str(i + 1))

text = "namanchamanbomanamansanam"
pattern = "aman"
search(text, pattern)

''' Output
Pattern found at 1
Pattern found at 7
Pattern found at 16
Pattern found at 2
Pattern found at 8
Pattern found at 17
'''
Loading

0 comments on commit 12182d9

Please sign in to comment.