Skip to content

Commit 8db7087

Browse files
author
Deepak Malik
committed
Sorting
1 parent 9debc93 commit 8db7087

File tree

7 files changed

+559
-1
lines changed

7 files changed

+559
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# Algorithms-In-Java
1+
## Algorithms-In-Java
22
Implementation of Algorithms in Java
3+
4+
1. Searching
5+
- [Linear Search](../master/src/com/deepak/algorithms/Searching/LinearSearch.java)
6+
- [Binary Search](../master/src/com/deepak/algorithms/Searching/BinarySearch.java)
7+
8+
2. Sorting
9+
- [Time and Space Complexity](../master/src/com/deepak/algorithms/Sorting/TimeAndSpaceComplexity.md)
10+
- [Selection Sort](../master/src/com/deepak/algorithms/Sorting/SelectionSort.java)
11+
- [Insertion Sort](../master/src/com/deepak/algorithms/Sorting/InsertionSort.java)
12+
- [Bubble Sort](../master/src/com/deepak/algorithms/Sorting/BubbleSort.java)
13+
- [Merge Sort](../master/src/com/deepak/algorithms/Sorting/MergeSort.java)
14+
- [Counting Sort](../master/src/com/deepak/algorithms/Sorting/CountingSort.java)
15+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Algorithms-in-Java
3+
* BubbleSort.java
4+
*/
5+
package com.deepak.algorithms.Sorting;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Class for BubbleSort implementation
11+
* @author Deepak
12+
*/
13+
public class BubbleSort {
14+
15+
/**
16+
* Main method to start the flow of program
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
21+
System.out.println("******************* BUBBLE - SORT *******************");
22+
performBubbleSort(valuesToBeSorted);
23+
}
24+
25+
/**
26+
* Bubble Sort implementation
27+
* <p> Question - When will you consider a list of items to be sorted?
28+
* Answer - When all the elements to the left of each element are smaller then the element
29+
*
30+
* This algorithm works on this principle. We start this Algorithm from index = 1, because element
31+
* at index 0 is already considered as sorted since there are no elements on its left side.
32+
* In this algorithm, we pick up each item and compares it to the item positioned on its left.
33+
* If picked up item is smaller then the left item, then it is in wrong position.
34+
* We swap it and keep going on until the items are finished.</p>
35+
*
36+
* <p>How it works?
37+
* 1. Loop through the entire collection of elements n-1 times.
38+
* We start with N = 1 here, as explained above (Assume first element is already sorted in the collection)
39+
* 2. Loop through rest of the collection again to keep comparing each of the item
40+
* 3. If item on the left is smaller then the item on the right, swap it
41+
* 4. Repeat 1-3 till we get sorted collection back</p>
42+
*
43+
* <p>Time Complexity</p>
44+
* Whenever there are inner loops associated, complexity is n^2. In this case,
45+
* Best - O(n)
46+
* Average - O(n^2)
47+
* Worst - O(n^2)
48+
*
49+
* @param listOfValues - List of values passed in the request
50+
*/
51+
private static void performBubbleSort(int[] listOfValues) {
52+
for (int i = 0; i < listOfValues.length; i++) {
53+
for (int j = 1; j < (listOfValues.length - i); j++) {
54+
if (listOfValues[j - 1] > listOfValues[j]) {
55+
swap(listOfValues, j - 1, j);
56+
}
57+
}
58+
}
59+
Arrays.stream(listOfValues).forEach(System.out::println);
60+
}
61+
62+
/**
63+
* Method to swap 2 values
64+
* @param values
65+
* @param firstValue
66+
* @param secondValue
67+
*/
68+
private static void swap(int[] values, int firstValue, int secondValue) {
69+
int tempValue = values[firstValue];
70+
values[firstValue] = values[secondValue];
71+
values[secondValue] = tempValue;
72+
}
73+
74+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Algorithms-in-Java
3+
* CountingSort.java
4+
*/
5+
package com.deepak.algorithms.Sorting;
6+
7+
/**
8+
* Class for CountingSort implementation
9+
* @author Deepak
10+
*/
11+
public class CountingSort {
12+
13+
/**
14+
* Main method to start the flow of program
15+
* @param args
16+
*/
17+
public static void main(String[] args) {
18+
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
19+
System.out.println("******************* COUNTING - SORT *******************");
20+
performCountingSort(valuesToBeSorted);
21+
}
22+
23+
private static void performCountingSort(int[] values) {
24+
25+
}
26+
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Algorithms-in-Java
3+
* InsertionSort.java
4+
*/
5+
package com.deepak.algorithms.Sorting;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Class for InsertionSort implementation
11+
* @author Deepak
12+
*/
13+
public class InsertionSort {
14+
15+
/**
16+
* Main method to start the flow of program
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
21+
System.out.println("******************* INSERTION - SORT *******************");
22+
performInsertionSort(valuesToBeSorted);
23+
}
24+
25+
/**
26+
* Insertion Sort implementation
27+
* <p> Question - When will you consider a list of items to be sorted?
28+
* Answer - When all the elements to the left of each element are smaller then the element
29+
*
30+
* This algorithm works on this principle. We start this Algorithm from index = 1, because element
31+
* at index 0 is already considered as sorted since there are no elements on its left side.
32+
* In this algorithm, we pick up each item and compares it to the item positioned on its left.
33+
* If picked up item is smaller then the left item, then it is in wrong position.
34+
* We swap it and keep going on until the items are finished.</p>
35+
*
36+
* <p>How it works?
37+
* 1. Loop through the entire collection of elements n-1 times.
38+
* We start with N = 1 here, as explained above (Assume first element is already sorted in the collection)
39+
* 2. Loop through rest of the collection again to keep comparing each of the item
40+
* 3. If item on the left is smaller then the item on the right, swap it
41+
* 4. Repeat 1-3 till we get sorted collection back</p>
42+
*
43+
* <p>Time Complexity</p>
44+
* Whenever there are inner loops associated, complexity is n^2. In this case,
45+
* Best - O(n)
46+
* Average - O(n^2)
47+
* Worst - O(n^2)
48+
*
49+
* @param listOfValues - List of values passed in the request
50+
*/
51+
private static void performInsertionSort(int[] listOfValues) {
52+
for (int i = 1; i < listOfValues.length; i++) {
53+
for (int j = i; j > 0; j--) {
54+
if (listOfValues[j] < listOfValues[j-1]) {
55+
swap(listOfValues, j, j-1);
56+
}
57+
}
58+
}
59+
Arrays.stream(listOfValues).forEach(System.out::println);
60+
}
61+
62+
/**
63+
* Method to swap 2 values
64+
* @param values
65+
* @param firstValue
66+
* @param secondValue
67+
*/
68+
private static void swap(int[] values, int firstValue, int secondValue) {
69+
int tempValue = values[firstValue];
70+
values[firstValue] = values[secondValue];
71+
values[secondValue] = tempValue;
72+
}
73+
74+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Algorithms-in-Java
3+
* MergeSort.java
4+
*/
5+
package com.deepak.algorithms.Sorting;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Class for MergeSort implementation
11+
* @author Deepak
12+
*/
13+
public class MergeSort {
14+
15+
/**
16+
* Main method to start the flow of program
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
21+
System.out.println("******************* MERGE - SORT *******************");
22+
int[] sortedValues = performMergeSort(valuesToBeSorted, 0, valuesToBeSorted.length - 1);
23+
Arrays.stream(sortedValues).forEach(System.out::println);
24+
}
25+
26+
/**
27+
* Merge Sort implementation
28+
*
29+
* <p>Time Complexity</p>
30+
* Whenever there are inner loops associated, complexity is n^2. In this case,
31+
* Best - O(n log(n))
32+
* Average - O(n log(n))
33+
* Worst - O(n log(n))
34+
*
35+
* @param listOfValues - List of values passed in the request
36+
*/
37+
public static int[] performMergeSort(int[] iListOfValues, int iLowestPosition, int iHighestPosition) {
38+
if (iLowestPosition < iHighestPosition) {
39+
int middlePosition = iLowestPosition + (iHighestPosition - iLowestPosition) / 2;
40+
performMergeSort(iListOfValues, iLowestPosition, middlePosition);
41+
performMergeSort(iListOfValues, middlePosition + 1, iHighestPosition);
42+
merge(iListOfValues, iLowestPosition, middlePosition, iHighestPosition);
43+
}
44+
return iListOfValues;
45+
}
46+
47+
/**
48+
* Method to merge two items from the list
49+
* @param iListOfValues
50+
* @param iLowestPosition
51+
* @param middlePosition
52+
* @param iHighestPosition
53+
* @return {@link int[]}
54+
*/
55+
private static int[] merge(int[] iListOfValues, int iLowestPosition, int middlePosition, int iHighestPosition) {
56+
int[] copyOfArrays = new int[iListOfValues.length];
57+
for (int i = 0; i < iListOfValues.length; i++ ) {
58+
copyOfArrays[i] = iListOfValues[i];
59+
}
60+
61+
int i = iLowestPosition;
62+
int j = middlePosition + 1;
63+
int k = iLowestPosition;
64+
65+
while (i <= middlePosition && j <= iHighestPosition) {
66+
if (copyOfArrays[i] <= copyOfArrays[j]) {
67+
iListOfValues[k] = copyOfArrays[i];
68+
i++;
69+
} else {
70+
iListOfValues[k] = copyOfArrays[j];
71+
j++;
72+
}
73+
k++;
74+
}
75+
while (i <= middlePosition) {
76+
iListOfValues[k] = copyOfArrays[i];
77+
k++;
78+
i++;
79+
}
80+
return iListOfValues;
81+
}
82+
83+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Algorithms-in-Java
3+
* SelectionSort.java
4+
*/
5+
package com.deepak.algorithms.Sorting;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Class for SelectionSort implementation
11+
* @author Deepak
12+
*/
13+
public class SelectionSort {
14+
15+
/**
16+
* Main method to start the flow of program
17+
* @param args
18+
*/
19+
public static void main(String[] args) {
20+
int[] valuesToBeSorted = {7, 10, 47, 40, 83, 84, 65, 61, 32, 55, 49, 46, 25, 20, 93, 63, 54, 10};
21+
System.out.println("******************* SELECTION - SORT *******************");
22+
performSelectionSort(valuesToBeSorted);
23+
}
24+
25+
/**
26+
* Selection Sort implementation
27+
* <p> This Algorithm divides the array into two imaginary arrays i.e one with sorted elements,
28+
* and other one with unsorted elements. Initially sorted array will be empty, while unsorted
29+
* one contains the whole array.
30+
* At every step algorithm finds minimal element from the unsorted array and adds it to the end
31+
* of sorted one. When unsorted part becomes empty algorithm stops</p>
32+
*
33+
* <p>How it works?
34+
* 1. Loop through the entire collection of elements n-1 times.
35+
* We need n-1 passes here because second last pass will sort entire collection.
36+
* 2. Assume first element is the smallest one in the collection.
37+
* 3. Loop through rest of the unsorted collection and find the minimum element.
38+
* If found, replace it with the minimum we choose at the beginning.
39+
* 4. Keep doing it until we finish the inner loop i.e all unsorted elements.
40+
* 5. Now, swap the minimum value with the value from where parent loop started
41+
* 6. Repeat 2-5 till we get sorted collection back</p>
42+
*
43+
* <p>Time Complexity</p>
44+
* Whenever there are inner loops associated, complexity is n^2. In this case,
45+
* Best - O(n^2)
46+
* Average - O(n^2)
47+
* Worst - O(n^2)
48+
*
49+
* @param listOfValues - List of values passed in the request
50+
*/
51+
private static void performSelectionSort(int[] listOfValues) {
52+
for (int i = 0; i < listOfValues.length - 1; i++) {
53+
int positionHoldingMinimumValue = i;
54+
for (int j = i + 1; j < listOfValues.length; j++) {
55+
if (listOfValues[j] < listOfValues[positionHoldingMinimumValue]) {
56+
positionHoldingMinimumValue = j;
57+
}
58+
}
59+
swap(listOfValues, positionHoldingMinimumValue, i);
60+
}
61+
Arrays.stream(listOfValues).forEach(System.out::println);
62+
}
63+
64+
/**
65+
* Method to swap 2 values
66+
* @param values
67+
* @param firstValue
68+
* @param secondValue
69+
*/
70+
private static void swap(int[] values, int firstValue, int secondValue) {
71+
int tempValue = values[firstValue];
72+
values[firstValue] = values[secondValue];
73+
values[secondValue] = tempValue;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)