├── README.md
├── bin
└── Sorting
│ ├── Insertion.class
│ ├── MergeSort.class
│ ├── QuickSort.class
│ └── Selection.class
├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
└── src
└── Sorting
├── Selection.java
├── Insertion.java
├── MergeSort.java
└── QuickSort.java
/README.md:
--------------------------------------------------------------------------------
1 | Data Structures
2 | ==============
3 |
4 | Data structures and algorithms in Java
5 |
--------------------------------------------------------------------------------
/bin/Sorting/Insertion.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karan/DataStructures/master/bin/Sorting/Insertion.class
--------------------------------------------------------------------------------
/bin/Sorting/MergeSort.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karan/DataStructures/master/bin/Sorting/MergeSort.class
--------------------------------------------------------------------------------
/bin/Sorting/QuickSort.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karan/DataStructures/master/bin/Sorting/QuickSort.class
--------------------------------------------------------------------------------
/bin/Sorting/Selection.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karan/DataStructures/master/bin/Sorting/Selection.class
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | DataStructures
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.6
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.6
12 |
--------------------------------------------------------------------------------
/src/Sorting/Selection.java:
--------------------------------------------------------------------------------
1 | package Sorting;
2 |
3 | /*
4 | * Implementation of selection sort algorithm.
5 | *
6 | * General working:
7 | * Starting at index i, swap the value at index i with
8 | * the smallest value in the rest of the list.
9 | *
10 | * Complexity: O(n^2)
11 | * Swaps: n
12 | */
13 |
14 | public class Selection {
15 |
16 | public static void main(String[] args) {
17 | int[] a = {3, -6, 32, 0, -23, 0, 1, 2};
18 | selection_sort(a);
19 | print_list(a);
20 | }
21 |
22 | private static void print_list(int[] a) {
23 | for (int v : a) {
24 | System.out.print(v + " ");
25 | }
26 | }
27 |
28 | private static void selection_sort(int[] a) {
29 | for (int i = 0; i < a.length; i++) {
30 | for (int j = i + 1; j < a.length; j++) {
31 | if (a[j] < a[i]) {
32 | // swap
33 | int temp = a[j];
34 | a[j] = a[i];
35 | a[i] = temp;
36 | }
37 | }
38 | }
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/Sorting/Insertion.java:
--------------------------------------------------------------------------------
1 | package Sorting;
2 |
3 | /*
4 | * Implementation of insertion sort algorithm.
5 | *
6 | * General working:
7 | * Maintain two sublists, one being sorted, other being
8 | * unsorted. Place each element from unsorted part into
9 | * correct position in sorted list.
10 | *
11 | * Complexity: O(n^2) (avg and best)
12 | * Swaps: n
13 | */
14 |
15 | public class Insertion {
16 |
17 | public static void main(String[] args) {
18 | int[] a = {3, -6, 32, 0, -23, 0, 1, 2};
19 | insertion_sort(a);
20 | print_list(a);
21 | }
22 |
23 | private static void print_list(int[] a) {
24 | for (int v : a) {
25 | System.out.print(v + " ");
26 | }
27 | }
28 |
29 | private static void insertion_sort(int[] a) {
30 | for (int i = 0; i < a.length; i++) {
31 | int num = a[i]; // the pointer that separates the two lists
32 | int j = i; // j keeps track of sorted part
33 | while (j > 0 && num < a[j - 1]) {
34 | // we have a number less than previous number
35 | a[j] = a[j - 1]; // shift to the right
36 | j--; // move j back a step
37 | }
38 | a[j] = num; // put the number at right place
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/Sorting/MergeSort.java:
--------------------------------------------------------------------------------
1 | package Sorting;
2 |
3 | public class MergeSort {
4 |
5 | /*
6 | * Implementation of merge-sort algorithm.
7 | *
8 | * General working:
9 | * Divide and conquer. Divide array into two parts
10 | * and sort them recursively, and then merge them.
11 | *
12 | * Complexity: O(n*logn) (worst)
13 | */
14 |
15 | public static void main(String[] args) {
16 | int[] a = {3, -6, 32, 0, -23, 0, 1, 2};
17 | merge_sort(a);
18 | print_list(a);
19 | }
20 |
21 | private static void print_list(int[] a) {
22 | for (int v : a) {
23 | System.out.print(v + " ");
24 | }
25 | }
26 |
27 | private static void merge_sort(int[] a) {
28 | if (a.length > 1) {
29 | // recursively merge sort array
30 | int[] left = left(a);
31 | int[] right = right(a);
32 |
33 | merge_sort(left);
34 | merge_sort(right);
35 |
36 | merge(a, left, right);
37 | }
38 | }
39 |
40 | // returns left half of a array
41 | private static int[] left(int[] a) {
42 | int[] left = new int[a.length / 2];
43 | for (int i = 0; i < left.length; i++) {
44 | left[i] = a[i];
45 | }
46 | return left;
47 | }
48 |
49 | // returns right half of the array
50 | private static int[] right(int[] a) {
51 | int size = a.length / 2;
52 | int[] right = new int[size];
53 | for (int i = 0; i < size; i++) {
54 | right[i] = a[i + size];
55 | }
56 | return right;
57 | }
58 |
59 | // merges the left and right array into the given array
60 | private static void merge(int[] a, int[] left, int[] right) {
61 | int l = 0; // index for left
62 | int r = 0; // index for right
63 |
64 | for (int i = 0; i < a.length; i++) {
65 | if (r >= right.length || (l < left.length && left[l] < right[r])) {
66 | a[i] = left[l];
67 | l++;
68 | } else {
69 | a[i] = right[r];
70 | r++;
71 | }
72 | }
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/src/Sorting/QuickSort.java:
--------------------------------------------------------------------------------
1 | package Sorting;
2 |
3 | import java.util.LinkedList;
4 | import java.util.List;
5 | import java.util.Random;
6 |
7 |
8 | public class QuickSort {
9 |
10 | /*
11 | * Implementation of quick-sort algorithm.
12 | *
13 | * General working:
14 | * Divide and conquer. Divide array into two parts
15 | * based on value of a random pivot. Do this recursively
16 | * on the sublists.
17 | *
18 | * Complexity: O(n*logn) (average), O(n^2) (worst)
19 | */
20 |
21 | public static void main(String[] args) {
22 | int[] a = {3, -6, 32, 0, -23, 0, 1, 2};
23 | a = quick_sort(a);
24 | print_list(a);
25 | }
26 |
27 | private static void print_list(int[] a) {
28 | for (int v : a) {
29 | System.out.print(v + " ");
30 | }
31 | }
32 |
33 | private static int[] quick_sort(int[] a) {
34 | if (a.length > 1) {
35 | List less = new LinkedList();
36 | List more = new LinkedList();
37 | List equal = new LinkedList();
38 |
39 | Random rand = new Random();
40 | int pivot_index = rand.nextInt(a.length); // get pivot index (0 to len-1)
41 | int pivot = a[pivot_index]; // that's the pivot value
42 |
43 | // sort the list into 3 lists
44 | for (int i = 0; i < a.length; i++) {
45 | if (a[i] < pivot) {
46 | less.add(a[i]);
47 | } else if (a[i] > pivot) {
48 | more.add(a[i]);
49 | } else {
50 | equal.add(a[i]);
51 | }
52 | }
53 |
54 | // and then recursively sort less and more
55 | less = array_to_list(quick_sort(linked_to_array(less)));
56 | more = array_to_list(quick_sort(linked_to_array(more)));
57 |
58 | // merge the lists
59 | less.addAll(equal);
60 | less.addAll(more);
61 |
62 | // and return it
63 | return linked_to_array(less);
64 | }
65 | return a;
66 | }
67 |
68 | /*
69 | * Converts a List of Integers to an array.
70 | */
71 | private static int[] linked_to_array(List list) {
72 | int[] arr = new int[list.size()];
73 | for (int i = 0; i < list.size(); i++)
74 | arr[i] = list.get(i);
75 | return arr;
76 | }
77 |
78 | /*
79 | * Converts an array of ints to a LinkedList
80 | */
81 | private static List array_to_list(int[] arr) {
82 | List list = new LinkedList();
83 | for (int i = 0; i < arr.length; i++) {
84 | list.add(arr[i]);
85 | }
86 | return list;
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------