Skip to content

Commit 12182d9

Browse files
authored
Improvement (jainaman224#70)
1 parent 6137352 commit 12182d9

File tree

16 files changed

+90
-83
lines changed

16 files changed

+90
-83
lines changed

Binary_Search/Binary_Search.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
22

3-
//The namespace refers to the project name you are working on.
3+
// The namespace refers to the project name you are working on.
44
namespace BinarySearch
55
{
66
class Program
77
{
88
public static int Binary_Search(int[] array, int size, int desired)
99
{
1010
int left = 0, right = size - 1, middle;
11-
while(left<=right)
11+
while(left <= right)
1212
{
1313
middle = left + (right - left) / 2;
1414
if(array[middle] == desired)
@@ -28,19 +28,20 @@ static void Main(string[] args)
2828
Console.WriteLine("Found");
2929
else
3030
Console.WriteLine("Not Found");
31-
//Element 9 to be searched
31+
// Element 9 to be searched
3232
if(Binary_Search(array, 7, 9) != -1)
3333
Console.WriteLine("Found");
3434
else
3535
Console.WriteLine("Not Found");
3636
Console.WriteLine();
37-
Console.ReadLine(); //To hold output (optional)
37+
Console.ReadLine(); // To hold output (optional)
3838
}
3939
}
4040
}
4141

42-
/*
43-
output:
42+
/* Output
43+
4444
Found
4545
Not Found
46+
4647
*/

Bubble_Sort/Bubble_Sort.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Bubble_Sort
44
{
55
class Program
66
{
7-
// function for bubble sort
7+
// Function for bubble sort
88
public static void BubbleSort(int[] array, int size)
99
{
1010
int temp;
@@ -24,7 +24,7 @@ public static void BubbleSort(int[] array, int size)
2424

2525
}
2626

27-
// function to print array
27+
// Function to print array
2828
public static void Print_Array(int[] array, int size)
2929
{
3030
for(int i = 0; i < size; i++)

Chinese_Remainder_Theorem/Chinese_Remainder_Theorem.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public static int findMinimumDividend(int[] divisor, int[] remainder)
3535
int i, result, partialProduct;
3636
int len = divisor.GetLength(0);
3737

38-
for(i=0; i<len; i++)
38+
for(i = 0; i < len; i++)
3939
product *= divisor[i];
4040

4141
result = 0;
4242

43-
for(i=0; i<len; i++)
43+
for(i = 0; i < len; i++)
4444
{
4545
partialProduct = product / divisor[i];
4646
result += remainder[i] * inverse(partialProduct, divisor[i]) * partialProduct;
@@ -59,5 +59,6 @@ static void Main(String[] args)
5959
}
6060
}
6161
}
62-
//Output
63-
//Minimum value of dividend is : 3371
62+
63+
// Output
64+
// Minimum value of dividend is : 3371

Counting_Sort/Counting_Sort.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static void CountingSort(int[] input)
4141
for(int i = 0; i < n; i++)
4242
input[i] = output[i]; // Copy the output array to input, so that input now contains sorted values
4343
}
44-
// function for bubble sort
45-
// function ro print array
44+
// Function for bubble sort
45+
// Function ro print array
4646
public static void Print_Array(int[] array, int size)
4747
{
4848
for(int i = 0; i < size; i++)

Euclidean_Algorithm/Euclidean_Algorithm.cs

+23-22
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,29 @@ public static int EuclidGCD(int a, int b)
1212
return EuclidGCD(b, a % b);
1313
}
1414

15-
public static void Main (String[] args)
16-
{
17-
int a = 315;
18-
int b = 105;
19-
int c = EuclidGCD(a,b);
20-
Console.WriteLine(c);
21-
// prints 105
22-
// GCD of 315 and 105 is 105
23-
a = 30;
24-
b = 105;
25-
c = EuclidGCD(a,b);
26-
Console.WriteLine(c);
27-
// prints 15
28-
// GCD of 30 and 105 is 15
29-
Console.WriteLine();
30-
Console.ReadLine();
31-
}
32-
}
15+
public static void Main(String[] args)
16+
{
17+
int a = 315;
18+
int b = 105;
19+
int c = EuclidGCD(a, b);
20+
Console.WriteLine(c);
21+
// Prints 105
22+
// GCD of 315 and 105 is 105
23+
a = 30;
24+
b = 105;
25+
c = EuclidGCD(a, b);
26+
Console.WriteLine(c);
27+
// Prints 15
28+
// GCD of 30 and 105 is 15
29+
Console.WriteLine();
30+
Console.ReadLine();
31+
}
32+
}
3333
}
3434

