Skip to content

Commit ba9960c

Browse files
Initial commit
0 parents  commit ba9960c

13 files changed

+421
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>tech.bingulhan</groupId>
8+
<artifactId>JavaAlgorithmsWays</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tech.bingulhan.sortandsearch;
2+
3+
public class BinarySearchExample {
4+
5+
public static int binarySearch(int[] array, int target) {
6+
int left = 0;
7+
int right = array.length - 1;
8+
9+
while (left <= right) {
10+
int mid = left + (right - left) / 2;
11+
12+
// Eğer hedef değer ortanca elemana eşitse, index'i döndür
13+
if (array[mid] == target) {
14+
return mid;
15+
}
16+
17+
// Eğer hedef değer ortanca elemandan küçükse, aramanın sol tarafına odaklan
18+
if (array[mid] > target) {
19+
right = mid - 1;
20+
}
21+
// Eğer hedef değer ortanca elemandan büyükse, aramanın sağ tarafına odaklan
22+
else {
23+
left = mid + 1;
24+
}
25+
}
26+
27+
return -1; // Hedef eleman bulunamadığında -1 döndür
28+
}
29+
30+
public static void main(String[] args) {
31+
int[] array = {1, 2, 4, 5, 7, 9, 12, 15, 18};
32+
int target = 7;
33+
34+
int result = binarySearch(array, target);
35+
36+
if (result != -1) {
37+
System.out.println("Element " + target + " found at index " + result);
38+
} else {
39+
System.out.println("Element " + target + " not found in the array");
40+
}
41+
}
42+
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tech.bingulhan.sortandsearch;
2+
3+
import java.util.Arrays;
4+
5+
public class BubbleSortExample {
6+
7+
public static void bubbleSort(int[] array) {
8+
int n = array.length;
9+
10+
for (int i = 0; i < n - 1; i++) {
11+
for (int j = 0; j < n - i - 1; j++) {
12+
// Komşu elemanları karşılaştır ve gerekirse yer değiştir
13+
if (array[j] > array[j + 1]) {
14+
// Swap
15+
int temp = array[j];
16+
array[j] = array[j + 1];
17+
array[j + 1] = temp;
18+
}
19+
}
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] array = {64, 34, 25, 12, 22, 11, 90};
25+
System.out.println("Before bubble sort: " + Arrays.toString(array));
26+
27+
bubbleSort(array);
28+
29+
System.out.println("After bubble sort: " + Arrays.toString(array));
30+
}
31+
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tech.bingulhan.sortandsearch;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
/**
7+
*
8+
* Bumblesort, elemanları rastgele yer değiştirerek bir diziyi "sıralayan" basit bir algoritma olarak tasarlanmıştır. Bu nedenle, gerçek bir sıralama algoritması olarak kullanılması pek önerilmez.
9+
*
10+
*/
11+
12+
public class BumbleSortExample {
13+
14+
15+
16+
public static void bumblesort(int[] array) {
17+
Random random = new Random();
18+
19+
for (int i = 0; i < array.length - 1; i++) {
20+
// Elemanları rastgele yer değiştir
21+
int randomIndex = random.nextInt(array.length - i) + i;
22+
swap(array, i, randomIndex);
23+
}
24+
}
25+
26+
public static void swap(int[] array, int i, int j) {
27+
int temp = array[i];
28+
array[i] = array[j];
29+
array[j] = temp;
30+
}
31+
32+
public static void main(String[] args) {
33+
int[] array = {5, 2, 8, 9, 1, 3};
34+
System.out.println("Before bumblesort: " + Arrays.toString(array));
35+
36+
bumblesort(array);
37+
38+
System.out.println("After bumblesort: " + Arrays.toString(array));
39+
}
40+
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tech.bingulhan.sortandsearch;
2+
3+
import java.util.Arrays;
4+
5+
public class InsertionSortExample {
6+
7+
public static void insertionSort(int[] array) {
8+
int n = array.length;
9+
10+
for (int i = 1; i < n; ++i) {
11+
int key = array[i];
12+
int j = i - 1;
13+
14+
// Sıralı kısmı dolaşarak key'in uygun yerini bul
15+
while (j >= 0 && array[j] > key) {
16+
array[j + 1] = array[j];
17+
j = j - 1;
18+
}
19+
array[j + 1] = key;
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] array = {12, 11, 13, 5, 6};
25+
System.out.println("Before insertion sort: " + Arrays.toString(array));
26+
27+
insertionSort(array);
28+
29+
System.out.println("After insertion sort: " + Arrays.toString(array));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)