Skip to content

Adding counting sort #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Sorting algorithms/CountingSort.java
Original file line number Diff line number Diff line change
@@ -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<n; ++i)
++count[arr[i]];

// Change count[i] so that count[i] now contains actual
// position of this character in output array
for (int i=1; i<=255; ++i)
count[i] += count[i-1];

// Build the output character array
// To make it stable we are operating in reverse order.
for (int i = n-1; 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<n; ++i)
arr[i] = output[i];
}

// Driver method
public static void main(String args[])
{
CountingSort ob = new CountingSort();
char arr[] = {'a','e','o','a','e','p','e','e','o','i','l'};

ob.sort(arr);

System.out.print("Sorted character array is ");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i]);
}
}
125 changes: 125 additions & 0 deletions Sorting algorithms/bubbleSort.java
Original file line number Diff line number Diff line change
@@ -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; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}

/*
In the bubbleSort program, the invariant is that the data items to the right of out are sorted. This remains true throughout the running of the algorithm. (On the first pass, nothing has been sorted yet, and there are no items to the right of out because it starts on the rightmost element.)
*/

public void bubbleSort() {
int out, in;
for(out=nElems-1; out>1; out--)
for(in=0; in<out; in++)
if( a[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<out; in++) {
if( a[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<nElems-1; in+=2) {
if( a[in] > a[in+1] ) {
swap(in, in+1);
isSwap=true;
}
}

for(int in=0; in<nElems-1; in+=2) {
if( a[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();
}
}
63 changes: 63 additions & 0 deletions Sorting algorithms/selectionSort.java
Original file line number Diff line number Diff line change
@@ -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<nElems; j++)
System.out.print(a[j] + " "); // display it
System.out.println("");
}

public void selectionSort() {
int out, in, min;
for(out=0; out<nElems-1; out++) {
min = out;
for(in=out+1; in<nElems; in++)
if(a[in] < a[min] )
min = in;
swap(out, min);
}
}

private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;

}

}

class selectionSort {
public static void main(String[] args) {
int maxSize = 100;
ArraySel arr = new ArraySel(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.selectionSort();

arr.display();
}
}