35-
/*
36-
Output:
37-
105
38-
15
35+
/* Output
36+
37+
105
38+
15
39+
3940
*/

Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static int gcdFunction(int a, int b, int x, int y)
1515
int x1 = 0;
1616
int y1 = 0;
1717
int gcd = gcdFunction(b % a, a, x1, y1);
18-
x = y1 - b / a * x1;
18+
x = y1 - (b / a) * x1;
1919
y = x1;
2020
return gcd;
2121
}
@@ -34,5 +34,5 @@ public static void Main(String[] args)
3434
}
3535
}
3636

37-
//output
38-
//GCD of numbers 98 and 21 is 7
37+
// Output
38+
// GCD of numbers 98 and 21 is 7

Heap_Sort/Heap_Sort.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void HeapSort(int[] array, int size)
4848
}
4949
}
5050

51-
// function ro print array
51+
// Function ro print array
5252
public static void Print_Array(int[] array, int size)
5353
{
5454
for(int i = 0; i < size; i++)
@@ -67,5 +67,5 @@ public static void Main(String[] args)
6767
}
6868
}
6969

70-
//output
70+
// Output
7171
// 1 2 3 4 4 6 8

Insertion_Sort/Insertion_Sort.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Insertion_Sort
44
{
55
class Program
66
{
7-
// function for insertion sort
7+
// Function for insertion sort
88
public static void InsertionSort(int[] array, int size)
99
{
1010
int temp, j;
@@ -22,7 +22,7 @@ public static void InsertionSort(int[] array, int size)
2222
}
2323
}
2424

25-
// function to print array
25+
// Function to print array
2626
public static void Print_Array(int[] array, int size)
2727
{
2828
for(int i = 0; i < size; i++)

Interpolation_Search/Interpolation_Search.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Program
66
{
77
public static int interpolation(int[] a, int n, int search_item) // Function implementing Interpolation_Search
88
{
9-
int high = n-1;
9+
int high = n - 1;
1010
int low = 0;
1111
int pos;
1212

@@ -44,7 +44,8 @@ public static void Main(String[] args)
4444
}
4545
}
4646

47-
/*
48-
Output:
49-
Found at position7
47+
/* Output
48+
49+
Found at position 7
50+
5051
*/

Kadane_Algorithm/Kadane_Algorithm.cs

+16-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public static int max(int a, int b)
1111
else
1212
return b;
1313
}
14-
// function implementing Kadane's Algorithm (array contains at least one positive number)
14+
15+
// Function implementing Kadane's Algorithm (array contains at least one positive number)
1516
public static int kadane(int[] input, int size)
1617
{
1718
int current_max = 0;
@@ -22,41 +23,42 @@ public static int kadane(int[] input, int size)
2223
current_max = max(0, current_max + input[i]);
2324
max_so_far = max(max_so_far, current_max);
2425
}
25-
return max_so_far;// maximum subarray sum
26+
return max_so_far; // Maximum subarray sum
2627
}
2728

