Skip to content

Instantly share code, notes, and snippets.

@tomrockdsouza
Last active June 2, 2017 06:56
Show Gist options
  • Save tomrockdsouza/742275013562fa9a8202c2da8930d4df to your computer and use it in GitHub Desktop.
Save tomrockdsouza/742275013562fa9a8202c2da8930d4df to your computer and use it in GitHub Desktop.
Java Implementation of Asynchronous Mulithreaded Odd-EvenTransposition Sort (Parallel Bubble Sort)
/**
* The class encapsulates an implementation of the Asynchornous Mulithreaded Odd-EvenTransposition
* @contributor Tomrock D'souza, St. Francis Institute Of Technology, University of Mumbai, 2017
* Part of This code is Taken from : www.roseindia.net/java/beginners/arrayexamples/OddEvenTranspositionSort.shtml
* No reproduction in whole or part without maintaining this notice
* and imposing this condition on any subsequent users.
*/
class EvenOddTrans extends Thread {
private int j;
public static final int array[] = {12,9,4,99,120,1,3,10,13};
public EvenOddTrans(int x) {
j = x;
}
public void run() {
if (array[j] > array[j + 1]) {
int T = array[j];
array[j] = array[j + 1];
array[j + 1] = T;
}
}
public static void main(String args[]) throws InterruptedException {
int i;
System.out.println("Values Before the sort:");
for (i = 0; i < EvenOddTrans.array.length; i++)
System.out.print(EvenOddTrans.array[i] + " ");
long start = System.nanoTime();
odd_even_srt(array.length);
long end = System.nanoTime();
System.out.println("\nValues after the sort:");
for (i = 0; i < EvenOddTrans.array.length; i++)
System.out.print(EvenOddTrans.array[i] + " ");
System.out.println("\nTook : " + (end - start) + " ns\n");
System.out.println("PAUSE");
}
public static void odd_even_srt(int n) {
int a = 0;
EvenOddTrans[] obj = new EvenOddTrans[40];
for (int f = 0; f < n / 2; f++) {
for (int g = 0; g + 1 < n; g += 2) {
obj[a] = new EvenOddTrans(g);
obj[a].start();
a++;
}
for (int g = 1; g + 1 < n; g += 2) {
obj[a] = new EvenOddTrans(g);
obj[a].start();
a++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment