Saturday, September 3, 2011

Bubble Sort

BubbleSort


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;
}
}
}

}






1 comment:

  1. where is the code of your layout .. you need to show the code for the layout soo that we can understand your work.. i dont know what you button id or text id is.. if somebody copy this and paste to there eclipse the program still dont work because he need to put the button id.. or text id.

    ReplyDelete