From b3e82bb7f9e15016f4ce642337718dcb8a84af2c Mon Sep 17 00:00:00 2001 From: agampreetsingh Date: Tue, 2 Oct 2018 19:01:07 +0530 Subject: [PATCH 1/2] Adding counting sort --- Sorting algorithms/CountingSort.java | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Sorting algorithms/CountingSort.java diff --git a/Sorting algorithms/CountingSort.java b/Sorting algorithms/CountingSort.java new file mode 100644 index 0000000..932ea8d --- /dev/null +++ b/Sorting algorithms/CountingSort.java @@ -0,0 +1,52 @@ +// Java implementation of Counting Sort +class CountingSort +{ + void sort(char arr[]) + { + int n = arr.length; + + // The output character array that will have sorted arr + char output[] = new char[n]; + + // Create a count array to store count of inidividul + // characters and initialize count array as 0 + int count[] = new int[256]; + for (int i=0; i<256; ++i) + count[i] = 0; + + // store count of each character + for (int i=0; i=0; i--) + { + output[count[arr[i]]-1] = arr[i]; + --count[arr[i]]; + } + + // Copy the output array to arr, so that arr now + // contains sorted characters + for (int i = 0; i Date: Tue, 2 Oct 2018 19:03:19 +0530 Subject: [PATCH 2/2] Adding bubblesort and selection sort --- Sorting algorithms/bubbleSort.java | 125 ++++++++++++++++++++++++++ Sorting algorithms/selectionSort.java | 63 +++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 Sorting algorithms/bubbleSort.java create mode 100644 Sorting algorithms/selectionSort.java diff --git a/Sorting algorithms/bubbleSort.java b/Sorting algorithms/bubbleSort.java new file mode 100644 index 0000000..2e72b80 --- /dev/null +++ b/Sorting algorithms/bubbleSort.java @@ -0,0 +1,125 @@ +/* + Solved By: Sandeep Ranjan (1641012352) + + Solution for Programming Projects + 3.1, 3.4 +*/ + + +class ArrayBub { + private long[] a; + private int nElems; + + public ArrayBub(int max) { + a = new long[max]; + nElems = 0; + } + + public void insert(long value) { + a[nElems] = value; + nElems++; + } + + public void display() { + for(int j=0; j1; out--) + for(in=0; in a[in+1] ) + swap(in, in+1); + } + + + public void biDirectionBubbleSort() { + int out=nElems-1, in, _out=0; + + boolean isSwap = true; + + while(isSwap) { + isSwap = false; + + for(in=_out; in a[in+1] ) { + swap(in, in+1); + isSwap=true; + } + } + + for(in=out-1; in>_out; in--) { + if(a[in] < a[in-1]) { + swap(in, in-1); + isSwap=true; + } + } + + _out++; + out--; + } + + } + + public void oddEvenSort() { + boolean isSwap=true; + + while(isSwap) { + isSwap = false; + + for(int in=1; in a[in+1] ) { + swap(in, in+1); + isSwap=true; + } + } + + for(int in=0; in a[in+1] ) { + swap(in, in+1); + isSwap=true; + } + } + + } + + } + + private void swap(int one, int two) { + long temp = a[one]; + a[one] = a[two]; + a[two] = temp; + + } + +} + +class bubbleSort { + public static void main(String[] args) { + int maxSize = 100; + ArrayBub arr = new ArrayBub(maxSize); + + arr.insert(77); + arr.insert(99); + arr.insert(44); + arr.insert(55); + arr.insert(22); + arr.insert(88); + arr.insert(11); + arr.insert(00); + arr.insert(66); + arr.insert(33); + + arr.display(); + + // arr.oddEvenSort(); + + // arr.display(); + } +} diff --git a/Sorting algorithms/selectionSort.java b/Sorting algorithms/selectionSort.java new file mode 100644 index 0000000..0415d8d --- /dev/null +++ b/Sorting algorithms/selectionSort.java @@ -0,0 +1,63 @@ +class ArraySel { + private long[] a; + private int nElems; + + public ArraySel(int max) { + a = new long[max]; + nElems = 0; + } + + public void insert(long value) { + a[nElems] = value; + nElems++; + } + + public void display() { + for(int j=0; j