package com.exercise.AndroidBubbleSort;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidBubbleSortActivity extends Activity {
TextView src, result;
int[] data = new int[10];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
src = (TextView)findViewById(R.id.src);
result = (TextView)findViewById(R.id.result);
Random random = new Random();
String stringSrc = "Src = ";
for (int i = 0; i < data.length; i++){
data[i] = random.nextInt(100);
stringSrc += data[i] + " ";
}
src.setText(stringSrc);
bubblesort();
String stringResult = "Result = ";
for (int i = 0; i < data.length; i++){
stringResult += data[i] + " ";
}
result.setText(stringResult);
}
void bubblesort() {
int min;
for (int i = 0; i < data.length - 1; i++){
min = i;
for (int j = i + 1; j < data.length; j++){
if (data[j] < data[min]){
min = j;
}
}
if (i != min){
int tmp = data[i];
data[i] = data[min];
data[min] = tmp;
}
}
}
}
Saturday, 3 September 2011
Bubble Sort
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment