大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Java技巧 > 直接选择排序Java实现

直接选择排序Java实现

关键词:直接选择排序Java直接选择排序Java实现  阅读(513) 赞(20)

[摘要]本文主要是对直接选择排序Java实现的讲解,希望对大家学习直接选择排序Java实现有所帮助。

  About this application:

  This application implements Straight Selection Sort algorithm, which is described like this:

  If there are N numbers, find the minimum and exchange it with the first number, then N-1 numbers remained. Continue to find the minimum number in the remained N-1 numbers, and exchange it with the second number. Repeat this, until all the numbers are in order.

  Note: This is SWT application, so you need org.eclipse.swt.win32.win32.x86_3.5.0.v3550b.jar, org.eclipse.jface_3.5.0.I20090525-2000.jar, org.eclipse.core.commands_3.5.0.I20090525-2000.jar, This is for Eclipse 3.5

  Source Code:

  package selection.sort;

  import java.util.ArrayList;

  import org.eclipse.swt.SWT;

  import org.eclipse.swt.events.KeyAdapter;

  import org.eclipse.swt.events.KeyEvent;

  import org.eclipse.swt.events.ModifyEvent;

  import org.eclipse.swt.events.ModifyListener;

  import org.eclipse.swt.events.SelectionAdapter;

  import org.eclipse.swt.events.SelectionEvent;

  import org.eclipse.swt.layout.FormAttachment;

  import org.eclipse.swt.layout.FormData;

  import org.eclipse.swt.layout.FormLayout;

  import org.eclipse.swt.widgets.Button;

  import org.eclipse.swt.widgets.Display;

  import org.eclipse.swt.widgets.Group;

  import org.eclipse.swt.widgets.Label;

  import org.eclipse.swt.widgets.Shell;

  import org.eclipse.swt.widgets.Text;

  /**

  * This application implements Straight Selection Sort algorithm, which means

  * get the minimum number from the numbers, and exchange it with the first

  * number, then doing this for other numbers except the first number. Repeat

  * this, until all numbers are in order. If you have any suggestion or problem,

  * please e-mail to happylongnv@126.com

  *

  * @author vivien Data:20100915

  */

  public class StraightSelectionSort {

  /** The string containing the number wait for sorted */

  public String numString = new String();

  public Text numText;

  public Text resText;

  public Button btSort;

  public Label errorLabel;

  /** The flag to indicate if there is any error for inputed numbers */

  public boolean hasError = false;

  /** The arrayList containing the double numbers wait for sorted */

  public ArrayList<Double> numList = new ArrayList<Double>();

  public static void main(String[] args) {

  StraightSelectionSort selectionSort = new StraightSelectionSort();

  selectionSort.createControl();

  }

  /**

  * Create the control for the interface

  */

  public void createControl() {

  Display display = new Display();

  Shell shell = new Shell(display);

  shell.setBounds(250, 250, 400, 400);

  // Set Title

  shell.setText("Straight selection sort");

  FormLayout layout = new FormLayout();

  shell.setLayout(layout);

  FormData fd = new FormData();

  // The Start Sort button

  btSort = new Button(shell, SWT.NONE | SWT.CENTER);

  btSort.setText("&Start Sort");

  fd = new FormData();

  fd.height = 28;

  fd.top = new FormAttachment(40);

  fd.left = new FormAttachment(40);

  btSort.setLayoutData(fd);

  // The Input numbers group

  Group numGroup = new Group(shell, SWT.NONE);

  numGroup.setText("Input numbers:");

  numGroup.setLayout(layout);

  fd = new FormData();

  fd.top = new FormAttachment(0, 5);

  fd.left = new FormAttachment(0, 5);

  fd.right = new FormAttachment(100, -5);

  fd.bottom = new FormAttachment(btSort, -8);

  numGroup.setLayoutData(fd);

  // Label for input numbers

  Label numLabel = new Label(numGroup, SWT.WRAP);

  numLabel

  .setText("&Please input the numbers you want to sort: (Note: Numbers need to be seperated by space)");

  fd = new FormData();

  fd.top = new FormAttachment(0, 10);

  fd.left = new FormAttachment(0, 8);

  fd.right = new FormAttachment(100, -8);

  numLabel.setLayoutData(fd);

  // Text for input numbers

  numText = new Text(numGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL

  | SWT.WRAP);

  numText.setToolTipText("Numbers need to be seperated by space");

  fd = new FormData();

  fd.top = new FormAttachment(numLabel, 10);

  fd.left = new FormAttachment(0, 8);

  fd.right = new FormAttachment(100, -8);

  fd.bottom = new FormAttachment(100, -10);

  numText.setLayoutData(fd);

 

  // The results group

  Group resGroup = new Group(shell, SWT.NONE);

  resGroup.setText("The results:");

  resGroup.setLayout(layout);

  fd = new FormData();

  fd.top = new FormAttachment(btSort, 8);

  fd.left = new FormAttachment(0, 5);

  fd.right = new FormAttachment(100, -5);

  fd.bottom = new FormAttachment(90, -5);

  resGroup.setLayoutData(fd);

  // Label for results

  Label resLabel = new Label(resGroup, SWT.WRAP);

  resLabel

  .setText("The &results after sorted are: (Note: Results are seperated by space)");

  fd = new FormData();

  fd.top = new FormAttachment(0, 10);

  fd.left = new FormAttachment(0, 8);

  fd.right = new FormAttachment(100, -8);

  resLabel.setLayoutData(fd);

  // Text for results

  resText = new Text(resGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL

  | SWT.WRAP);

  resText.setToolTipText("Results are seperated by space");

  resText.setEditable(false);

  fd = new FormData();

  fd.top = new FormAttachment(resLabel, 10);

  fd.left = new FormAttachment(0, 8);

  fd.right = new FormAttachment(100, -8);

  fd.bottom = new FormAttachment(100, -10);

  resText.setLayoutData(fd);

  // Label for showing error message

  errorLabel = new Label(shell, SWT.NONE);

  fd = new FormData();

  fd.top = new FormAttachment(90, 5);

  fd.left = new FormAttachment(0, 5);

  fd.right = new FormAttachment(100, -5);

  fd.bottom = new FormAttachment(100, -5);

  errorLabel.setLayoutData(fd);

  errorLabel.setForeground(display.getSystemColor(SWT.COLOR_RED));

  // Listen to the numText change

  numText.addModifyListener(new ModifyListener() {

  @Override

  public void modifyText(ModifyEvent e) {

  numString = numText.getText().trim();

  hasError = false;

  }

  });

  // If press Return, focus go to Start Sort button, and start sort

  numText.addKeyListener(new KeyAdapter() {

  @Override

  public void keyPressed(KeyEvent e) {

  if (e.keyCode == '\r') {

  e.doit = false;

  btSort.setFocus();

  startSort();

  }

  }

  });

  // Listen to the button selection

  btSort.addSelectionListener(new SelectionAdapter() {

  public void widgetSelected(SelectionEvent e) {

  startSort();

  }

  });

  shell.open();

  while (!shell.isDisposed()) {

  if (!display.readAndDispatch())

  display.sleep();

  }

  display.dispose();

  }

  /**

  * Get double values from string

  */

  public void getDoubleFromString() {

  int index = 0;

  // Split string using space

  String[] splitedNumbers = numString.split(" ");

  if (numList.size() != 0)

 

  // Clear the arrayList for last used

  numList.clear();

  for (int i = 0; i < splitedNumbers.length; i++) {

  if (splitedNumbers[i].trim().length() != 0) {

  try {

  numList.add(index++, Double.valueOf(splitedNumbers[i]));

  } catch (NumberFormatException e) {

  setErrorMessage("Please input the correct numbers");

  hasError = true;

  break;

  }

  }

  }

  }

  /**

  * Start sort the string containing numbers waited for sort

  */

  public void startSort() {

  if (numString != null)

  if (numString.trim().length() != 0) {

  getDoubleFromString();

  startStraightSelectionSort();

  setResults();

  } else {

  setErrorMessage("Please input numbers");

  hasError = true;

  }

  }

  /**

  * Set the results to the results group

  */

  public void setResults() {

  if (!hasError) {

  String resString = new String();

  for (int i = 0; i < numList.size(); i++)

  if (i != numList.size() - 1)

  resString = resString + numList.get(i) + " ";

  else

  // If be the last string

  resString = resString + numList.get(i);

  resText.setText(resString);

  // Clear errorLabel

  errorLabel.setText("");

  }

  }

  /**

  * Sort the numbers using Straight selection Sort algorithm

  */

  public void startStraightSelectionSort() {

  int minPosition = 0;

  for (int j = 0; j < numList.size() - 1; j++) {

  minPosition = j;

  for (int i = j + 1; i < numList.size(); i++) {

  if (numList.get(i) < numList.get(minPosition)) {

  minPosition = i;

  }

  }

  if (minPosition != j) {

  // Exchange the minimum with the first number of the numbers

  // waited for sort

  double temp = numList.get(j);

  numList.set(j, numList.get(minPosition));

  numList.set(minPosition, temp);

  }

  }

  }

  /**

  * Set the error message on the error Label

  *

  * @param errorString

  *            The string used for set on the errorLabel

  */

  public void setErrorMessage(String errorString) {

  errorLabel.setText(errorString);

  // Clear the text of results

  resText.setText("");

  hasError = true;

  }

  }

  Black-box Test Case:

  1)      All numbers are zero:

查看更多精彩图片

  2)      All numbers are integer:

  3)      All number are double:

  4)      Numbers with characters which are not digit numbers:

  5)      If no numbers are inputted by click Start Sort button (or just click return)

  6)      Numbers separated by other signs:

  7)      Numbers are too more

  8)      Drag the dialog



相关评论