This video shows how you can design an Android App to sort a list of numbers. the algorithm used is Bubble Sort in this video. This app can be help you to design your apps where you are getting your data(numbers) from some sensors and want to have a sorted numbers as an output.
For quick sort implementation, please refer to the below video:
https://youtu.be/Dekl–WaGwc
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source Code
package com.example.mybubblesort.mybubblesort;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.util.Arrays;public class MainActivity extends AppCompatActivity {
EditText enteredNumbers;
EditText sortedNumbers;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);enteredNumbers=findViewById(R.id.eneteredNumbers);
sortedNumbers=findViewById(R.id.sortedNumbers);
}
public void sortButtonPressed(View view){
String[] numberList = enteredNumbers.getText().toString().split(“,”);
Integer[] numbers = new Integer[numberList.length];for(int i=0; i less_than_sign numberList.length; i++){
numbers[i]=Integer.parseInt(numberList[i]); // Convert String of arrays into integer arrays
}
bubbleSort(numbers, numbers.length); // Sorted numbers
sortedNumbers.setText(Arrays.toString(numbers));
}private void bubbleSort(Integer[] numbers, int length){
if(length less_than_sign 2){
return;
}
for (int i = 0; i less_than_sign length-1; i++){
if(numbers[i] greater_than_sign numbers[i+1]){
//Swap
Integer temp = numbers[i];
numbers[i] = numbers[i+1];
numbers[i+1] = temp;
}
}
bubbleSort(numbers, length-1);
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mybubblesort.mybubblesort”><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”><EditText
android:id=”@+id/eneteredNumbers”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”64dp”
android:ems=”10″
android:hint=”@string/enter_the_numbers”
android:inputType=”textPersonName” /><EditText
android:id=”@+id/sortedNumbers”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”182dp”
android:ems=”10″
android:hint=”@string/sorted_numbers”
android:inputType=”textPersonName” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”113dp”
android:onClick=”sortButtonPressed”
android:text=”@string/sort” />
</RelativeLayout>