Skip to content

Commit 4f88edc

Browse files
committed
Add project files.
+ Sorting algorithms: - insertion sort - selection sort - bubble sort - quick sort + Searching algorithms: - linear search - binary search + Java linked list class
1 parent 4bf5367 commit 4f88edc

File tree

11 files changed

+517
-0
lines changed

11 files changed

+517
-0
lines changed

datastructures/.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}

datastructures/src/LinkedListApp.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.LinkedList;
2+
3+
public class LinkedListApp {
4+
public static void main(String[] args) {
5+
6+
LinkedList<String> linkedList = new LinkedList<String>();
7+
8+
// Adding elements to the linked list
9+
linkedList.add("A");
10+
linkedList.add("B");
11+
linkedList.addLast("C");
12+
linkedList.addFirst("D");
13+
linkedList.add(2, "E");
14+
15+
System.out.println(linkedList);
16+
System.out.println("\n");
17+
18+
// Changing one element of the linked list
19+
linkedList.set(0, "Z");
20+
21+
// Using get methods and loops
22+
for (int i = 0; i < linkedList.size(); i++) {
23+
24+
System.out.print(linkedList.get(i) + " ");
25+
}
26+
System.out.println("\n");
27+
28+
// Removing elements from the linked list
29+
linkedList.remove("B");
30+
linkedList.remove(3);
31+
linkedList.removeFirst();
32+
linkedList.removeLast();
33+
34+
System.out.println(linkedList);
35+
}
36+
}

