Skip to content

Commit 77ab7ce

Browse files
committed
Sorting algorithms
1 parent f46c59e commit 77ab7ce

File tree

8 files changed

+184
-166
lines changed

8 files changed

+184
-166
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Implementation of Algorithms in Java, Most of these names are picked up from Gee
1616
- [X] [Merge Sort](../master/src/com/deepak/algorithms/Sorting/MergeSort.java)
1717
- [X] [Counting Sort](../master/src/com/deepak/algorithms/Sorting/CountingSort.java)
1818
- [ ] Shell Sort
19-
- [ ] Quick Sort
19+
- [X] [Quick Sort](../master/src/com/deepak/algorithms/Sorting/QuickSort.java)
2020
- [ ] Heap Sort
2121
- [ ] Radix Sort
2222

src/com/deepak/algorithms/Bit_Manipulation/UsefulOperations.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static int countSetBits(int a) {
126126
* @param b - Second Number
127127
* @return {@link Map<String, Integer>} - Result
128128
*/
129-
public Map<String, Integer> swap2Integers(int a, int b) {
129+
public static Map<String, Integer> swap2Integers(int a, int b) {
130130
Map<String, Integer> result = new HashMap<>();
131131
a ^= b;
132132
b ^= a;

src/com/deepak/algorithms/Sorting/BubbleSort.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import com.deepak.algorithms.Library.ArrayUtils;
88

99
/**
10-
* Class for BubbleSort implementation
10+
* BubbleSort Implementation
11+
*
1112
* @author Deepak
1213
*/
1314
public class BubbleSort {
@@ -36,17 +37,20 @@ public class BubbleSort {
3637
* Average - O(n^2)
3738
* Worst - O(n^2)
3839
*
39-
* @param listOfValues - List of values passed in the request
40+
* @param list - List of values passed in the request
4041
*/
41-
public static Integer[] performBubbleSort(Integer[] listOfValues) {
42-
for (int i = 0; i < listOfValues.length; i++) {
43-
for (int j = 1; j < (listOfValues.length - i); j++) {
44-
if (listOfValues[j - 1] > listOfValues[j]) {
45-
ArrayUtils.swap(listOfValues, j - 1, j);
42+
public static Integer[] performBubbleSort(Integer[] list) {
43+
/* Loop through the entire collection */
44+
for (int i = 0; i < list.length; i++) {
45+
/* Loop through rest of the elements */
46+
for (int j = 1; j < (list.length - i); j++) {
47+
/* If previous is greater then the current, swap it */
48+
if (list[j - 1] > list[j]) {
49+
ArrayUtils.swap(list, j - 1, j);
4650
}
4751
}
4852
}
49-
return listOfValues;
53+
return list;
5054
}
5155

5256
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
2-
* Algorithms-in-Java
2+
* Algorithms-In-Java
33
* CountingSort.java
44
*/
55
package com.deepak.algorithms.Sorting;
66

77
/**
8-
* Class for CountingSort implementation
8+
* Counting Sort Implementation
9+
*
910
* @author Deepak
1011
*/
1112
public class CountingSort {
@@ -24,55 +25,42 @@ public static void main(String[] args) {
2425
* Counting sort is a sorting technique based on keys between a specific range.
2526
* It works by counting the number of objects having distinct key values (kind of hashing).
2627
* Then doing some arithmetic to calculate the position of each object in the output sequence.
28+
*
2729
* <p>Notes:
28-
* 1. Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted.
29-
* Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
30-
2. It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the range of data.
31-
3. It is often used as a sub-routine to another sorting algorithm like radix sort.
32-
4. Counting sort uses a partial hashing to count the occurrence of the data object in O(1).
33-
5. Counting sort can be extended to work for negative inputs also.
34-
6. This type of integer sorting algorithms are usually designed to work in either the pointer machine or random access machine models of computing
35-
</p>
30+
* 1. Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted.
31+
* - Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
32+
* 2. It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the range of data.
33+
* 3. It is often used as a sub-routine to another sorting algorithm like radix sort.
34+
* 4. Counting sort uses a partial hashing to count the occurrence of the data object in O(1).
35+
* 5. Counting sort can be extended to work for negative inputs also.
36+
* 6. This type of integer sorting algorithms are usually designed to work in either the pointer machine or random access machine models of computing
37+
* </p>
3638
*/
37-
private static void performCountingSort(int[] values) {
38-
39-
40-
int n = values.length;
41-
42-
// The output character array that will have sorted arr
43-
int output[] = new int[n];
44-
45-
// Create a count array to store count of inidividul
46-
// characters and initialize count array as 0
47-
int count[] = new int[256];
48-
for (int i=0; i<256; ++i)
49-
count[i] = 0;
50-
51-
// store count of each character
52-
for (int i=0; i<n; ++i)
53-
++count[values[i]];
54-
55-
// Change count[i] so that count[i] now contains actual
56-
// position of this character in output array
57-
for (int i=1; i<=255; ++i)
58-
count[i] += count[i-1];
59-
60-
// Build the output character array
61-
for (int i = 0; i<n; ++i)
62-
{
63-
output[count[values[i]]-1] = values[i];
64-
--count[values[i]];
65-
}
66-
67-
// Copy the output array to arr, so that arr now
68-
// contains sorted characters
69-
for (int i = 0; i<n; ++i)
70-
values[i] = output[i];
71-
72-
System.out.print("Sorted array is ");
73-
for (int i=0; i<values.length; ++i)
74-
System.out.print(values[i]+",");
39+
private static void performCountingSort(int[] values) {
40+
/* Find the length of the collection */
41+
int n = values.length;
42+
/* Output character array which will hold the sorted array */
43+
int output[] = new int[n];
44+
/* Create a count array of 256 size, and store the count of each character in it */
45+
int count[] = new int[256];
46+
for (int i = 0; i < n; ++i) {
47+
++count[values[i]];
48+
}
49+
/* Change count[i] so that count[i] now contains actual position of the character */
50+
for (int i = 1; i <= 255; ++i) {
51+
count[i] += count[i-1];
52+
}
53+
/* Build the output character array */
7554

55+
// Build the output character array
56+
for (int i = 0; i<n; ++i) {
57+
output[count[values[i]] - 1] = values[i];
58+
--count[values[i]];
59+
}
60+
System.out.print("Sorted array is : ");
61+
for (int i = 0; i < output.length; ++i) {
62+
System.out.print(output[i]+", ");
63+
}
7664
}
7765

7866
}

src/com/deepak/algorithms/Sorting/InsertionSort.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import com.deepak.algorithms.Library.ArrayUtils;
88

99
/**
10-
* Insertion sort implementation
10+
* Insertion Sort Implementation
1111
*
1212
* @author Deepak
1313
*/
1414
public class InsertionSort {
1515

1616
/**
1717
* Insertion Sort implementation
18+
*
1819
* <p> Question - When will you consider a list of items to be sorted?
1920
* Answer - When all the elements to the left of each element are smaller then the element
2021
*
@@ -37,17 +38,21 @@ public class InsertionSort {
3738
* Average - O(n^2)
3839
* Worst - O(n^2)
3940
*
40-
* @param listOfValues - List of values passed in the request
41+
* @param list - List of values passed in the request
4142
*/
42-
public static Integer[] performInsertionSort(Integer[] listOfValues) {
43-
for (int i = 1; i < listOfValues.length; i++) {
43+
public static Integer[] performInsertionSort(Integer[] list) {
44+
/* Loop through the items starting from 1.
45+
* 1st element is always considered as sorted */
46+
for (int i = 1; i < list.length; i++) {
47+
/* Loop through the rest of the elements and compare each with the previous one.
48+
* If small, swap it */
4449
for (int j = i; j > 0; j--) {
45-
if (listOfValues[j] < listOfValues[j - 1]) {
46-
ArrayUtils.swap(listOfValues, j, j - 1);
50+
if (list[j] < list[j - 1]) {
51+
ArrayUtils.swap(list, j, j - 1);
4752
}
4853
}
4954
}
50-
return listOfValues;
55+
return list;
5156
}
5257

5358
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
/**
2-
* Algorithms-in-Java
2+
* Algorithms-In-Java
33
* MergeSort.java
44
*/
55
package com.deepak.algorithms.Sorting;
66

77
import java.util.Arrays;
88

99
/**
10-
* Class for MergeSort implementation
10+
* MergeSort Implementation
11+
*
1112
* @author Deepak
1213
*/
1314
public class MergeSort {
1415

1516
/**
1617
* Main method to start the flow of program
18+
*
1719
* @param args
1820
*/
1921
public static void main(String[] args) {
@@ -32,52 +34,61 @@ public static void main(String[] args) {
3234
* Average - O(n log(n))
3335
* Worst - O(n log(n))
3436
*
35-
* @param listOfValues - List of values passed in the request
37+
* @param list - List of values to be sorted
38+
* @param low - lowest index i.e 0
39+
* @param high - highest index i.e list.length - 1
3640
*/
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);
41+
public static int[] performMergeSort(int[] list, int low, int high) {
42+
/* If low is less then high, find the middle position,
43+
* and perform merge sort on both the sides of the list.
44+
* Finally, merge the two sorted parts */
45+
if (low < high) {
46+
int middle = low + (high - low) / 2;
47+
/* Merge sort of left side */
48+
performMergeSort(list, low, middle);
49+
/* Merge sort on right side */
50+
performMergeSort(list, middle + 1, high);
51+
merge(list, low, middle, high);
4352
}
44-
return iListOfValues;
53+
return list;
4554
}
4655

4756
/**
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[]}
57+
* Method to merge a list
58+
*
59+
* @param list
60+
* @param low
61+
* @param middle
62+
* @param high
63+
* @return {@link int[]}
5464
*/
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];
65+
private static int[] merge(int[] list, int low, int middle, int high) {
66+
/* Create a new copy of array and fill it with same elements */
67+
int[] copy = new int[list.length];
68+
for (int i = 0; i < list.length; i++ ) {
69+
copy[i] = list[i];
5970
}
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];
71+
/* Find i, j and k */
72+
int i = low;
73+
int j = middle + 1;
74+
int k = low;
75+
/* TODO : Explain what is happening here */
76+
while (i <= middle && j <= high) {
77+
if (copy[i] <= copy[j]) {
78+
list[k] = copy[i];
6879
i++;
6980
} else {
70-
iListOfValues[k] = copyOfArrays[j];
81+
list[k] = copy[j];
7182
j++;
7283
}
7384
k++;
7485
}
75-
while (i <= middlePosition) {
76-
iListOfValues[k] = copyOfArrays[i];
86+
while (i <= middle) {
87+
list[k] = copy[i];
7788
k++;
7889
i++;
7990
}
80-
return iListOfValues;
91+
return list;
8192
}
8293

8394
}

0 commit comments

Comments
 (0)