31 | * auto set theme when user changed theme 32 | *
33 | * Created by Duy on 19/7/2016 34 | */ 35 | public abstract class AbstractAppCompatActivity extends AppCompatActivity 36 | implements SharedPreferences.OnSharedPreferenceChangeListener { 37 | public static final String TAG = "MainActivity"; 38 | protected SharedPreferences mPreferences; 39 | public static final String APP_ID = BuildConfig.APPLICATION_ID; 40 | 41 | 42 | /** 43 | * set theme and init mHistoryDatabase for history 44 | * 45 | * @param savedInstanceState 46 | */ 47 | @Override 48 | protected void onCreate(@Nullable Bundle savedInstanceState) { 49 | super.onCreate(savedInstanceState); 50 | 51 | // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 52 | // WindowManager.LayoutParams.FLAG_FULLSCREEN); 53 | // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 54 | // Window w = getWindow(); // in Activity's onCreate() for instance 55 | // w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 56 | // WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 57 | // } 58 | mPreferences = PreferenceManager.getDefaultSharedPreferences(this); 59 | 60 | setLocale(false); 61 | 62 | //set theme for app 63 | setTheme(false); 64 | } 65 | 66 | /** 67 | * set language 68 | * 69 | * @param create 70 | */ 71 | 72 | private void setLocale(boolean create) { 73 | Locale locale; 74 | String code = mPreferences.getString(getString(R.string.key_pref_lang), "default_lang"); 75 | if (code.equals("default_lang")) { 76 | Log.d(TAG, "setLocale: default"); 77 | locale = Locale.getDefault(); 78 | } else { 79 | locale = new Locale(code); 80 | } 81 | Locale.setDefault(locale); 82 | Configuration config = new Configuration(); 83 | config.locale = locale; 84 | Resources resources = getResources(); 85 | resources.updateConfiguration(config, resources.getDisplayMetrics()); 86 | if (create) recreate(); 87 | } 88 | 89 | 90 | @Override 91 | public void onBackPressed() { 92 | super.onBackPressed(); 93 | } 94 | 95 | @Override 96 | protected void onStart() { 97 | super.onStart(); 98 | if (mPreferences != null) 99 | mPreferences.registerOnSharedPreferenceChangeListener(this); 100 | // Monitor launch times and interval from installation 101 | RateThisApp.onStart(this); 102 | // If the criteria is satisfied, "Rate this app" dialog will be shown 103 | RateThisApp.showRateDialogIfNeeded(this); 104 | RateThisApp.setCallback(new RateThisApp.Callback() { 105 | @Override 106 | public void onYesClicked() { 107 | rateApp(null); 108 | } 109 | 110 | @Override 111 | public void onNoClicked() { 112 | } 113 | 114 | @Override 115 | public void onCancelClicked() { 116 | } 117 | }); 118 | 119 | } 120 | 121 | @Override 122 | protected void onResume() { 123 | super.onResume(); 124 | } 125 | 126 | /** 127 | * set theme for app 128 | * 129 | * @param recreate -call method onCreate 130 | */ 131 | protected void setTheme(boolean recreate) { 132 | String name = mPreferences.getString(getResources().getString(R.string.key_pref_theme), ""); 133 | ThemeEngine themeEngine = new ThemeEngine(getApplicationContext()); 134 | int themeId = themeEngine.getTheme(name); 135 | if (themeId != ThemeEngine.THEME_NOT_FOUND) { 136 | super.setTheme(themeId); 137 | if (recreate) recreate(); 138 | Log.d(TAG, "Set theme ok"); 139 | } else { 140 | Log.d(TAG, "Theme not found"); 141 | } 142 | } 143 | 144 | @Override 145 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { 146 | if (s.equals(getResources().getString(R.string.key_pref_theme))) { 147 | setTheme(true); 148 | } else if (s.equals(getString(R.string.key_pref_lang))) { 149 | setLocale(true); 150 | Toast.makeText(this, getString(R.string.change_lang_msg), Toast.LENGTH_SHORT).show(); 151 | } 152 | } 153 | 154 | @Override 155 | protected void onPause() { 156 | super.onPause(); 157 | } 158 | 159 | @Override 160 | protected void onDestroy() { 161 | super.onDestroy(); 162 | if (mPreferences != null) 163 | mPreferences.unregisterOnSharedPreferenceChangeListener(this); 164 | } 165 | 166 | 167 | /** 168 | * share app 169 | */ 170 | public void shareApp(View view) { 171 | Intent intent = new Intent(); 172 | intent.setAction(Intent.ACTION_SEND); 173 | intent.putExtra(Intent.EXTRA_TEXT, "http://play.google.com/store/apps/details?id=" + APP_ID); 174 | intent.setType("text/plain"); 175 | startActivity(intent); 176 | } 177 | 178 | /** 179 | * show dialog with title and messenger 180 | * 181 | * @param title - title 182 | * @param msg - messenger 183 | */ 184 | protected void showDialog(String title, String msg) { 185 | AlertDialog.Builder builder = new AlertDialog.Builder(this); 186 | builder.setTitle(title).setMessage(msg); 187 | builder.setNegativeButton(this.getString(R.string.close), new DialogInterface.OnClickListener() { 188 | @Override 189 | public void onClick(DialogInterface dialogInterface, int i) { 190 | dialogInterface.cancel(); 191 | } 192 | }); 193 | builder.create().show(); 194 | } 195 | 196 | /** 197 | * show dialog with title and messenger 198 | * 199 | * @param msg - messenger 200 | */ 201 | protected void showDialog(String msg) { 202 | this.showDialog("", msg); 203 | } 204 | 205 | 206 | 207 | public void rateApp(View view) { 208 | Uri uri = Uri.parse("market://details?id=" + APP_ID); 209 | Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); 210 | // To count with Play market backstack, After pressing back button, 211 | // to taken back to our application, we need to add following flags to intent. 212 | goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | 213 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 214 | try { 215 | startActivity(goToMarket); 216 | } catch (ActivityNotFoundException e) { 217 | startActivity(new Intent(Intent.ACTION_VIEW, 218 | Uri.parse("http://play.google.com/store/apps/details?id=" + APP_ID))); 219 | } 220 | } 221 | 222 | public void moreApp(View view) { 223 | Uri uri = Uri.parse("market://search?q=pub:Trần Lê Duy"); 224 | Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); 225 | // To count with Play market backstack, After pressing back button, 226 | // to taken back to our application, we need to add following flags to intent. 227 | goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | 228 | 229 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 230 | try { 231 | startActivity(goToMarket); 232 | } catch (ActivityNotFoundException e) { 233 | startActivity(new Intent(Intent.ACTION_VIEW, 234 | Uri.parse("http://play.google.com/store/search?q=pub:Trần Lê Duy"))); 235 | } 236 | } 237 | 238 | protected void hideKeyboard(EditText editText) { 239 | InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 240 | imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 241 | } 242 | 243 | 244 | } 245 | -------------------------------------------------------------------------------- /app/src/main/java/com/duy/algorithm/AbstractSortAlgorithmFragment.java: -------------------------------------------------------------------------------- 1 | package com.duy.algorithm; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.design.widget.FloatingActionButton; 6 | import android.support.v4.app.Fragment; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.Button; 12 | import android.widget.EditText; 13 | import android.widget.SeekBar; 14 | import android.widget.TextView; 15 | 16 | import com.github.jorgecastilloprz.FABProgressCircle; 17 | import com.sothree.slidinguppanel.SlidingUpPanelLayout; 18 | 19 | import static com.duy.algorithm.algorithms.AlgorithmThread.KEY_ALGORITHM; 20 | 21 | /** 22 | * Created by DUy on 04-Feb-17. 23 | */ 24 | 25 | public abstract class AbstractSortAlgorithmFragment extends Fragment implements SortCompletionListener { 26 | final String TAG = AbstractSortAlgorithmFragment.class.getSimpleName(); 27 | protected View mRootView; 28 | protected FloatingActionButton mPlay; 29 | protected FABProgressCircle mPlayAnimation; 30 | protected TextView txtInfo; 31 | protected EditText mEditNumber; 32 | protected SeekBar mTimeBar; 33 | protected TextView txtProgress; 34 | protected Button btnRandom; 35 | protected EditText mEditArray; 36 | protected SlidingUpPanelLayout mSlidingUpPanelLayout; 37 | 38 | @Nullable 39 | @Override 40 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 41 | mRootView = getView(inflater, container); 42 | return mRootView; 43 | } 44 | 45 | @Override 46 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 47 | super.onActivityCreated(savedInstanceState); 48 | 49 | createSortView(); 50 | 51 | mPlay = (FloatingActionButton) findViewById(R.id.fab); 52 | mPlayAnimation = (FABProgressCircle) findViewById(R.id.fabProgressCircle); 53 | txtProgress = (TextView) findViewById(R.id.txt_process); 54 | mTimeBar = (SeekBar) findViewById(R.id.seek_bar_time); 55 | 56 | /** 57 | * length of array and array 58 | */ 59 | mEditNumber = (EditText) findViewById(R.id.edit_length); 60 | mEditArray = (EditText) findViewById(R.id.edit_entry); 61 | 62 | btnRandom = (Button) findViewById(R.id.ckb_random); 63 | 64 | mSlidingUpPanelLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout); 65 | 66 | //set up fragment 67 | setupFragment(getArguments().getString(KEY_ALGORITHM)); 68 | 69 | txtProgress.setText(" " + (mTimeBar.getProgress() + 10)); 70 | //set time delay for thread 71 | mTimeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 72 | @Override 73 | public void onProgressChanged(SeekBar seekBar, int value, boolean fromUser) { 74 | txtProgress.setText(" " + (value + 10)); 75 | setDelayTime(value); 76 | } 77 | 78 | @Override 79 | public void onStartTrackingTouch(SeekBar seekBar) { 80 | 81 | } 82 | 83 | @Override 84 | public void onStopTrackingTouch(SeekBar seekBar) { 85 | 86 | } 87 | }); 88 | btnRandom.setOnClickListener(new View.OnClickListener() { 89 | @Override 90 | public void onClick(View v) { 91 | generateRandomData(); 92 | // mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); 93 | } 94 | }); 95 | 96 | mPlay.setImageResource(R.drawable.ic_play_arrow_white_24dp); 97 | mPlay.setOnClickListener(new View.OnClickListener() { 98 | @Override 99 | public void onClick(View v) { 100 | fabClick(); 101 | } 102 | }); 103 | 104 | } 105 | 106 | protected abstract void fabClick(); 107 | 108 | protected abstract void createSortView(); 109 | 110 | /** 111 | * start thread sort 112 | */ 113 | protected abstract void startSort(); 114 | 115 | /** 116 | * pause all thread sort 117 | */ 118 | protected abstract void pauseSort(); 119 | 120 | /** 121 | * resume all thread 122 | */ 123 | protected abstract void resumeSort(); 124 | 125 | protected abstract void setDelayTime(int value); 126 | 127 | /** 128 | * create random data 129 | */ 130 | protected abstract void generateRandomData(); 131 | 132 | public abstract void setupFragment(String name); 133 | 134 | /** 135 | * find view by id and return view, if view is null, find view in parent activity 136 | * 137 | * @param viewId - id of view 138 | * @return view 139 | */ 140 | public View findViewById(int viewId) { 141 | View view; 142 | view = mRootView.findViewById(viewId); 143 | if (view != null) return view; 144 | return getActivity().findViewById(viewId); 145 | } 146 | 147 | /** 148 | * get container view and store it to mRootView 149 | * 150 | * @return - container view 151 | */ 152 | public abstract View getView(LayoutInflater inflater, ViewGroup v); 153 | 154 | /** 155 | * check input empty and create data if need 156 | * 157 | * @return - true if success, otherwise false 158 | */ 159 | public abstract boolean createData(); 160 | 161 | /** 162 | * enable some view of needed 163 | */ 164 | public void enableView() { 165 | Log.d(TAG, "enableView: "); 166 | if (getActivity() != null) { 167 | getActivity().runOnUiThread(new Runnable() { 168 | @Override 169 | public void run() { 170 | mEditArray.setEnabled(true); 171 | mEditNumber.setEnabled(true); 172 | btnRandom.setEnabled(true); 173 | } 174 | }); 175 | } 176 | } 177 | 178 | /** 179 | * disable some view if needed 180 | */ 181 | public void disableView() { 182 | Log.d(TAG, "disableView: "); 183 | if (getActivity() != null) { 184 | getActivity().runOnUiThread(new Runnable() { 185 | @Override 186 | public void run() { 187 | mEditArray.setEnabled(false); 188 | mEditNumber.setEnabled(false); 189 | btnRandom.setEnabled(false); 190 | } 191 | }); 192 | } 193 | } 194 | 195 | @Override 196 | public void onResume() { 197 | super.onResume(); 198 | // resumeSort(); 199 | } 200 | 201 | @Override 202 | public void onPause() { 203 | super.onPause(); 204 | Log.d(TAG, "onPause: "); 205 | try { 206 | pauseSort(); enableView(); 207 | 208 | } catch (Exception e) { 209 | e.printStackTrace(); 210 | } 211 | } 212 | 213 | @Override 214 | public void onDestroy() { 215 | super.onDestroy(); 216 | 217 | } 218 | 219 | @Override 220 | public void onSortCompleted() { 221 | 222 | } 223 | 224 | } 225 | -------------------------------------------------------------------------------- /app/src/main/java/com/duy/algorithm/AppAboutActivity.java: -------------------------------------------------------------------------------- 1 | package com.duy.algorithm; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.MenuItem; 9 | 10 | public class AppAboutActivity extends AbstractAppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_app_about); 16 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 17 | setSupportActionBar(toolbar); 18 | getSupportActionBar().setHomeButtonEnabled(true); 19 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 20 | 21 | } 22 | 23 | public Intent getOpenFacebookIntent(Context context) { 24 | try { 25 | context.getPackageManager().getPackageInfo("com.facebook.katana", 0); 26 | return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/883398988363238")); 27 | } catch (Exception e) { 28 | return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/com.duy.calculator.n.plus/")); 29 | } 30 | } 31 | 32 | @Override 33 | public boolean onOptionsItemSelected(MenuItem item) { 34 | int id = item.getItemId(); 35 | if (id == android.R.id.home) { 36 | finish(); 37 | } 38 | return super.onOptionsItemSelected(item); 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/duy/algorithm/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.duy.algorithm; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.os.Handler; 6 | import android.support.annotation.NonNull; 7 | import android.support.annotation.Nullable; 8 | import android.support.design.widget.NavigationView; 9 | import android.support.v4.app.Fragment; 10 | import android.support.v4.app.FragmentManager; 11 | import android.support.v4.app.FragmentTransaction; 12 | import android.support.v4.view.GravityCompat; 13 | import android.support.v4.widget.DrawerLayout; 14 | import android.support.v7.app.ActionBarDrawerToggle; 15 | import android.support.v7.widget.Toolbar; 16 | import android.view.MenuItem; 17 | import android.view.View; 18 | 19 | import com.sothree.slidinguppanel.SlidingUpPanelLayout; 20 | 21 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.BUBBLE_SORT; 22 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.COCKTAIL_SORT; 23 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.INSERTION_SORT; 24 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.MERGE_SORT; 25 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.QUICK_SORT; 26 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.SELECTION_SORT; 27 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.SHELL_SORT; 28 | 29 | /** 30 | * Created by DUy on 02-Feb-17. 31 | */ 32 | 33 | public class MainActivity extends AbstractAppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 34 | private DrawerLayout drawerLayout; 35 | private NavigationView navigationView; 36 | private SlidingUpPanelLayout slidingUpPanelLayout; 37 | private Handler handler = new Handler(); 38 | 39 | @Override 40 | protected void onCreate(@Nullable Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_main); 43 | 44 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 45 | setSupportActionBar(toolbar); 46 | 47 | navigationView = (NavigationView) findViewById(R.id.nav_view); 48 | navigationView.setNavigationItemSelectedListener(this); 49 | 50 | drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 51 | 52 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 53 | drawerLayout.setDrawerListener(toggle); 54 | toggle.syncState(); 55 | 56 | slidingUpPanelLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout); 57 | 58 | // logAdapter = new LogAdapter(this); 59 | // mLogger = (RecyclerView) findViewById(R.id.rc_logger); 60 | // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 61 | // linearLayoutManager.setStackFromEnd(true); 62 | // mLogger.setHasFixedSize(true); 63 | // mLogger.setAdapter(logAdapter); 64 | } 65 | 66 | public int getStatusBarHeight() { 67 | int result = 0; 68 | int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 69 | if (resourceId > 0) { 70 | result = getResources().getDimensionPixelSize(resourceId); 71 | } 72 | return result; 73 | } 74 | 75 | @Override 76 | protected void onResume() { 77 | super.onResume(); 78 | } 79 | 80 | @Override 81 | public void onBackPressed() { 82 | if (drawerLayout.isDrawerOpen(GravityCompat.START)) { 83 | drawerLayout.closeDrawers(); 84 | return; 85 | } 86 | if (slidingUpPanelLayout.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) { 87 | slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); 88 | return; 89 | } 90 | super.onBackPressed(); 91 | } 92 | 93 | @Override 94 | public boolean onNavigationItemSelected(@NonNull MenuItem item) { 95 | SortAlgorithmFragment algorithmFragment = null; 96 | SortSpeedTestFragmentSort sortSpeedTestFragment = null; 97 | setTitle(item.getTitle().toString()); 98 | int id = item.getItemId(); 99 | switch (id) { 100 | case R.id.nav_bubble_sort: 101 | algorithmFragment = SortAlgorithmFragment.newInstance(BUBBLE_SORT); 102 | commit(algorithmFragment); 103 | break; 104 | case R.id.nav_insert_sort: 105 | algorithmFragment = SortAlgorithmFragment.newInstance(INSERTION_SORT); 106 | commit(algorithmFragment); 107 | break; 108 | case R.id.nav_merge_sort: 109 | algorithmFragment = SortAlgorithmFragment.newInstance(MERGE_SORT); 110 | commit(algorithmFragment); 111 | break; 112 | case R.id.nav_quick_sort: 113 | algorithmFragment = SortAlgorithmFragment.newInstance(QUICK_SORT); 114 | commit(algorithmFragment); 115 | break; 116 | case R.id.nav_select_sort: 117 | algorithmFragment = SortAlgorithmFragment.newInstance(SELECTION_SORT); 118 | commit(algorithmFragment); 119 | break; 120 | case R.id.nav_shell_sort: 121 | algorithmFragment = SortAlgorithmFragment.newInstance(SHELL_SORT); 122 | commit(algorithmFragment); 123 | break; 124 | case R.id.nav_sort_test: 125 | sortSpeedTestFragment = SortSpeedTestFragmentSort.newInstance(SHELL_SORT); 126 | commit(sortSpeedTestFragment); 127 | break; 128 | case R.id.nav_setting: 129 | startActivity(new Intent(getApplicationContext(), SettingsActivity.class)); 130 | break; 131 | case R.id.nav_info: 132 | startActivity(new Intent(getApplicationContext(), AppAboutActivity.class)); 133 | break; 134 | case R.id.nav_cocktail_sort: 135 | algorithmFragment = SortAlgorithmFragment.newInstance(COCKTAIL_SORT); 136 | commit(algorithmFragment); 137 | break; 138 | } 139 | drawerLayout.closeDrawers(); 140 | return true; 141 | } 142 | 143 | @Override 144 | protected void onStop() { 145 | super.onStop(); 146 | } 147 | 148 | private void commit(Fragment algorithmFragment) { 149 | FragmentManager fragmentManager = getSupportFragmentManager(); 150 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 151 | fragmentTransaction.replace(R.id.container, algorithmFragment); 152 | fragmentTransaction.commit(); 153 | } 154 | 155 | public void openDrawer(View view) { 156 | drawerLayout.openDrawer(GravityCompat.START); 157 | // if (view != null) view.setVisibility(View.GONE); 158 | } 159 | 160 | 161 | public void showLog(String log) { 162 | // logAdapter.showLog(log); 163 | // mLogger.scrollToPosition(logAdapter.getItemCount() - 1); 164 | } 165 | 166 | public void clearLog() { 167 | // logAdapter.clear(); 168 | } 169 | 170 | public void showLog(String message, int[] array) { 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /app/src/main/java/com/duy/algorithm/SettingsActivity.java: -------------------------------------------------------------------------------- 1 | package com.duy.algorithm; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.preference.PreferenceActivity; 6 | import android.preference.PreferenceFragment; 7 | 8 | /** 9 | * Setting for calcualtor 10 | *
11 | * Include precision of calculate. Font, theme, style. Dev mode, trace mode.
12 | */
13 | public class SettingsActivity extends PreferenceActivity {
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setTitle(R.string.setting);
18 | getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
19 | }
20 |
21 | @Override
22 | protected void onResume() {
23 | super.onResume();
24 | }
25 |
26 | public static class MyPreferenceFragment extends PreferenceFragment {
27 | @Override
28 | public void onCreate(final Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | addPreferencesFromResource(R.xml.setting);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/SortAlgorithmFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm;
16 |
17 | import android.os.Bundle;
18 | import android.util.Log;
19 | import android.view.LayoutInflater;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 | import android.widget.Toast;
23 |
24 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
25 | import com.duy.algorithm.algorithms.algo.BubbleSortThread;
26 | import com.duy.algorithm.algorithms.algo.CocktailShakerSortThread;
27 | import com.duy.algorithm.algorithms.algo.InsertionSortThread;
28 | import com.duy.algorithm.algorithms.algo.MergeSortThread;
29 | import com.duy.algorithm.algorithms.algo.QuickSortThread;
30 | import com.duy.algorithm.algorithms.algo.SelectionSortThread;
31 | import com.duy.algorithm.algorithms.algo.ShellSortThread;
32 | import com.duy.algorithm.customview.LogView;
33 | import com.duy.algorithm.customview.SortView;
34 | import com.duy.algorithm.utils.ArrayUtils;
35 |
36 | import java.util.regex.Pattern;
37 |
38 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.BUBBLE_SORT;
39 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.COCKTAIL_SORT;
40 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.INSERTION_SORT;
41 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.MERGE_SORT;
42 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.QUICK_SORT;
43 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.SELECTION_SORT;
44 | import static com.duy.algorithm.algorithms.AlgorithmThread.ALGORITHM_NAME.SHELL_SORT;
45 | import static com.duy.algorithm.algorithms.AlgorithmThread.COMMAND_START_ALGORITHM;
46 | import static com.duy.algorithm.algorithms.AlgorithmThread.KEY_ALGORITHM;
47 |
48 | public class SortAlgorithmFragment extends AbstractSortAlgorithmFragment {
49 |
50 | private final static String TAG = SortAlgorithmFragment.class.getSimpleName();
51 | protected SortAlgorithmThread thread;
52 | private SortView mSortView;
53 | private int mCountData = 50;
54 | private LogView mLogger;
55 |
56 | public static SortAlgorithmFragment newInstance(String algorithm) {
57 | SortAlgorithmFragment fragment = new SortAlgorithmFragment();
58 | Bundle bundle = new Bundle();
59 | bundle.putString(KEY_ALGORITHM, algorithm);
60 | fragment.setArguments(bundle);
61 | return fragment;
62 | }
63 |
64 | @Override
65 | public View getView(LayoutInflater inflater, ViewGroup container) {
66 | return inflater.inflate(R.layout.fragment_sort, container, false);
67 | }
68 |
69 | @Override
70 | protected void createSortView() {
71 | mSortView = (SortView) findViewById(R.id.sortView);
72 | mLogger = (LogView) findViewById(R.id.rc_log);
73 | mLogger.setmEmptyView(findViewById(R.id.empty_view));
74 | }
75 |
76 | @Override
77 | public void setupFragment(String algorithmKey) {
78 | switch (algorithmKey) {
79 | case BUBBLE_SORT:
80 | thread = new BubbleSortThread(mSortView, getActivity());
81 | break;
82 | case INSERTION_SORT:
83 | thread = new InsertionSortThread(mSortView, getActivity());
84 | break;
85 | case SELECTION_SORT:
86 | thread = new SelectionSortThread(mSortView, getActivity());
87 | break;
88 | case QUICK_SORT:
89 | thread = new QuickSortThread(mSortView, getActivity());
90 | break;
91 | case MERGE_SORT:
92 | thread = new MergeSortThread(mSortView, getActivity());
93 | break;
94 | case SHELL_SORT:
95 | thread = new ShellSortThread(mSortView, getActivity());
96 | break;
97 | case COCKTAIL_SORT:
98 | thread = new CocktailShakerSortThread(mSortView, getActivity());
99 | break;
100 | }
101 | thread.setStarted(false);
102 | thread.setCompletionListener(this);
103 | thread.setLogger(mLogger);
104 | generateRandomData();
105 | }
106 |
107 | @Override
108 | public void enableView() {
109 | mEditArray.setEnabled(true);
110 | mEditNumber.setEnabled(true);
111 | btnRandom.setEnabled(true);
112 | }
113 |
114 |
115 | @Override
116 | protected void setDelayTime(int value) {
117 | if (thread.isStarted())
118 | thread.setDelayTime(value + 10);
119 | }
120 |
121 | @Override
122 | protected void startSort() {
123 | if (createData()) {
124 | thread.sendMessage(COMMAND_START_ALGORITHM);
125 | mPlay.setImageResource(R.drawable.ic_pause_white_24dp);
126 | mPlayAnimation.show();
127 | disableView();
128 | }
129 | }
130 |
131 | @Override
132 | protected void fabClick() {
133 | if (!thread.isStarted()) {//If you have sorted or not sorted
134 | startSort();
135 | } else { //if it is sorting, pause or resume thread
136 | if (thread.isPaused())
137 | resumeSort();
138 | else
139 | pauseSort();
140 | }
141 | }
142 |
143 |
144 | @Override
145 | public void generateRandomData() {
146 | //check empty length of array
147 | if (mEditNumber.getText().toString().isEmpty()) {
148 | mCountData = Integer.parseInt(mEditNumber.getHint().toString());
149 | } else {
150 | mCountData = Integer.parseInt(mEditNumber.getText().toString());
151 | }
152 |
153 | int[] array = ArrayUtils.createIntArray(mCountData);
154 | thread.setData(array);
155 | mEditArray.setText(ArrayUtils.arrayToString(array));
156 | }
157 |
158 | @Override
159 | public boolean createData() {
160 | Log.d(TAG, "createData: ");
161 |
162 | //set time sleep for thread
163 | int delayTime = mTimeBar.getProgress();
164 | thread.setDelayTime(delayTime);
165 |
166 | //check empty length of array
167 | if (mEditNumber.getText().toString().isEmpty()) {
168 | mCountData = Integer.parseInt(mEditNumber.getHint().toString());
169 | } else {
170 | mCountData = Integer.parseInt(mEditNumber.getText().toString());
171 | }
172 |
173 | String raw = mEditArray.getText().toString();
174 | final String strArray[] = raw.split(Pattern.quote(","));
175 | if (strArray.length == mCountData) {
176 | int[] array = ArrayUtils.arrayStringToInt(strArray);
177 |
178 | //If receive an error during conversion
179 | if (array[0] == -1) {
180 | return false;
181 | }
182 | thread.setData(array);
183 | return true;
184 | } else {
185 | // mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
186 | if (strArray.length > mCountData) {
187 | Toast.makeText(getActivity(), "Please remove " +
188 | +(strArray.length - mCountData)
189 | + " entry of array, length of array is "
190 | + strArray.length, Toast.LENGTH_LONG).show();
191 | } else {
192 | Toast.makeText(getActivity(), "Please add "
193 | + (-strArray.length + mCountData)
194 | + " entry of array, length of array is "
195 | + strArray.length, Toast.LENGTH_LONG).show();
196 | }
197 |
198 | return false;
199 | }
200 | }
201 |
202 | @Override
203 | public void pauseSort() {
204 | if (thread.isStarted()) {
205 | thread.setPaused(true);
206 | mPlay.setImageResource(R.drawable.ic_play_arrow_white_24dp);
207 | mPlayAnimation.hide();
208 | disableView();
209 | }
210 | }
211 |
212 | @Override
213 | protected void resumeSort() {
214 | if (thread.isPaused()) {
215 | thread.setPaused(false);
216 | mPlay.setImageResource(R.drawable.ic_pause_white_24dp);
217 | mPlayAnimation.show();
218 | disableView();
219 | }
220 | }
221 |
222 | @Override
223 | public void onSortCompleted() {
224 | if (getActivity() != null) {
225 | getActivity().runOnUiThread(new Runnable() {
226 | @Override
227 | public void run() {
228 | Toast.makeText(getActivity().getApplicationContext(), R.string.sorted, Toast.LENGTH_SHORT).show();
229 | enableView();
230 | mPlayAnimation.hide();
231 | mPlay.setImageResource(R.drawable.ic_settings_backup_restore_white_24dp);
232 | }
233 | });
234 | }
235 | super.onSortCompleted();
236 | }
237 | }
238 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/SortCompletionListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm;
16 |
17 | public interface SortCompletionListener {
18 |
19 | void onSortCompleted();
20 | }
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/SortSpeedTestFragmentSort.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm;
16 |
17 | import android.os.Bundle;
18 | import android.util.Log;
19 | import android.view.LayoutInflater;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 | import android.widget.Toast;
23 |
24 | import com.duy.algorithm.algorithms.AlgorithmThread;
25 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
26 | import com.duy.algorithm.algorithms.algo.BubbleSortThread;
27 | import com.duy.algorithm.algorithms.algo.CocktailShakerSortThread;
28 | import com.duy.algorithm.algorithms.algo.InsertionSortThread;
29 | import com.duy.algorithm.algorithms.algo.MergeSortThread;
30 | import com.duy.algorithm.algorithms.algo.QuickSortThread;
31 | import com.duy.algorithm.algorithms.algo.SelectionSortThread;
32 | import com.duy.algorithm.algorithms.algo.ShellSortThread;
33 | import com.duy.algorithm.customview.SortView;
34 | import com.duy.algorithm.utils.ArrayUtils;
35 | import com.sothree.slidinguppanel.SlidingUpPanelLayout;
36 |
37 | import java.util.regex.Pattern;
38 |
39 | import static com.duy.algorithm.algorithms.AlgorithmThread.KEY_ALGORITHM;
40 |
41 | public class SortSpeedTestFragmentSort extends AbstractSortAlgorithmFragment {
42 | private final static String TAG = SortSpeedTestFragmentSort.class.getSimpleName();
43 | private final int mCount = 7;
44 | private SortAlgorithmThread[] threads = new SortAlgorithmThread[mCount];
45 | private SortView[] mSortViews = new SortView[mCount];
46 | private int mCountData = 50;
47 | private int numThreadCompleted = 0;
48 |
49 | public static SortSpeedTestFragmentSort newInstance(String algorithm) {
50 | SortSpeedTestFragmentSort fragment = new SortSpeedTestFragmentSort();
51 | Bundle bundle = new Bundle();
52 | bundle.putString(KEY_ALGORITHM, algorithm);
53 | fragment.setArguments(bundle);
54 | return fragment;
55 | }
56 |
57 | @Override
58 | public View getView(LayoutInflater inflater, ViewGroup v) {
59 | return inflater.inflate(R.layout.fragment_multi_sort, v, false);
60 | }
61 |
62 | @Override
63 | protected void createSortView() {
64 | mSortViews[0] = (SortView) findViewById(R.id.sortView0);
65 | mSortViews[0].setName(R.string.bubble_sort);
66 |
67 | mSortViews[1] = (SortView) findViewById(R.id.sortView1);
68 | mSortViews[1].setName(R.string.insert_sort);
69 |
70 | mSortViews[2] = (SortView) findViewById(R.id.sortView2);
71 | mSortViews[2].setName(R.string.selection_sort);
72 |
73 | mSortViews[3] = (SortView) findViewById(R.id.sortView3);
74 | mSortViews[3].setName(R.string.quick_sort);
75 |
76 | mSortViews[4] = (SortView) findViewById(R.id.sortView4);
77 | mSortViews[4].setName(R.string.merge_sort);
78 |
79 | mSortViews[5] = (SortView) findViewById(R.id.sortView5);
80 | mSortViews[5].setName(R.string.shell_sort);
81 |
82 | mSortViews[6] = (SortView) findViewById(R.id.sortView6);
83 | mSortViews[6].setName(R.string.cocktail_sort);
84 | }
85 |
86 | @Override
87 | protected void setDelayTime(int value) {
88 | for (AlgorithmThread thread : threads) {
89 | thread.setDelayTime(value + 10);
90 | }
91 | }
92 |
93 | @Override
94 | protected void fabClick() {
95 | boolean ended = true;
96 |
97 | for (AlgorithmThread thread : threads) {
98 | if (thread.isStarted()) {
99 | ended = false;
100 | break;
101 | }
102 | }
103 |
104 | if (ended) {//If you have sorted or not sorted
105 | startSort();
106 | } else { //if it is sorting, pause or resume threads
107 | boolean isAllPause = true;
108 | for (AlgorithmThread thread : threads) {
109 | if (!thread.isPaused()) {
110 | isAllPause = false;
111 | break;
112 | }
113 | }
114 | if (isAllPause) {
115 | resumeSort();
116 | } else {
117 | pauseSort();
118 | }
119 | }
120 | }
121 |
122 | protected void startSort() {
123 | if (createData()) {
124 | numThreadCompleted = 0;
125 | for (AlgorithmThread thread : threads) {
126 | thread.sendMessage(AlgorithmThread.COMMAND_START_ALGORITHM);
127 | }
128 | mPlay.setImageResource(R.drawable.ic_pause_white_24dp);
129 | mPlayAnimation.show();
130 | disableView();
131 | }
132 | }
133 |
134 | @Override
135 | public void setupFragment(String name) {
136 | mEditNumber.setText("50");
137 | mTimeBar.setProgress(100);
138 |
139 | threads[0] = new BubbleSortThread(mSortViews[0], getActivity());
140 | threads[1] = new InsertionSortThread(mSortViews[1], getActivity());
141 | threads[2] = new SelectionSortThread(mSortViews[2], getActivity());
142 | threads[3] = new QuickSortThread(mSortViews[3], getActivity());
143 | threads[4] = new MergeSortThread(mSortViews[4], getActivity());
144 | threads[5] = new ShellSortThread(mSortViews[5], getActivity());
145 | threads[6] = new CocktailShakerSortThread(mSortViews[6], getActivity());
146 | //disable animate
147 | for (SortAlgorithmThread thread : threads) {
148 | thread.setSwapAnimateEnable(false);
149 | thread.setCompletionListener(this);
150 |
151 | }
152 | generateRandomData();
153 | }
154 |
155 | @Override
156 | protected void generateRandomData() {
157 | //check empty length of array
158 | if (mEditNumber.getText().toString().isEmpty()) {
159 | mCountData = Integer.parseInt(mEditNumber.getHint().toString());
160 | } else {
161 | mCountData = Integer.parseInt(mEditNumber.getText().toString());
162 | }
163 |
164 | int[] array = ArrayUtils.createIntArray(mCountData);
165 | for (SortAlgorithmThread thread : threads) {
166 | thread.setData(array.clone());
167 | }
168 | mEditArray.setText(ArrayUtils.arrayToString(array));
169 | }
170 |
171 |
172 | @Override
173 | public boolean createData() {
174 | Log.d(TAG, "createData: ");
175 |
176 | //set time sleep for threads[
177 | int delayTime = mTimeBar.getProgress();
178 | for (AlgorithmThread thread : threads) {
179 | thread.setDelayTime(delayTime);
180 | }
181 |
182 | //check empty length of array
183 | if (mEditNumber.getText().toString().isEmpty()) {
184 | mCountData = Integer.parseInt(mEditNumber.getHint().toString());
185 | } else {
186 | mCountData = Integer.parseInt(mEditNumber.getText().toString());
187 | }
188 |
189 | String raw = mEditArray.getText().toString();
190 | final String strArray[] = raw.split(Pattern.quote(","));
191 | if (strArray.length == mCountData) {
192 | int[] array = ArrayUtils.arrayStringToInt(strArray);
193 |
194 | //If receive an error during conversion
195 | if (array[0] == -1) {
196 | return false;
197 | }
198 | for (SortAlgorithmThread thread : threads) {
199 | thread.setData(array.clone());
200 | }
201 | return true;
202 | } else {
203 | mSlidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
204 | if (strArray.length > mCountData) {
205 | Toast.makeText(getActivity(), "Please remove " +
206 | +(strArray.length - mCountData)
207 | + " entry of array, length of array is "
208 | + strArray.length, Toast.LENGTH_LONG).show();
209 | } else {
210 | Toast.makeText(getActivity(), "Please add "
211 | + (-strArray.length + mCountData)
212 | + " entry of array, length of array is "
213 | + strArray.length, Toast.LENGTH_LONG).show();
214 | }
215 |
216 | return false;
217 | }
218 | }
219 |
220 | @Override
221 | protected void pauseSort() {
222 | for (AlgorithmThread thread : threads) {
223 | thread.setPaused(true);
224 | mPlay.setImageResource(R.drawable.ic_play_arrow_white_24dp);
225 | mPlayAnimation.hide();
226 | }
227 | }
228 |
229 | @Override
230 | protected void resumeSort() {
231 | for (AlgorithmThread thread : threads) {
232 | thread.setPaused(false);
233 | }
234 | mPlay.setImageResource(R.drawable.ic_pause_white_24dp);
235 | mPlayAnimation.show();
236 | }
237 |
238 | @Override
239 | public void onSortCompleted() {
240 | numThreadCompleted++;
241 | if (numThreadCompleted == threads.length) {
242 | if (getActivity() != null) {
243 | getActivity().runOnUiThread(new Runnable() {
244 | @Override
245 | public void run() {
246 | Toast.makeText(getActivity().getApplicationContext(), R.string.sorted,
247 | Toast.LENGTH_SHORT).show();
248 | enableView();
249 | mPlayAnimation.hide();
250 | mPlay.setImageResource(R.drawable.ic_settings_backup_restore_white_24dp);
251 | }
252 | });
253 | }
254 | }
255 | super.onSortCompleted();
256 |
257 | }
258 | }
259 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/AlgorithmThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms;
16 |
17 | import android.app.Activity;
18 | import android.os.Handler;
19 | import android.os.HandlerThread;
20 | import android.os.Message;
21 |
22 | import com.duy.algorithm.SortCompletionListener;
23 | import com.duy.algorithm.customview.LogView;
24 |
25 | import java.util.concurrent.atomic.AtomicBoolean;
26 |
27 | public class AlgorithmThread extends HandlerThread {
28 | public static final String KEY_ALGORITHM = "KEY_ALGORITHM";
29 | public static final String COMMAND_START_ALGORITHM = "start";
30 | private final static String TAG = "AlgorithmThread";
31 | private final AtomicBoolean paused = new AtomicBoolean(false);
32 | private final Object pauseLock = new Object();
33 | protected Activity activity;
34 | protected long delayTime = 50;
35 | private SortCompletionListener completionListener;
36 | private boolean started;
37 | private Handler mHandler;
38 | private LogView mLogger;
39 |
40 | public AlgorithmThread() {
41 | super("");
42 | }
43 |
44 | public long getDelayTime() {
45 | return delayTime;
46 | }
47 |
48 | public void setDelayTime(long delayTime) {
49 | this.delayTime = delayTime;
50 | }
51 |
52 | public void sleep() {
53 | sleepFor(delayTime);
54 | }
55 |
56 | public void sleepFor(long time) {
57 | try {
58 | sleep(time);
59 | if (isPaused())
60 | pauseExecution();
61 | else resumeExecution();
62 | } catch (InterruptedException e) {
63 | e.printStackTrace();
64 | Thread.currentThread().interrupt();
65 | }
66 | }
67 |
68 | public String getString(int resID){
69 | return activity.getString(resID);
70 | }
71 |
72 | public void startExecution() {
73 | started = true;
74 | sleepFor(delayTime * 2);
75 | }
76 |
77 | /**
78 | * pause thread
79 | */
80 | private void pauseExecution() {
81 | if (paused.get()) {
82 | synchronized (getPauseLock()) {
83 | if (paused.get()) {
84 | try {
85 | getPauseLock().wait();
86 | } catch (InterruptedException ignored) {
87 | }
88 | }
89 | }
90 | }
91 | }
92 |
93 | /**
94 | * resume thread
95 | */
96 | private void resumeExecution() {
97 | synchronized (pauseLock) {
98 | pauseLock.notifyAll();
99 | }
100 | }
101 |
102 | private Object getPauseLock() {
103 | return pauseLock;
104 | }
105 |
106 | public boolean isPaused() {
107 | return paused.get();
108 | }
109 |
110 | public void setPaused(boolean b) {
111 | paused.set(b);
112 | if (!b) {
113 | synchronized (getPauseLock()) {
114 | getPauseLock().notify();
115 | }
116 | }
117 | }
118 |
119 | public boolean isStarted() {
120 | return started;
121 | }
122 |
123 | public void setStarted(boolean started) {
124 | this.started = started;
125 | }
126 |
127 | public void showLog(final String log) {
128 | activity.runOnUiThread(new Runnable() {
129 | @Override
130 | public void run() {
131 | if (mLogger != null)
132 | mLogger.addLog(log);
133 | }
134 | });
135 | }
136 |
137 | public void showLog(final String message, final int[] array) {
138 | activity.runOnUiThread(new Runnable() {
139 | @Override
140 | public void run() {
141 | if (mLogger != null)
142 | mLogger.addLog(message, array.clone());
143 |
144 | }
145 | });
146 | }
147 |
148 | public void setCompletionListener(SortCompletionListener completionListener) {
149 | this.completionListener = completionListener;
150 | }
151 |
152 | public void prepareHandler(final IDataHandler dataHandler) {
153 | mHandler = new Handler(getLooper(), new Handler.Callback() {
154 | @Override
155 | public boolean handleMessage(Message msg) {
156 | if (msg.obj instanceof String) {
157 | dataHandler.onMessageReceived((String) msg.obj);
158 | } else {
159 | dataHandler.onDataReceived(msg.obj);
160 | }
161 | return true;
162 | }
163 | });
164 |
165 | }
166 |
167 | /**
168 | * send data
169 | *
170 | * @param data
171 | */
172 | public void sendData(Object data) {
173 | mHandler.obtainMessage(1, data).sendToTarget();
174 | }
175 |
176 | /**
177 | * send command
178 | *
179 | * @param message
180 | */
181 | public void sendMessage(String message) {
182 | mHandler.obtainMessage(1, message).sendToTarget();
183 | }
184 |
185 | /**
186 | * completed sort
187 | */
188 | public void onCompleted() {
189 | if (completionListener != null) completionListener.onSortCompleted();
190 | started = false;
191 | }
192 |
193 | public void setLogger(LogView mLogger) {
194 | this.mLogger = mLogger;
195 | }
196 |
197 | public static final class ALGORITHM_NAME {
198 | public static final String BUBBLE_SORT = "BUBBLE_SORT";
199 | public static final String INSERTION_SORT = "INSERTION_SORT";
200 | public static final String SELECTION_SORT = "SELECTION_SORT";
201 | public static final String QUICK_SORT = "QUICK_SORT";
202 | public static final String SHELL_SORT = "SHELL_SORT";
203 | public static final String MERGE_SORT = "MERGE_SORT";
204 | public static final String COCKTAIL_SORT = "COCKTAIL_SORT";
205 | public static final String BOGO_SORT = "BOGO_SORT";
206 | public static final String COUNTING_SORT = "COUNTING_SORT";
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/IDataHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms;
16 |
17 | public interface IDataHandler {
18 |
19 | void onDataReceived(Object data);
20 |
21 | void onMessageReceived(String message);
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/SortAlgorithmThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms;
16 |
17 |
18 | import android.support.annotation.UiThread;
19 |
20 | import com.duy.algorithm.customview.SortView;
21 |
22 | public class SortAlgorithmThread extends AlgorithmThread implements IDataHandler {
23 |
24 | public static final String TAG = SortAlgorithmThread.class.getSimpleName();
25 | public SortView mSortView;
26 | private boolean isPrepared = false;
27 | private boolean swapAnimateEnable = true;
28 |
29 | public void prepare() {
30 | if (isPrepared) return;
31 | start();
32 | prepareHandler(this);
33 | isPrepared = true;
34 | }
35 |
36 | /**
37 | * set array data for view
38 | *
39 | * @param array
40 | */
41 | @UiThread
42 | public void setData(final int[] array) {
43 | prepare();
44 | activity.runOnUiThread(new Runnable() {
45 | @Override
46 | public void run() {
47 | mSortView.setCompletePosition(-1);
48 | mSortView.setArray(array);
49 | mSortView.setTime(0);
50 | mSortView.invalidate();
51 | }
52 | });
53 | sendData(array);
54 | }
55 |
56 | /**
57 | * highlight swap
58 | */
59 | @UiThread
60 | public void onSwapping(final int one, final int two) {
61 |
62 | if (swapAnimateEnable) { //some cases without effect
63 | mSortView.setSwapPosition(one, two, false);
64 | //xA always smaller xB
65 | int delta = mSortView.getDelta();
66 | long timeSleep;
67 | if (delta == 0) {
68 | timeSleep = 1;
69 | } else {
70 | timeSleep = (long) (delayTime * 1.5 / delta);
71 | }
72 | while (delta >= 0) {
73 | activity.runOnUiThread(new Runnable() {
74 | @Override
75 | public void run() {
76 | mSortView.incPositionSwap(2);
77 | }
78 | });
79 | sleepFor(timeSleep);
80 | delta -= 2;
81 | }
82 | } else {
83 | activity.runOnUiThread(new Runnable() {
84 | @Override
85 | public void run() {
86 | mSortView.setSwapPosition(one, two);
87 | }
88 | });
89 | sleep();
90 | }
91 | }
92 |
93 | /**
94 | * highlight swap
95 | */
96 | @UiThread
97 | public void onSwapped() {
98 | activity.runOnUiThread(new Runnable() {
99 | @Override
100 | public void run() {
101 | mSortView.setSwapPosition(-1, -1);
102 | }
103 | });
104 | sleep();
105 | }
106 |
107 | /**
108 | * highlight swap
109 | */
110 | @UiThread
111 | public void onSwapped(boolean b) {
112 | activity.runOnUiThread(new Runnable() {
113 | @Override
114 | public void run() {
115 | mSortView.setSwapPosition(-1, -1);
116 | }
117 | });
118 | }
119 |
120 | /**
121 | * highlight trace
122 | *
123 | * @param position - position for trace
124 | */
125 | public void onTrace(final int position) {
126 | activity.runOnUiThread(new Runnable() {
127 | @Override
128 | public void run() {
129 | mSortView.setTracePosition(position);
130 | }
131 | });
132 | }
133 |
134 | /**
135 | * highlight target
136 | *
137 | * @param position-position for target
138 | */
139 | public void onTarget(final int position) {
140 | activity.runOnUiThread(new Runnable() {
141 | @Override
142 | public void run() {
143 | mSortView.setTargetPosition(position);
144 | }
145 | });
146 | }
147 |
148 | @Override
149 | public void onDataReceived(Object data) {
150 |
151 | }
152 |
153 | @Override
154 | public void onMessageReceived(String message) {
155 |
156 | }
157 |
158 | /**
159 | * array is sorted
160 | */
161 | @Override
162 | public void onCompleted() {
163 | finishSorting();
164 | super.onCompleted();
165 | }
166 |
167 |
168 | private void finishSorting() {
169 | for (int i = 0; i < mSortView.getSizeArray(); i++) {
170 | mSortView.setCompletePosition(i);
171 | activity.runOnUiThread(new Runnable() {
172 | @Override
173 | public void run() {
174 | mSortView.invalidate();
175 | }
176 | });
177 | sleepFor(delayTime / 3);
178 | }
179 |
180 | activity.runOnUiThread(new Runnable() {
181 | @Override
182 | public void run() {
183 | // mSortView.setCompletePosition(-1);
184 | mSortView.setTracePosition(-1);
185 | mSortView.invalidate();
186 | }
187 | });
188 | }
189 |
190 | @Override
191 | public void sleep() {
192 | activity.runOnUiThread(new Runnable() {
193 | @Override
194 | public void run() {
195 | // mSortView.setCompletePosition(-1);
196 | mSortView.addTimeUnit(1);
197 | mSortView.invalidate();
198 | }
199 | });
200 | super.sleep();
201 | }
202 |
203 | public boolean isSwapAnimateEnable() {
204 | return swapAnimateEnable;
205 | }
206 |
207 | public void setSwapAnimateEnable(boolean swapAnimateEnable) {
208 | this.swapAnimateEnable = swapAnimateEnable;
209 | }
210 | }
211 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/BogoSortThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms.algo;
16 |
17 |
18 | import android.app.Activity;
19 |
20 | import com.duy.algorithm.R;
21 | import com.duy.algorithm.algorithms.AlgorithmThread;
22 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
23 | import com.duy.algorithm.customview.SortView;
24 | import com.duy.algorithm.utils.ArrayUtils;
25 |
26 | public class BogoSortThread extends SortAlgorithmThread {
27 |
28 | int[] array;
29 |
30 | public BogoSortThread(SortView sortView, Activity activity) {
31 | this.mSortView = sortView;
32 | this.activity = activity;
33 | }
34 |
35 |
36 | public boolean bogoIsSorted(int[] arr) {
37 | for (int i = 1; i < arr.length; i++)
38 | if (arr[i] < arr[i - 1])
39 | return false;
40 | return true;
41 | }
42 |
43 | public void sort() {
44 | showLog("Original array - ", array);
45 |
46 | while (!bogoIsSorted(array)) {
47 | for (int i = 0; i < array.length; i++) {
48 | showLog("Doing iteration - " + i);
49 |
50 |
51 | int j = (int) (Math.random() * array.length);
52 | onSwapping(i, j);
53 | sleep();
54 | showLog("Swapping " + array[i] + " and " + array[j]);
55 |
56 | ArrayUtils.swap(array, i, j);
57 |
58 | onSwapping(i, j);
59 | sleep();
60 | }
61 | //sleep(100.0);
62 | }
63 | showLog(getString(R.string.arr_sorted), array);
64 | onCompleted();
65 | }
66 |
67 |
68 | @Override
69 | public void onDataReceived(Object data) {
70 | super.onDataReceived(data);
71 | this.array = (int[]) data;
72 | }
73 |
74 | @Override
75 | public void onMessageReceived(String message) {
76 | super.onMessageReceived(message);
77 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
78 | startExecution();
79 | sort();
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/BubbleSortThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms.algo;
16 |
17 |
18 | import android.app.Activity;
19 |
20 | import com.duy.algorithm.R;
21 | import com.duy.algorithm.algorithms.AlgorithmThread;
22 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
23 | import com.duy.algorithm.customview.SortView;
24 |
25 | public class BubbleSortThread extends SortAlgorithmThread {
26 |
27 | int[] array;
28 |
29 | public BubbleSortThread(SortView sortView, Activity activity) {
30 | this.mSortView = sortView;
31 | this.activity = activity;
32 | }
33 |
34 | public void sort() {
35 | showLog(getString(R.string.original_arr), array);
36 |
37 | for (int i = array.length - 1; i >= 0; i--) {
38 | // showLog("i = " + i);
39 | boolean swapped = false;
40 | for (int j = 0; j < i; j++) {
41 | //handle trace
42 | onTrace(j);
43 | sleep();
44 |
45 | if (array[j] > array[j + 1]) {
46 | //swapping
47 | onSwapping(j, j + 1);
48 |
49 | showLog("Swapping a[" + i + "]=" + array[j] + " and a[" + j + "]=" + array[j + 1]);
50 | int temp = array[j];
51 | array[j] = array[j + 1];
52 | array[j + 1] = temp;
53 |
54 | //swapped
55 | onSwapped();
56 | swapped = true;
57 | }
58 | }
59 | if (!swapped) {
60 | break;
61 | }
62 | }
63 | showLog(getString(R.string.arr_sorted), array);
64 |
65 | onCompleted();
66 | }
67 |
68 | @Override
69 | public void run() {
70 | super.run();
71 | }
72 |
73 |
74 | @Override
75 | public void onDataReceived(Object data) {
76 | super.onDataReceived(data);
77 | this.array = (int[]) data;
78 | }
79 |
80 | @Override
81 | public void onMessageReceived(String message) {
82 | super.onMessageReceived(message);
83 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
84 | startExecution();
85 | sort();
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/CocktailShakerSortThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms.algo;
16 |
17 |
18 | import android.app.Activity;
19 |
20 | import com.duy.algorithm.R;
21 | import com.duy.algorithm.algorithms.AlgorithmThread;
22 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
23 | import com.duy.algorithm.customview.SortView;
24 | import com.duy.algorithm.utils.ArrayUtils;
25 |
26 | public class CocktailShakerSortThread extends SortAlgorithmThread {
27 |
28 | int[] array;
29 |
30 | public CocktailShakerSortThread(SortView sortView, Activity activity) {
31 | this.mSortView = sortView;
32 | this.activity = activity;
33 | }
34 |
35 |
36 | public void cocktailShakerSort() {
37 | int i = 0;
38 | while (i < array.length / 2) {
39 | for (int j = i; j < array.length - i - 1; j++) {
40 | showLog("Doing iteration - " + i);
41 | onTrace(j);
42 | sleep();
43 |
44 | if (array[j] > array[j + 1]) {
45 |
46 | showLog("Swapping " + array[j] + " and " + array[j + 1]);
47 | onSwapping(j, j + 1);
48 |
49 | ArrayUtils.swap(array, j, j + 1);
50 |
51 | onSwapped();
52 | }
53 | }
54 | for (int j = array.length - i - 1; j > i; j--) {
55 | showLog("Doing iteration - " + i);
56 | onTrace(j);
57 | sleep();
58 |
59 | if (array[j] < array[j - 1]) {
60 |
61 | onSwapping(j, j - 1);
62 | showLog("Swapping " + array[j] + " and " + array[j - 1]);
63 |
64 | ArrayUtils.swap(array, j, j - 1);
65 |
66 | onSwapped();
67 | }
68 | }
69 | i++;
70 | }
71 | }
72 |
73 | public void sort() {
74 | showLog(getString(R.string.original_arr), array);
75 |
76 | cocktailShakerSort();
77 | showLog(getString(R.string.arr_sorted), array);
78 |
79 | onCompleted();
80 | }
81 |
82 |
83 | @Override
84 | public void onDataReceived(Object data) {
85 | super.onDataReceived(data);
86 | this.array = (int[]) data;
87 | }
88 |
89 | @Override
90 | public void onMessageReceived(String message) {
91 | super.onMessageReceived(message);
92 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
93 | startExecution();
94 | sort();
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/CountingSortThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms.algo;
16 |
17 |
18 | import android.app.Activity;
19 |
20 | import com.duy.algorithm.R;
21 | import com.duy.algorithm.algorithms.AlgorithmThread;
22 | import com.duy.algorithm.utils.ArrayUtils;
23 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
24 | import com.duy.algorithm.customview.SortView;
25 |
26 | public class CountingSortThread extends SortAlgorithmThread {
27 |
28 | int[] array;
29 |
30 | public CountingSortThread(SortView sortView, Activity activity) {
31 | this.mSortView = sortView;
32 | this.activity = activity;
33 | }
34 |
35 |
36 | public void sort() {
37 | showLog(getString(R.string.original_arr), array);
38 |
39 |
40 | int max = ArrayUtils.findMax(array);
41 | int[] counts = new int[max + 1];
42 | for (int i = 0; i < array.length; i++) {
43 | showLog("Doing iteration - " + i);
44 | onTrace(i);
45 | sleep();
46 |
47 | counts[array[i]]++;
48 | }
49 | int x = 0;
50 | for (int i = 0; i < array.length; i++) {
51 | showLog("Doing iteration - " + i);
52 | onTrace(i);
53 | sleep();
54 |
55 | if (counts[x] == 0)
56 | x++;
57 | array[i] = x;
58 | counts[x]--;
59 | }
60 |
61 | showLog(getString(R.string.arr_sorted), array);
62 |
63 | onCompleted();
64 | }
65 |
66 | @Override
67 | public void onDataReceived(Object data) {
68 | super.onDataReceived(data);
69 | this.array = (int[]) data;
70 | }
71 |
72 | @Override
73 | public void onMessageReceived(String message) {
74 | super.onMessageReceived(message);
75 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
76 | startExecution();
77 | sort();
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/InsertionSortThread.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Naman Dwivedi
3 | *
4 | * Licensed under the GNU General Public License v3
5 | *
6 | * This is free software: you can redistribute it and/or modify it
7 | * under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
9 | *
10 | * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 | * See the GNU General Public License for more details.
13 | */
14 |
15 | package com.duy.algorithm.algorithms.algo;
16 |
17 | import android.app.Activity;
18 |
19 | import com.duy.algorithm.R;
20 | import com.duy.algorithm.algorithms.AlgorithmThread;
21 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
22 | import com.duy.algorithm.customview.SortView;
23 | import com.duy.algorithm.utils.ArrayUtils;
24 |
25 | public class InsertionSortThread extends SortAlgorithmThread {
26 | int[] array;
27 |
28 | public InsertionSortThread(SortView sortView, Activity activity) {
29 | this.mSortView = sortView;
30 | this.activity = activity;
31 | }
32 |
33 | public void sort() {
34 | showLog(getString(R.string.original_arr), array);
35 |
36 |
37 | int pos;
38 | for (int i = 1; i < array.length; i++) {
39 | pos = i;
40 | onTrace(i);
41 | sleep();
42 |
43 | while (pos > 0 && array[pos] < array[pos - 1]) {
44 | onSwapping(pos, pos - 1);//swapping
45 |
46 | showLog("Swapping a[" + pos + "]=" + array[pos]
47 | + " and a[" + (pos - 1) + "]=" + array[pos - 1]);
48 | ArrayUtils.swap(array, pos, pos - 1);
49 |
50 | onSwapped();
51 | pos--;
52 | }
53 | }
54 | showLog(getString(R.string.arr_sorted), array);
55 |
56 | onCompleted();
57 | }
58 |
59 |
60 | @Override
61 | public void run() {
62 | super.run();
63 | }
64 |
65 |
66 | @Override
67 | public void onDataReceived(Object data) {
68 | super.onDataReceived(data);
69 | this.array = (int[]) data;
70 | }
71 |
72 | @Override
73 | public void onMessageReceived(String message) {
74 | super.onMessageReceived(message);
75 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
76 | startExecution();
77 | sort();
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/MergeSortThread.java:
--------------------------------------------------------------------------------
1 | package com.duy.algorithm.algorithms.algo;
2 |
3 | import android.app.Activity;
4 |
5 | import com.duy.algorithm.R;
6 | import com.duy.algorithm.algorithms.AlgorithmThread;
7 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
8 | import com.duy.algorithm.customview.SortView;
9 |
10 | public class MergeSortThread extends SortAlgorithmThread {
11 |
12 | private int[] array;
13 | private int[] aux;
14 |
15 | public MergeSortThread(SortView sortView, Activity activity) {
16 | this.mSortView = sortView;
17 | this.activity = activity;
18 | }
19 |
20 | private void merge(int lo, int mid, int hi) {
21 | int l = lo;
22 | int r = mid + 1;
23 |
24 | System.arraycopy(array, lo, aux, lo, hi + 1 - lo);
25 |
26 | for (int k = l; k <= hi; k++) {
27 | onTrace(k);
28 | sleep();
29 | int rec = 0;
30 | if (l > mid) {
31 | rec = r;
32 | array[k] = aux[r++];
33 | } else if (r > hi) {
34 | rec = l;
35 | array[k] = aux[l++];
36 | } else if (aux[r] < aux[l]) {
37 | rec = r;
38 | array[k] = aux[r++];
39 | } else {
40 | rec = l;
41 | array[k] = aux[l++];
42 | }
43 |
44 | // onSwapping(rec, k);
45 | // sleep();
46 | }
47 |
48 |
49 | }
50 |
51 | private void sort(int lo, int hi) {
52 |
53 | if (hi <= lo) return;
54 | int mid = lo + (hi - lo) / 2;
55 | sort(lo, mid); // 将左半部分排序
56 | sort(mid + 1, hi); // 将又半部分排序
57 | merge(lo, mid, hi); // 归并结果
58 |
59 | }
60 |
61 |
62 | public void sort() {
63 | showLog(getString(R.string.original_arr), array);
64 |
65 | aux = new int[array.length];
66 | sort(0, array.length - 1);
67 | showLog(getString(R.string.arr_sorted), array);
68 |
69 | onCompleted();
70 | }
71 |
72 | @Override
73 | public void run() {
74 | super.run();
75 | }
76 |
77 |
78 | @Override
79 | public void onDataReceived(Object data) {
80 | super.onDataReceived(data);
81 | this.array = (int[]) data;
82 | }
83 |
84 | @Override
85 | public void onMessageReceived(String message) {
86 | super.onMessageReceived(message);
87 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
88 | startExecution();
89 | sort();
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/QuickSortThread.java:
--------------------------------------------------------------------------------
1 | package com.duy.algorithm.algorithms.algo;
2 |
3 | import android.app.Activity;
4 |
5 | import com.duy.algorithm.R;
6 | import com.duy.algorithm.algorithms.AlgorithmThread;
7 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
8 | import com.duy.algorithm.customview.SortView;
9 | import com.duy.algorithm.utils.ArrayUtils;
10 |
11 | public class QuickSortThread extends SortAlgorithmThread {
12 | public static final String TAG = "QuickSort";
13 | private int[] array;
14 |
15 | public QuickSortThread(SortView sortView, Activity activity) {
16 | this.mSortView = sortView;
17 | this.activity = activity;
18 | }
19 |
20 | private void sort(int l, int r) {
21 | int i = l;
22 | int j = r;
23 | int key = array[(l + r) / 2];
24 | while (i <= j) {
25 | while (array[i] < key) {
26 | onTrace(i);
27 | sleep();
28 | i++;
29 | }
30 | while (array[j] > key) {
31 | onTrace(j);
32 | sleep();
33 | j--;
34 | }
35 | if (i <= j) {
36 | if (array[i] > array[j]) {
37 | onSwapping(i, j);
38 |
39 | ArrayUtils.swap(array, i, j);
40 |
41 | //swapped, show it
42 | onSwapped();
43 | }
44 | i++;
45 | j--;
46 | }
47 | }
48 | if (i < r) sort(i, r);
49 | if (l < j) sort(l, j);
50 | }
51 |
52 | public void sort() {
53 | showLog(getString(R.string.original_arr), array);
54 |
55 | sort(0, array.length - 1);
56 | showLog(getString(R.string.arr_sorted), array);
57 |
58 | onCompleted();
59 | }
60 |
61 | @Override
62 | public void run() {
63 | super.run();
64 | }
65 |
66 |
67 | @Override
68 | public void onDataReceived(Object data) {
69 | super.onDataReceived(data);
70 | this.array = (int[]) data;
71 | }
72 |
73 | @Override
74 | public void onMessageReceived(String message) {
75 | super.onMessageReceived(message);
76 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
77 | startExecution();
78 | sort();
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/SelectionSortThread.java:
--------------------------------------------------------------------------------
1 | package com.duy.algorithm.algorithms.algo;
2 |
3 | import android.app.Activity;
4 |
5 | import com.duy.algorithm.R;
6 | import com.duy.algorithm.algorithms.AlgorithmThread;
7 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
8 | import com.duy.algorithm.customview.SortView;
9 |
10 | /**
11 | * Created by amit on 21/11/16.
12 | */
13 |
14 | public class SelectionSortThread extends SortAlgorithmThread {
15 |
16 | int[] array;
17 |
18 | public SelectionSortThread(SortView sortView, Activity activity) {
19 | this.mSortView = sortView;
20 | this.activity = activity;
21 | }
22 |
23 | public void sort() {
24 | showLog(getString(R.string.original_arr), array);
25 |
26 |
27 | int n = array.length;
28 | for (int i = 0; i < n - 1; i++) {
29 | int min_idx = i;
30 | onTarget(i);
31 | sleep();
32 |
33 | for (int j = i + 1; j < n; j++) {
34 | onTrace(j);
35 | sleep();
36 |
37 | if (array[j] < array[min_idx]) {
38 | min_idx = j;
39 | onTarget(j);
40 | }
41 | }
42 |
43 | //swapping
44 | onTarget(min_idx);
45 | onSwapping(min_idx, i);
46 |
47 | int temp = array[min_idx];
48 | array[min_idx] = array[i];
49 | array[i] = temp;
50 |
51 | showLog("Swapping " + array[i] + " and " + temp);
52 |
53 | //swapped
54 | onTarget(i);
55 | onSwapped();
56 |
57 |
58 | }
59 | showLog(getString(R.string.arr_sorted), array);
60 |
61 | onCompleted();
62 | }
63 |
64 | @Override
65 | public void run() {
66 | super.run();
67 | }
68 |
69 |
70 | @Override
71 | public void onDataReceived(Object data) {
72 | super.onDataReceived(data);
73 | this.array = (int[]) data;
74 | }
75 |
76 | @Override
77 | public void onMessageReceived(String message) {
78 | super.onMessageReceived(message);
79 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
80 | startExecution();
81 | sort();
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/algorithms/algo/ShellSortThread.java:
--------------------------------------------------------------------------------
1 | package com.duy.algorithm.algorithms.algo;
2 |
3 |
4 | import android.app.Activity;
5 |
6 | import com.duy.algorithm.R;
7 | import com.duy.algorithm.algorithms.AlgorithmThread;
8 | import com.duy.algorithm.algorithms.SortAlgorithmThread;
9 | import com.duy.algorithm.customview.SortView;
10 |
11 | public class ShellSortThread extends SortAlgorithmThread {
12 |
13 | int[] array;
14 |
15 | public ShellSortThread(SortView sortView, Activity activity) {
16 | this.mSortView = sortView;
17 | this.activity = activity;
18 | }
19 |
20 | public void sort() {
21 | showLog(getString(R.string.original_arr), array);
22 |
23 |
24 | int h = 1;
25 | while (h < array.length) h = 3 * h + 1;
26 | while (h > 0) {
27 | h = h / 3;
28 | for (int k = 0; k < h; k++) {
29 | for (int i = h + k; i < array.length; i += h) {
30 | onTrace(i);
31 | sleep();
32 |
33 | int key = array[i];
34 | int j = i - h;
35 |
36 | while (j >= 0 && array[j] > key) {
37 | onSwapping(j, j + h);
38 | sleep();
39 |
40 | array[j + h] = array[j];
41 | onSwapped(false);
42 | j -= h;
43 | }
44 | array[j + h] = key;
45 | //-> invariant: array[0,h,2*h..j] is sorted
46 | }
47 | }
48 | //->invariant: each h-sub-array is sorted
49 | }
50 |
51 | showLog(getString(R.string.arr_sorted), array);
52 |
53 | onCompleted();
54 |
55 | }
56 |
57 |
58 | @Override
59 | public void run() {
60 | super.run();
61 | }
62 |
63 |
64 | @Override
65 | public void onDataReceived(Object data) {
66 | super.onDataReceived(data);
67 | this.array = (int[]) data;
68 | }
69 |
70 | @Override
71 | public void onMessageReceived(String message) {
72 | super.onMessageReceived(message);
73 | if (message.equals(AlgorithmThread.COMMAND_START_ALGORITHM)) {
74 | startExecution();
75 | sort();
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/customview/EditTextInputArray.java:
--------------------------------------------------------------------------------
1 | package com.duy.algorithm.customview;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.AppCompatEditText;
5 | import android.text.InputType;
6 | import android.text.method.NumberKeyListener;
7 | import android.util.AttributeSet;
8 |
9 | /**
10 | * Created by DUy on 03-Feb-17.
11 | */
12 |
13 | public class EditTextInputArray extends AppCompatEditText {
14 |
15 | public EditTextInputArray(Context context) {
16 | super(context);
17 | init(context);
18 | }
19 |
20 | public EditTextInputArray(Context context, AttributeSet attrs) {
21 | super(context, attrs);
22 | init(context);
23 |
24 | }
25 |
26 | public EditTextInputArray(Context context, AttributeSet attrs, int defStyleAttr) {
27 | super(context, attrs, defStyleAttr);
28 | init(context);
29 |
30 | }
31 |
32 | private void init(Context context) {
33 | setKeyListener(new NumberKeyListener() {
34 | @Override
35 | protected char[] getAcceptedChars() {
36 | return "1234567890,".toCharArray();
37 | }
38 |
39 | @Override
40 | public int getInputType() {
41 | return InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
42 | }
43 | });
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/duy/algorithm/customview/LogView.java:
--------------------------------------------------------------------------------
1 | package com.duy.algorithm.customview;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.widget.LinearLayoutManager;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.util.AttributeSet;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.widget.TextView;
12 |
13 | import com.duy.algorithm.R;
14 |
15 | import java.util.ArrayList;
16 |
17 | /**
18 | * Created by DUy on 04-Feb-17.
19 | */
20 |
21 | public class LogView extends RecyclerView {
22 | private LogAdapter mLogAdapter;
23 | private View mEmptyView;
24 |
25 | public LogView(Context context) {
26 | super(context);
27 | setup(context);
28 | }
29 |
30 | public LogView(Context context, @Nullable AttributeSet attrs) {
31 | super(context, attrs);
32 | setup(context);
33 |
34 | }
35 |
36 | public LogView(Context context, @Nullable AttributeSet attrs, int defStyle) {
37 | super(context, attrs, defStyle);
38 | setup(context);
39 |
40 | }
41 |
42 | private void setup(Context context) {
43 | if (!isInEditMode()) {
44 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
45 | linearLayoutManager.setStackFromEnd(true);
46 | setLayoutManager(linearLayoutManager);
47 | mLogAdapter = new LogView.LogAdapter(context);
48 | setAdapter(mLogAdapter);
49 | }
50 | }
51 |
52 | public void addLog(String log) {
53 | mLogAdapter.addLog(log);
54 | if (mEmptyView != null) mEmptyView.setVisibility(GONE);
55 | scrollToPosition(mLogAdapter.getItemCount() - 1);
56 | }
57 |
58 | public void addLog(String log, int[] array) {
59 | if (mEmptyView != null) mEmptyView.setVisibility(GONE);
60 |
61 | String str = "[";
62 | for (int i = 0; i < array.length - 1; i++) {
63 | str += array[i];
64 | str += ", ";
65 | }
66 | if (array.length > 0) {
67 | str += array[array.length - 1];
68 | str += "]";
69 | }
70 | mLogAdapter.addLog(log + " " + str);
71 | scrollToPosition(mLogAdapter.getItemCount() - 1);
72 |
73 | }
74 |
75 | public void setmEmptyView(View mEmptyView) {
76 | this.mEmptyView = mEmptyView;
77 | }
78 |
79 | public static class LogAdapter extends Adapter