Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some algorithms and fixed issue #200

Merged
merged 8 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,474 changes: 830 additions & 644 deletions README.md

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions localization/ko/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,33 @@
}
```

### Mode in Array

```java
public static int modeArray(int arr[])
{
int mode=0,maxcount=0;

for(int i=0;i<arr.length;i++)
{
int count=0;

for(int j=0;j<arr.length;j++)
{
if(arr[i]==arr[j])
count++;
}
if(count>maxcount)
{
maxcount=count;
mode=arr[i];
}
}
return mode;
}

```

### 배열의 합

```java
Expand Down Expand Up @@ -354,6 +381,19 @@

### 재귀함수를 활용한 피보나치

### Even or Odd

```java
public static String evenodd(int num)
{
if(num%2==0)
return "even";
else
return "odd";
}

```

```java
public static int fibonacci(int n) {
if (n <= 1) {
Expand Down Expand Up @@ -482,6 +522,39 @@ public static int calculateLuhnChecksum(long num) {
}
```

### SquareRoot of a Number
```java
static double sqrt(int num,int p) //p-precision(till how many decimal numbers we want)
{
int start=0,end=num;
double root=0.0;

while(start<=end)
{
int mid=start+(end-start)/2;

if((mid*mid)>num)
end=mid-1;
else if((mid*mid)<num)
start=mid+1;
else
return mid;
}
//root=end;
double incr=0.1;

for(int i=0;i<p;i++)
{
while(root*root < num)
root=root+incr;

root=root-incr;
incr=incr/10;
}

return root;
}
```
### 최대공약수(GCD)

```java
Expand Down
72 changes: 72 additions & 0 deletions localization/zh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@
return arr.length % 2 != 0 ? (double) arr[mid] : (double) (arr[mid] + arr[mid - 1]) / 2;
}
```
### Mode in Array

```java
public static int modeArray(int arr[])
{
int mode=0,maxcount=0;

for(int i=0;i<arr.length;i++)
{
int count=0;

for(int j=0;j<arr.length;j++)
{
if(arr[i]==arr[j])
count++;
}
if(count>maxcount)
{
maxcount=count;
mode=arr[i];
}
}
return mode;
}

```

### 计算整数数组的和

Expand Down Expand Up @@ -350,6 +376,19 @@

## 数学

### Even or Odd

```java
public static String evenodd(int num)
{
if(num%2==0)
return "even";
else
return "odd";
}

```

### 斐波那契

```java
Expand Down Expand Up @@ -479,6 +518,39 @@ public static int calculateLuhnChecksum(long num) {
return checksumDigit;
}
```
### SquareRoot of a Number
```java
static double sqrt(int num,int p) //p-precision(till how many decimal numbers we want)
{
int start=0,end=num;
double root=0.0;

while(start<=end)
{
int mid=start+(end-start)/2;

if((mid*mid)>num)
end=mid-1;
else if((mid*mid)<num)
start=mid+1;
else
return mid;
}
//root=end;
double incr=0.1;

for(int i=0;i<p;i++)
{
while(root*root < num)
root=root+incr;

root=root-incr;
incr=incr/10;
}

return root;
}
```

### 最大公约数

Expand Down
95 changes: 95 additions & 0 deletions src/main/java/algorithm/BinarySearchIn2dArraySnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package algorithm;

/**
* BinarySearchIn2dArraySnippet.
*/
public class BinarySearchIn2dArraySnippet {

/**
* Search an item with binarySearch algorithm.
*
* @param matrix should be sorted
* @param target an item to search
* @return if location of item is found, otherwise return {-1,-1}
*/
public static int[] binarySearchIn2darr(int[][] matrix, int target) {
int rows = matrix.length - 1;
int cols = matrix[0].length - 1;

if (rows == 1) {
return binarySearch(matrix, target, 0, 0, cols);
}

int rstart = 0;
int rend = rows;
int cmid = cols / 2;

while (rstart < rend - 1) {
int rmid = rstart + (rend - rstart) / 2;
if (matrix[rmid][cmid] > target) {
rend = rmid;
} else if (matrix[rmid][cmid] < target) {
rstart = rmid;
} else {
return new int[]{rmid, cmid};
}
}
if (matrix[rstart][cmid] == target) {
return new int[]{rstart, cmid};
}
if (matrix[rend][cmid] == target) {
return new int[]{rend, cmid};
}
if (target <= matrix[rstart][cmid - 1]) {
return binarySearch(matrix, target, rstart, 0, cmid - 1);
}
if (target >= matrix[rstart][cmid + 1]) {
return binarySearch(matrix, target, rstart, cmid + 1, cols);
}
if (target <= matrix[rend][cmid - 1]) {
return binarySearch(matrix, target, rend, 0, cmid - 1);
}
if (target <= matrix[rend][cmid + 1]) {
return binarySearch(matrix, target, rend, cmid + 1, cols);
}
return new int[]{-1, -1};
}

static int[] binarySearch(int[][] matrix, int target, int row, int cstart, int cend) {
while (cstart <= cend) {
int cmid = cstart + (cend - cstart) / 2;
if (matrix[row][cmid] > target) {
cend = cmid - 1;
} else if (matrix[row][cmid] < target) {
cstart = cend + 1;
} else {
return new int[]{row, cmid};
}
}
return new int[]{-1, -1};
}
}
52 changes: 52 additions & 0 deletions src/main/java/algorithm/LinearSearchIn2dArraySnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package algorithm;

/**
* LinearSearchIn2dArraySnippet.
*/
public class LinearSearchIn2dArraySnippet {

/**
* Search an item with linearSearch algorithm.
*
* @param arr array to search
* @param target an item to search
* @return if location of target is found,otherwise return {-1,-1}
*/

public static int[] linearSearch2dArray(int[][] arr, int target) {

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target) {
return new int[]{i, j};
}
}
}

return new int[]{-1, -1};
}
}
57 changes: 57 additions & 0 deletions src/main/java/array/ArrayModeSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package array;

/**
* ArrayModeSnippet.
*/
public class ArrayModeSnippet {

/**
* Returns the mode of the array.
*
* @param arr array to find mode in it
* @return mode of array
*/
public static int modeArray(int[] arr) {
int mode = 0;
int maxcount = 0;

for (int i = 0; i < arr.length; i++) {
int count = 0;

for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count > maxcount) {
maxcount = count;
mode = arr[i];
}
}
return mode;
}
}
Loading