Skip to content
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

Quick sort with Callable implementation #21

Open
wants to merge 4 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
20 changes: 20 additions & 0 deletions os/code/os/src/main/java/com/scaler/quicksort/QuickRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.scaler.quicksort;

import java.util.concurrent.*;

public class QuickRunner {

public static void main(String[] args) throws ExecutionException, InterruptedException {
Integer[] arr = {10, 9, 8, 7, 1, 2, 3, 4};

ExecutorService executor = Executors.newCachedThreadPool();
QuickSorter sorter = new QuickSorter(arr, executor,0,arr.length-1);
Future<Integer[]> future = executor.submit(sorter);
arr = future.get();
executor.shutdown();

for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
}
}
48 changes: 48 additions & 0 deletions os/code/os/src/main/java/com/scaler/quicksort/QuickSorter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.scaler.quicksort;

import lombok.AllArgsConstructor;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;

@AllArgsConstructor
public class QuickSorter implements Callable<Integer[]> {

private Integer[] values;
private ExecutorService es;
private int start;
private int end;

private int partition (Integer[] a, int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);

for (int j = start; j <= end - 1; j++)
{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}


@Override
public Integer[] call() throws Exception {
if(start < end) {
int p = partition(values, start, end);
es.submit(new QuickSorter(values, es, start, p - 1));
es.submit(new QuickSorter(values, es, p + 1, end));
}
return values;
}
}
Binary file removed os/code/os/target/classes/com/scaler/App.class
Binary file not shown.
Binary file not shown.