This video shows the steps to implement or create a Quick Sort Android App using Android Studio.
For Bubble Sort Tutorial please refer to the below video:
https://youtu.be/8GXwFImJgpU
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
Source Code:
package com.example.myquicksortapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);editText = findViewById(R.id.editText);
textView = findViewById(R.id.textview);
}public void SortButton(View view){
String[] stringsNumber = editText.getText().toString().split(“,”);
Integer[] integersNumber = new Integer[stringsNumber.length];for (int i = 0; i [LESS THAN] stringsNumber.length; i++){
integersNumber[i] = Integer.parseInt(stringsNumber[i]); //Converting from String Array to Integer Array
}
Integer[] sortedNumbers = QuickSort(integersNumber);
textView.setText(Arrays.toString(sortedNumbers));
}private Integer[] QuickSort(Integer[] numbers){
int n = numbers.length;
if(n [LESS THAN] 2){
return numbers;
}Integer[] sortedNumber = new Integer[n];
List[ANGLED BRACKET]Integer[ANGLED BRACKET] leftNumbers = new ArrayList[ANGLED BRACKET]Integer[ANGLED BRACKET]();
List[ANGLED BRACKET]Integer[ANGLED BRACKET] rightNumbers = new ArrayList[ANGLED BRACKET]Integer[ANGLED BRACKET]();for(int i=0;i [LESS THAN] n-1;i++){
if ((numbers[i] [LESS THAN] numbers[n-1])){
leftNumbers.add(numbers[i]);
}else{
rightNumbers.add(numbers[i]);
}
}
Integer[] leftNumberSorted = QuickSort(leftNumbers.toArray(new Integer[leftNumbers.size()]));
Integer[] rightNumberSorted = QuickSort(rightNumbers.toArray(new Integer[rightNumbers.size()]));
int k;
for (k=0; k [LESS THAN] leftNumberSorted.length;k++){
sortedNumber[k]=leftNumberSorted[k];
}
sortedNumber[k] = numbers[n-1];for (int j=0; j [LESS THAN] rightNumberSorted.length; j++){
sortedNumber[++k] = rightNumberSorted[j];
}
return sortedNumber;
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myquicksortapp”><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”><TextView
android:id=”@+id/textview”
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”30dp”
android:layout_marginTop=”250dp”
android:text=”@string/output_display”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”150dp”
android:layout_marginTop=”50dp”
android:onClick=”SortButton”
android:text=”@string/sort” /><EditText
android:id=”@+id/editText”
android:layout_width=”300dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”30dp”
android:layout_marginTop=”120dp”
android:ems=”10″
android:hint=”@string/input_here”
android:inputType=”textPersonName”
android:autofillHints=”” /></RelativeLayout>