2829
static void Main(String[] args)
2930
{
3031
int size, max_subarray_sum;
3132

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

34-
size = 9;//size of array
35+
size = 9; // Size of array
3536

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

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

40-
for(int i = 0; i < size; i++) // scanning each element in array
41+
for(int i = 0; i < size; i++) // Scanning each element in array
4142
{
42-
if(input[i] >= 0)// if any element is positive, kadane's algo can be applied
43+
if(input[i] >= 0) // If any element is positive, kadane's algo can be applied
4344
{
4445
flag = 1;
4546
break;
4647
}
47-
else if(input[i] > largest_in_negative) // if all the elements are negative, find the largest in them
48+
else if(input[i] > largest_in_negative) // If all the elements are negative, find the largest in them
4849
largest_in_negative = input[i];
4950
}
5051

51-
if(flag == 1)// kadane's algo applicable
52+
if(flag == 1) // Kadane's algo applicable
5253
max_subarray_sum = kadane(input, size);
5354
else
54-
max_subarray_sum = largest_in_negative;// kadane 's algo not applicable,
55-
//hence the max_subarray_sum will be the largest number in array itself
56-
Console.WriteLine("Maximum Subarray Sum is "+ Convert.ToString(max_subarray_sum));
55+
max_subarray_sum = largest_in_negative; // Kadane 's algo not applicable,
56+
// hence the max_subarray_sum will be the largest number in array itself
57+
Console.WriteLine("Maximum Subarray Sum is " + Convert.ToString(max_subarray_sum));
5758
Console.ReadLine();
5859
}
5960
}
6061
}
6162

62-
//output : Maximum Subarray Sum is 6
63+
// Output
64+
// Maximum Subarray Sum is 6

Knuth_Morris_Pratt_Algorithm/KMP.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Program
66
{
77
public static void calculateLps(string pattern, int[] lps)
88
{
9-
int length = 0; // Length of the previous longest prefix suffix
9+
int length = 0; // Length of the previous longest prefix suffix
1010
int i;
1111

1212
lps[0] = 0;

Linear_Search/Linear_Search.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static int LinearSearch(int[] array, int size, int desired)
88
{
99
for (int i = 0; i < size; i++)
1010
{
11-
// return position if element is found
11+
// Return position if element is found
1212
if (array[i] == desired)
1313
return i;
1414
}
@@ -27,7 +27,7 @@ public static void Main(String[] args)
2727
else
2828
Console.WriteLine("Not Found");
2929

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

40-
/*
41-
Output:
40+
/* Output
41+
4242
Found
4343
Not Found
44+
4445
*/

Naive_String_Matching/Naive_Approach.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void search(string text, string pattern)
1616
break;
1717

1818
if(j == lengthPattern)
19-
cout << "Pattern found at " << i << endl;
19+
cout << "Pattern found at " << i + 1 << endl;
2020
}
2121
}
2222

@@ -30,8 +30,8 @@ int main()
3030

3131
/* Output
3232
33-
Pattern found at 1
34-
Pattern found at 7
35-
Pattern found at 16
33+
Pattern found at 2
34+
Pattern found at 8
35+
Pattern found at 17
3636
3737
*/

Naive_String_Matching/Naive_Approach.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Naive_Approach
1+
class Main
22
{
33
public static void search(String text, String pattern)
44
{
@@ -14,7 +14,7 @@ public static void search(String text, String pattern)
1414
break;
1515

1616
if(j == lengthPattern)
17-
System.out.println("Pattern found at " + i);
17+
System.out.println("Pattern found at " + (i + 1));
1818
}
1919
}
2020

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

2929
/* Output
3030
31-
Pattern found at 1
32-
Pattern found at 7
33-
Pattern found at 16
31+
Pattern found at 2
32+
Pattern found at 8
33+
Pattern found at 17
3434
3535
*/

Naive_String_Matching/Naive_Approach.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ def search(text, pattern):
1212
j += 1
1313

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

1717
text = "namanchamanbomanamansanam"
1818
pattern = "aman"
1919
search(text, pattern)
2020

2121
''' Output
2222
23-
Pattern found at 1
24-
Pattern found at 7
25-
Pattern found at 16
23+
Pattern found at 2
24+
Pattern found at 8
25+
Pattern found at 17
2626
2727
'''

0 commit comments

Comments
 (0)