Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created November 6, 2017 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Binary-Finery/3337acd2c75ee9ba16b2cbc094c8f119 to your computer and use it in GitHub Desktop.
Save Binary-Finery/3337acd2c75ee9ba16b2cbc094c8f119 to your computer and use it in GitHub Desktop.
sort factory
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int[] master_array, quick_array, bubble_array, insertion_array, selection_array, temp_merge_array, merge_array;
private TextView tvQuickSort, tvBubbleSort, tvInsertionSort, tvSelectionSort, tvMergeSort, tvMaxLength, tvMaxRandom, tvStatus;
private final String DOING = " : processing...";
private EditText etSize, etRandom;
private FloatingActionButton fab;
private int statusTracker = 0;
private int arrLength = 0, randQty = 0;
private boolean sizeOk = false, randomOk = false;
private int[] pbarIDs = {R.id.p1, R.id.p2, R.id.p3, R.id.p4, R.id.p5};
private ProgressBar[] progressBars = new ProgressBar[pbarIDs.length];
BarChart barChart;
private int barSelection, barInsertion, barQuick, barBubble, barMerge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvQuickSort = (TextView) findViewById(R.id.tv_output);
tvBubbleSort = (TextView) findViewById(R.id.tv_op_bubble);
tvInsertionSort = (TextView) findViewById(R.id.tv_op_insertion);
tvSelectionSort = (TextView) findViewById(R.id.tv_op_select);
tvMergeSort = (TextView) findViewById(R.id.tv_op_merge);
etRandom = (EditText) findViewById(R.id.et_max_random);
etSize = (EditText) findViewById(R.id.et_array_size);
fab = (FloatingActionButton) findViewById(R.id.fab);
barChart = (BarChart) findViewById(R.id.bar_chart);
final TextInputLayout arrLengthWrapper = (TextInputLayout) findViewById(R.id.sizeWrapper);
final TextInputLayout randomWrapper = (TextInputLayout) findViewById(R.id.randomWrapper);
arrLengthWrapper.setHint("Array length (2 to 50,000)");
randomWrapper.setHint("Max random (2 to 10,000");
for (int i = 0; i < progressBars.length; i++) {
progressBars[i] = (ProgressBar) findViewById(pbarIDs[i]);
progressBars[i].setVisibility(View.INVISIBLE);
}
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sizeOk = false;
randomOk = false;
String sa = etSize.getText().toString();
String sb = etRandom.getText().toString();
if (sa.length() > 0 && sb.length() > 0) {
int temp1 = Integer.parseInt(sa);
int temp2 = Integer.parseInt(sb);
if ((temp1 >= 2 && temp1 <= 50000) && (temp2 >= 2 && temp2 <= 10000)) {
sizeOk = true;
randomOk = true;
arrLength = temp1;
randQty = temp2;
}
}
if (randomOk && sizeOk) {
fab.setClickable(false);
statusTracker = 0;
master_array = null;
quick_array = null;
bubble_array = null;
insertion_array = null;
selection_array = null;
merge_array = null;
temp_merge_array = null;
Random r = new Random();
master_array = new int[arrLength];
for (int i = 0; i < master_array.length; i++)
master_array[i] = r.nextInt(randQty);
quick_array = new int[master_array.length];
bubble_array = new int[master_array.length];
insertion_array = new int[master_array.length];
selection_array = new int[master_array.length];
merge_array = new int[master_array.length];
temp_merge_array = new int[master_array.length];
System.arraycopy(master_array, 0, quick_array, 0, master_array.length);
System.arraycopy(master_array, 0, bubble_array, 0, master_array.length);
System.arraycopy(master_array, 0, insertion_array, 0, master_array.length);
System.arraycopy(master_array, 0, selection_array, 0, master_array.length);
System.arraycopy(master_array, 0, merge_array, 0, master_array.length);
tvBubbleSort.setText(DOING);
tvQuickSort.setText(DOING);
tvInsertionSort.setText(DOING);
tvSelectionSort.setText(DOING);
tvMergeSort.setText(DOING);
barBubble = 0;
barInsertion = 0;
barMerge = 0;
barSelection = 0;
barQuick = 0;
initiateBubbleThread(bubble_array);
initiateSortThread();
initiateInsertionThread(insertion_array);
initiateSelectionThread(selection_array);
initiateMergeThread(0, merge_array.length - 1);
for (int i = 0; i < progressBars.length; i++) {
progressBars[i].setVisibility(View.VISIBLE);
}
} else {
Toast.makeText(getApplicationContext(), "One or more values is not within the accepted range", Toast.LENGTH_LONG).show();
}
}
});
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
int pivot = quick_array[lowerIndex + (higherIndex - lowerIndex) / 2];
// Divide into two arrays
while (i <= j) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a number
* from right side which is less then the pivot value. Once the search
* is done, then we exchange both numbers.
*/
while (quick_array[i] < pivot) {
i++;
}
while (quick_array[j] > pivot) {
j--;
}
if (i <= j) {
int temp = quick_array[i];
quick_array[i] = quick_array[j];
quick_array[j] = temp;
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j) quickSort(lowerIndex, j);
if (i < higherIndex) quickSort(i, higherIndex);
}
private void bubbleSort(int[] x) {
boolean swapped = true;
int temp;
while (swapped) {
swapped = false;
for (int i = 1; i < x.length; i++) {
if (x[i - 1] > x[i]) {
temp = x[i - 1];
x[i - 1] = x[i];
x[i] = temp;
swapped = true;
}
}
}
}
private void insertionSort(int[] arr) {
int n = arr.length;
for (int j = 1; j < n; j++) {
int key = arr[j];
int i = j - 1;
while ((i > -1) && (arr[i] > key)) {
arr[i + 1] = arr[i];
i--;
}
arr[i + 1] = key;
}
}
private void mergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the merge_array
mergeSort(lowerIndex, middle);
// Below step sorts the right side of the merge_array
mergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
temp_merge_array[i] = merge_array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (temp_merge_array[i] <= temp_merge_array[j]) {
merge_array[k] = temp_merge_array[i];
i++;
} else {
merge_array[k] = temp_merge_array[j];
j++;
}
k++;
}
while (i <= middle) {
merge_array[k] = temp_merge_array[i];
k++;
i++;
}
}
private void selectionSort(int[] ssa) {
for (int i = 0; i < ssa.length - 1; i++) {
int index = i;
for (int j = i + 1; j < ssa.length; j++) {
if (ssa[j] < ssa[index]) {
index = j;//searching for lowest index
}
}
int smallerNumber = ssa[index];
ssa[index] = ssa[i];
ssa[i] = smallerNumber;
}
}
private void initiateBubbleThread(final int[] bsar) {
Thread bubbleThread = new Thread(new Runnable() {
@Override
public void run() {
long pre = System.currentTimeMillis();
bubbleSort(bsar);
long post = System.currentTimeMillis();
final long diff = post - pre;
runOnUiThread(new Runnable() {
public void run() {
statusTracker++;
updateStatus(3);
tvBubbleSort.setText(" : " + String.valueOf(diff) + " ms");
barBubble = (int) diff;
}
});
}
});
bubbleThread.start();
}
private void initiateSortThread() {
Thread sortThread = new Thread(new Runnable() {
@Override
public void run() {
long pre = System.currentTimeMillis();
quickSort(0, quick_array.length - 1);
long post = System.currentTimeMillis();
final long diff = post - pre;
runOnUiThread(new Runnable() {
public void run() {
statusTracker++;
updateStatus(4);
tvQuickSort.setText(" : " + String.valueOf(diff) + " ms");
barQuick = (int) diff;
}
});
}
});
sortThread.start();
}
private void initiateInsertionThread(final int[] isar) {
Thread insertionThread = new Thread(new Runnable() {
@Override
public void run() {
long pre = System.currentTimeMillis();
insertionSort(isar);
long post = System.currentTimeMillis();
final long diff = post - pre;
runOnUiThread(new Runnable() {
public void run() {
statusTracker++;
updateStatus(0);
tvInsertionSort.setText(" : " + String.valueOf(diff) + " ms");
barInsertion = (int) diff;
}
});
}
});
insertionThread.start();
}
private void initiateSelectionThread(final int[] ssar) {
Thread selectionThread = new Thread(new Runnable() {
@Override
public void run() {
long pre = System.currentTimeMillis();
selectionSort(ssar);
long post = System.currentTimeMillis();
final long diff = post - pre;
runOnUiThread(new Runnable() {
public void run() {
statusTracker++;
updateStatus(2);
tvSelectionSort.setText(" : " + String.valueOf(diff) + " ms");
barSelection = (int) diff;
}
});
}
});
selectionThread.start();
}
private void initiateMergeThread(final int s, final int e) {
Thread mergeThread = new Thread(new Runnable() {
@Override
public void run() {
long pre = System.currentTimeMillis();
mergeSort(s, e);
long post = System.currentTimeMillis();
final long diff = post - pre;
runOnUiThread(new Runnable() {
public void run() {
statusTracker++;
updateStatus(1);
tvMergeSort.setText(" : " + String.valueOf(diff));
barMerge = (int) diff;
}
});
}
});
mergeThread.start();
}
private void updateStatus(int pid) {
progressBars[pid].setVisibility(View.GONE);
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
fab.setClickable(true);
initBarChart();
}
}, 250);
}
private void initBarChart() {
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry(barInsertion, 0));
barEntries.add(new BarEntry(barMerge, 1));
barEntries.add(new BarEntry(barSelection, 2));
barEntries.add(new BarEntry(barBubble, 3));
barEntries.add(new BarEntry(barQuick, 4));
BarDataSet dataSet = new BarDataSet(barEntries, "Algorithms");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
List<String> labels = new ArrayList<>();
labels.add("Insertion");
labels.add("Merge");
labels.add("Selection");
labels.add("Bubble");
labels.add("Quick");
BarData barData = new BarData(labels, dataSet);
barChart.getXAxis().setSpaceBetweenLabels(0);
barChart.setDescription("");
barChart.setData(barData);
XAxis bottomAxis = barChart.getXAxis();
bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
YAxis rightYAxis = barChart.getAxisRight();
rightYAxis.setEnabled(false);
YAxis leftYAxis = barChart.getAxisLeft();
leftYAxis.setEnabled(false);
barChart.getXAxis().setDrawGridLines(false);
barChart.setDrawGridBackground(false);
barChart.invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment