知识大全 直接选择排序Java实现

Posted 文字

篇首语:自由的生活方式是借知识和洞察获得的。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 直接选择排序Java实现相关的知识,希望对你有一定的参考价值。

直接选择排序Java实现  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  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 numbers remained Continue to find the minimum number in the remained N 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 eclipse swt win win x _ v b jar eclipse jface_ I jar mands_ I jar This is for Eclipse

  Source Code:

  package selection sort;

  import java util ArrayList;

  import eclipse swt SWT;

  import eclipse swt events KeyAdapter;

  import eclipse swt events KeyEvent;

  import eclipse swt events ModifyEvent;

  import eclipse swt events ModifyListener;

  import eclipse swt events SelectionAdapter;

  import eclipse swt events SelectionEvent;

  import eclipse swt layout FormAttachment;

  import eclipse swt layout FormData;

  import eclipse swt layout FormLayout;

  import eclipse swt widgets Button;

  import eclipse swt widgets Display;

  import eclipse swt widgets Group;

  import eclipse swt widgets Label;

  import eclipse swt widgets Shell;

  import 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

  *

  * @author vivien Data:

  */

  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( );

  // 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 = ;

  fd top = new FormAttachment( );

  fd left = new FormAttachment( );

  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( );

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

  fd bottom = new FormAttachment(btSort );

  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( );

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

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

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

  fd bottom = new FormAttachment( );

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

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

  fd bottom = new FormAttachment( );

  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( );

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

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

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

  fd bottom = new FormAttachment( );

  resText setLayoutData(fd);

  // Label for showing error message

  errorLabel = new Label(shell SWT NONE);

  fd = new FormData();

  fd top = new FormAttachment( );

  fd left = new FormAttachment( );

  fd right = new FormAttachment( );

  fd bottom = new FormAttachment( );

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

  // Split string using space

  String[] splitedNumbers = numString split( );

  if (numList size() != )

  // Clear the arrayList for last used

  numList clear();

  for (int i = ; i < splitedNumbers length; i++)

  if (splitedNumbers[i] trim() length() != )

  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() != )

  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 = ; i < numList size(); i++)

  if (i != numList size() )

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

  for (int j = ; j < numList size() ; j++)

  minPosition = j;

  for (int i = j + ; 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:

   )      All numbers are zero:

相关参考

知识大全 交换排序之直接选择排序

  选择排序(SelectionSort)的基本思想是每一趟从待排序的记录中选出关键字最小的记录顺序放在已排好序的子文件的最后直到全部记录排序完毕 直接选择排序  直接选择排序(Straig

知识大全 排序 - 各种内部排序方法的比较和选择(一)

  按平均时间将排序分为四类  ()平方阶(O(n))排序  一般称为简单排序例如直接插入直接选择和冒泡排序;  ()线性对数阶(O(nlgn))排序  如快速堆和归并排序;  ()O(n+£)阶排序

知识大全 数据结构第七章(图)串讲+复习要点

  排序是组织数据最基本的运算排序的方法也很多本章给出了几种典型的排序方法见下表  排序类别插入排序交换排序选择排序归并排序分配排序  排序方法直接插入冒泡法直接选择*归并排序箱排序  希尔排序*快速

知识大全 排序 - 分配排序 - 箱排序 (一)

  按平均时间将排序分为四类  ()平方阶(O(n))排序  一般称为简单排序例如直接插入直接选择和冒泡排序;  ()线性对数阶(O(nlgn))排序  如快速堆和归并排序;  ()O(n+£)阶排序

知识大全 排序算法的各趟排序算法

  以关键字序列()为例分别写出执行以下排序算法的各趟排序结束时关键字序列的状态  ()直接插入排序()希尔排序()冒泡排序()快速排序  ()直接选择排序()堆排序()归并排序()基数排序  上述方

知识大全 排序算法的各趟排序算法

  以关键字序列()为例分别写出执行以下排序算法的各趟排序结束时关键字序列的状态  ()直接插入排序()希尔排序()冒泡排序()快速排序  ()直接选择排序()堆排序()归并排序()基数排序  上述方

知识大全 第8章排序(基础知识)习题练习

以关键字序列()为例分别写出执行以下排序算法的各趟排序结束时关键字序列的状态 ()直接插入排序()希尔排序()冒泡排序()快速排序 ()直接选择排序()堆排序()归并排序()基数排序  上述方法中哪些

知识大全 第8章排序(基础知识)习题练习答案

以关键字序列()为例分别写出执行以下排序算法的各趟排序结束时关键字序列的状态 ()直接插入排序()希尔排序()冒泡排序()快速排序 ()直接选择排序()堆排序()归并排序()基数排序  上述方法中哪些

知识大全 常见排序算法的java实现

  最近在面试遇到很多排序算法问题总结一下  定义数组如下  [java]  int[]array=newint[];  int[]array=newint[];  首先是插入排序  [java]  

知识大全 java的各种排序算法

  Java代码  插入排序:    packagerututilalgorithmsupport;  importrututilalgorithmSortUtil;  publicclassInse