searchalgorithms/.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Current File",
10+
"request": "launch",
11+
"mainClass": "${file}"
12+
},
13+
{
14+
"type": "java",
15+
"name": "BinarySearch",
16+
"request": "launch",
17+
"mainClass": "BinarySearch",
18+
"projectName": "searchalghoritms_26a79ed7"
19+
}
20+
]
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.Scanner;
2+
3+
/*
4+
Binary Search is defined as a searching algorithm used in a sorted array;
5+
it works by repeatedly dividing the search interval in half.
6+
The idea of binary search is to use the information that the array is sorted
7+
and reduce the time complexity to O(logN).
8+
*/
9+
10+
public class BinarySearch {
11+
12+
/**
13+
* "Binary Search" function - Recursive variant
14+
*
15+
* @param sortedArray - array of int values, sorted - type: int
16+
* @param firstIndex - first index of the array - type: int
17+
* @param lastIndex - last index of the array - type: int
18+
* @param item - item to be found - type: int
19+
* @return item index, if found, else (-1) - type: int
20+
**/
21+
public int recursiveBinarySearch(int[] sortedArray, int firstIndex, int lastIndex, int item) {
22+
23+
// If the item is not found in the array, throws an exception
24+
if (firstIndex >= lastIndex)
25+
return -1;
26+
else {
27+
28+
// Finding the middle element index in the array
29+
// to eventually split the sorted array in n sub-arrays
30+
int middle = (firstIndex + lastIndex) / 2;
31+
32+
// Check if the searched item is in the middle of the array
33+
if (sortedArray[middle] == item)
34+
return middle;
35+
// If the middle element of the array is smaller than the searched item
36+
// then search again in the sub-array starting from middle+1 index
37+
else if (sortedArray[middle] < item)
38+
return recursiveBinarySearch(sortedArray, middle + 1, lastIndex, item);
39+
// If the middle element of the array is bigger than the searched item
40+
// then search again in the sub-array ending to middle index
41+
return recursiveBinarySearch(sortedArray, firstIndex, middle - 1, item);
42+
}
43+
}
44+
45+
/**
46+
* "Binary Search" function - Iterative variant
47+
*
48+
* @param sortedArray - array of int values, sorted - type: int
49+
* @param item - item to be found - type: int
50+
* @return item index, if found, else (-1) - type: int
51+
**/
52+
public int iterativeBinarySearch(int[] sortedArray, int item) {
53+
54+
// Define the first and last index of the sorted array
55+
int firstIndex = 0;
56+
int lastIndex = sortedArray.length - 1;
57+
// Define the middle index of the sorted array
58+
int middle = (firstIndex + lastIndex) / 2;
59+
60+
while (firstIndex < lastIndex) {
61+
62+
// Check if the searched item is in the middle of the array
63+
if (sortedArray[middle] == item)
64+
return middle;
65+
// If the middle element of the array is smaller than the searched item
66+
// then ignore the first half of the array
67+
if (sortedArray[middle] < item)
68+
firstIndex = middle + 1;
69+
// If the middle element of the array is bigger than the searched item
70+
// then ignore the first half of the array
71+
if (sortedArray[middle] > item)
72+
lastIndex = middle - 1;
73+
}
74+
75+
return -1;
76+
}
77+
78+
// Driver for testing
79+
/**
80+
* @param args
81+
*/
82+
public static void main(String args[]) {
83+
BinarySearch testObjBinarySearch = new BinarySearch();
84+
// Sorted array example
85+
int vector[] = { 1, 4, 11, 18, 23, 44 };
86+
int size = vector.length;
87+
88+
try (Scanner scanObj = new Scanner(System.in)) {
89+
// Read user input
90+
System.out.println("Insert an int value to search: ");
91+
int key = scanObj.nextInt();
92+
93+
int result = testObjBinarySearch.recursiveBinarySearch(vector, 0, size, key);
94+
95+
if (result == -1)
96+
System.out.println("Element not found in array!");
97+
else
98+
System.out.println("Element found at index: " + result);
99+
}
100+
}
101+
102+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.Scanner;
2+
3+
/*
4+
A Linear Search is defined as searching algorithm for finding an element within a list;
5+
it works by sequentially checking each element of the list until a match is found
6+
or the whole list has been searched, with a time complexity of O(N).
7+
*/
8+
9+
public class LinearSearch {
10+
11+
/**
12+
* "Linear Search" - Recursive variant
13+
*
14+
* @param array - list to be searched within - type: int
15+
* @param size - array lenght - type: int
16+
* @param item - item to be found - type: int
17+
* @return item index, if found, else (-1) - type: int
18+
*/
19+
int recursiveLinearSearch(int array[], int size, int item) {
20+
// If the element was not found in the whole array
21+
// then return -1
22+
if (size == 0) {
23+
return -1;
24+
} else if (array[size - 1] == item) {
25+
// else return the index of the found item
26+
return size - 1;
27+
} else {
28+
// Recursion
29+
return recursiveLinearSearch(array, size - 1, item);
30+
}
31+
}
32+
33+
/**
34+
* "Linear Search" - Iterative variant
35+
*
36+
* @param array - list to be searched within - type: int
37+
* @param item - item to be found - type: int
38+
* @return item index, if found, else (-1) - type: int
39+
*/
40+
int iterativeLinearSearch(int array[], int item) {
41+
42+
int size = array.length;
43+
// Loop on the array
44+
for (int i = 0; i < size; i++) {
45+
// If the item was found, then return its index
46+
if (array[i] == item)
47+
return i;
48+
}
49+
// else return -1
50+
return -1;
51+
}
52+
53+
// Driver for testing
54+
/**
55+
* @param args
56+
*/
57+
public static void main(String[] args) {
58+
LinearSearch testObjLinearSearch = new LinearSearch();
59+
// Sorted array example
60+
int vector[] = { 15, 5, 19, 27, 3 };
61+
int size = vector.length;
62+
63+
try (Scanner scanObj = new Scanner(System.in)) {
64+
// Read user input
65+
System.out.println("Insert an int value to search: ");
66+
int key = scanObj.nextInt();
67+
68+
int result = testObjLinearSearch.recursiveLinearSearch(vector, size, key);
69+
// int result = testObjLinearSearch.iterativeLinearSearch(vector, key);
70+
71+
if (result == -1)
72+
System.out.println("Element not found in array!");
73+
else
74+
System.out.println("Element found at index: " + result);
75+
}
76+
}
77+
78+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}

sortingalgorithms/src/BubbleSort.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list,
3+
comparing each pair of adjacent elements and swapping them if they are in the wrong order.
4+
*/
5+
6+
public class BubbleSort {
7+
8+
/**
9+
* "Bubble Sort" function
10+
*
11+
* @param array - array to be sorted - type: int
12+
*/
13+
void bubbleSort(int array[]) {
14+
15+
int size = array.length;
16+
int temp;
17+
18+
for (int i = 0; i < size - 1; i++) {
19+
for (int j = 0; j < size - i - 1; j++) {
20+
// If two consecutive elements are not sorted
21+
if (array[j] > array[j + 1]) {
22+
// then swap the two of them
23+
// by using a temp variable
24+
temp = array[j];
25+
array[j] = array[j + 1];
26+
array[j + 1] = temp;
27+
}
28+
}
29+
}
30+
}
31+
32+
// Utility function
33+
void printArray(int array[]) {
34+
35+
for (int i = 0; i < array.length; i++)
36+
System.out.print(array[i] + " ");
37+
System.out.println("\n");
38+
}
39+
40+
// Driver for testing
41+
/**
42+
* @param args
43+
*/
44+
public static void main(String args[]) {
45+
46+
BubbleSort testObjBubbleSort = new BubbleSort();
47+
48+
int vector[] = { 64, 34, 25, 12, 22, 11, 90 };
49+
50+
System.out.println("Original array: ");
51+
testObjBubbleSort.printArray(vector);
52+
System.out.println("Sorting array...\n");
53+
testObjBubbleSort.bubbleSort(vector);
54+
System.out.println("Array sorted: ");
55+
testObjBubbleSort.printArray(vector);
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Insertion Sort is a simple sorting algorithm that works by repeatedly comparing pair
3+
of elements and virtually splitting the array in a sorted sub-array and ad unsorted one.
4+
It is appropriate for data sets which are already partially sorted.
5+
*/
6+
7+
public class InsertionSort {
8+
9+
/**
10+
* "Insertion Sort" function
11+
*
12+
* @param array - array to be sorted - type: int
13+
*/
14+
public void insertionSort(int array[]) {
15+
16+
int size = array.length;
17+
18+
for (int i = 0; i < size; i++) {
19+
// Define a temp variable
20+
int temp = array[i];
21+
int j = i - 1;
22+
// Move elements that are bigger than temp
23+
// one position ahead of their current one
24+
while (j >= 0 && array[j] > temp) {
25+
array[j + 1] = array[j];
26+
j = j - 1;
27+
}
28+
array[j + 1] = temp;
29+
}
30+
}
31+
32+
// Utility function
33+
void printArray(int array[]) {
34+
35+
for (int i = 0; i < array.length; i++)
36+
System.out.print(array[i] + " ");
37+
System.out.println("\n");
38+
}
39+
40+
// Driver for testing
41+
/**
42+
* @param args
43+
*/
44+
public static void main(String args[]) {
45+
46+
InsertionSort testObjInsertionSort = new InsertionSort();
47+
48+
int vector[] = { 10, 7, 8, 9, 1, 23, 11 };
49+
50+
System.out.println("Original array: ");
51+
testObjInsertionSort.printArray(vector);
52+
System.out.println("Sorting array...\n");
53+
testObjInsertionSort.insertionSort(vector);
54+
System.out.println("Array sorted: ");
55+
testObjInsertionSort.printArray(vector);
56+
}
57+
}

0 commit comments

Comments
 (0)