Skip to content

Commit fac25b9

Browse files
author
sanghaisubham
committed
Implemented Dijkstra Algorithm in Java
2 parents b3a3117 + 1d98a52 commit fac25b9

File tree

12 files changed

+210
-1
lines changed

12 files changed

+210
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
GraphTheory&Trees/BST_JS_implementation/node_modules/
22

3+
*.o

Algorithms/Graph Algorithms/Shortest Path Algorithm/Dijkstra's Algorithm/Java/Dijkstra.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static void main (String[] args)
106106
{6, 8, 0, 0, 9},
107107
{0, 5, 7, 9, 0},
108108
};
109-
ShortestPath t = new ShortestPath();
109+
Dijkstra t = new Dijkstra();
110110
t.dijkstra(graph, 0);
111111
}
112112

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @author Patryk Jóźwiak
3+
*/
4+
function linearSearch(haystack, needle) {
5+
var len = haystack.length;
6+
// Go through all values
7+
for (var i = 0; i < len; i++) {
8+
// Check if current value is equel to search value
9+
if (haystack[i] === needle) return i;
10+
}
11+
return -1;
12+
}
13+
14+
var haystack = [1, 2, 3, 4, 9, 51, 54, 65, 45, 77];
15+
console.log( linearSearch(haystack, 65) );
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class LinearSearch():
2+
def search(self, haystack, needle):
3+
length = len(haystack)
4+
# go through all values
5+
for i in range(length-1):
6+
# check if current value is search value
7+
if haystack[i] == needle:
8+
return i
9+
return -1
10+
11+
ls = LinearSearch()
12+
haystack = [2, 3, 5, 7, 11, 13, 17, 19]
13+
14+
print( ls.search(haystack, 7) )
15+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
3+
#include "bubbleSort.h"
4+
5+
static void swap(int *p, int *q)
6+
{
7+
int t = *p;
8+
*p = *q;
9+
*q = t;
10+
}
11+
12+
void bubbleSort(int * arr, const size_t sz)
13+
{
14+
int swapped = 1;
15+
while(swapped) {
16+
swapped = 0;
17+
for(int i = 0; i < sz - 1; ++i) {
18+
if(arr[i] > arr[i+1]) {
19+
swap(arr + i, arr + i + 1);
20+
swapped = 1;
21+
}
22+
}
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _BUBBLE_SORT_H_
2+
#define _BUBBLE_SORT_H_
3+
4+
#include <stddef.h>
5+
6+
void bubbleSort(int * array, const size_t size);
7+
8+
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
3+
#include "bubbleSort.h"
4+
5+
#define MAX_ELEMENTS 100 // Maximum Elements that the user test function can handle
6+
7+
static void printArray(int * array, const size_t size);
8+
void test_1();
9+
void user_test();
10+
11+
int main(void)
12+
{
13+
test_1();
14+
user_test();
15+
return 0;
16+
}
17+
18+
// A function to print elements of an array separated by a tab
19+
static void printArray(int * array, const size_t size)
20+
{
21+
for(int i = 0; i < size; ++i) {
22+
printf("%d\t", array[i]);
23+
}
24+
}
25+
26+
/*
27+
* Some Test cases to make sure the sorting algorithm is working
28+
* properly.
29+
*/
30+
31+
void test_1()
32+
{
33+
int tst[] = {12, 0, 90, 31498, 1243, -1435, -1};
34+
35+
printf("Initial Array : ");
36+
printArray(tst, 7);
37+
printf("\n");
38+
39+
bubbleSort(tst, 7);
40+
41+
printf("Sorted Array : ");
42+
printArray(tst, 7);
43+
printf("\n");
44+
}
45+
46+
void user_test()
47+
{
48+
int tst[MAX_ELEMENTS];
49+
50+
printf("Enter number of elements you would like to enter : ");
51+
int n;
52+
scanf("%d", &n);
53+
printf("Enter elements of array :\n");
54+
for(int i = 0; i < n; ++i) {
55+
scanf("%d", &tst[i]);
56+
}
57+
58+
printf("Initial Array : ");
59+
printArray(tst, n);
60+
printf("\n");
61+
62+
bubbleSort(tst, n);
63+
64+
printf("Sorted Array : ");
65+
printArray(tst, n);
66+
printf("\n");
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Bubble Sort Algorithm
2+
----------------------
3+
4+
Bubble Sort Algorithm is a sorting algorithm with O(n^2) complexity. In Bubble Sort algorithm, the elements are sorted by iterating through the whole array and swapping two consecutive elements if they are not in order untill the swap during the iteration becomes zero. At this point, the array becomes sorted. The worst case for bubble sort algorithm to complete is to go through n^2 iterations.
5+
6+
To find more about Bubble Sort Algorithm :
7+
https://en.wikipedia.org/wiki/Bubble_sort
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def bubble_sort(array)
2+
swapped = true # This keeps track of when the algo has swapped
3+
n = array.length - 1 # This is for a later loop
4+
5+
# The sort
6+
while swapped do
7+
swapped = false # Reset the var
8+
n.times do [i]
9+
if array[i] > array[i+1] # Checks if items need swapping
10+
array[i], array[i+1] = array[i+1], array[i] # Does the swap
11+
# We have to do the swap on one line so no entry gets overwritten
12+
# Otherwise we need a third variable to store data temporarily
13+
swapped = true # Marks the swap
14+
end
15+
end
16+
end
17+
array # Return the sorted array
18+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module InsertionSort where
2+
3+
sort :: Ord a => [a] -> [a]
4+
sort [] = []
5+
sort (x:xs) = sortedInsert x $ sort xs where
6+
sortedInsert :: Ord a => a -> [a] -> [a]
7+
sortedInsert z [] = [z]
8+
sortedInsert z (y:ys) = if z <= y then z:y:ys else y:(sortedInsert z ys)

0 commit comments

Comments
 (0)