java

位置:IT落伍者 >> java >> 浏览文章

直接选择排序Java实现


发布日期:2022年04月19日
 
直接选择排序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 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 orgeclipseswtwinwinx_vbjar orgeclipsejface_Ijar mands_Ijar This is for Eclipse

Source Code:

package selectionsort;

import javautilArrayList;

import orgeclipseswtSWT;

import orgeclipseswteventsKeyAdapter;

import orgeclipseswteventsKeyEvent;

import orgeclipseswteventsModifyEvent;

import orgeclipseswteventsModifyListener;

import orgeclipseswteventsSelectionAdapter;

import orgeclipseswteventsSelectionEvent;

import orgeclipseswtlayoutFormAttachment;

import orgeclipseswtlayoutFormData;

import orgeclipseswtlayoutFormLayout;

import orgeclipseswtwidgetsButton;

import orgeclipseswtwidgetsDisplay;

import orgeclipseswtwidgetsGroup;

import orgeclipseswtwidgetsLabel;

import orgeclipseswtwidgetsShell;

import orgeclipseswtwidgetsText;

/**

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

selectionSortcreateControl();

}

/**

* Create the control for the interface

*/

public void createControl() {

Display display = new Display();

Shell shell = new Shell(display);

shellsetBounds( );

// Set Title

shellsetText(Straight selection sort);

FormLayout layout = new FormLayout();

shellsetLayout(layout);

FormData fd = new FormData();

// The Start Sort button

btSort = new Button(shell SWTNONE | SWTCENTER);

btSortsetText(&Start Sort);

fd = new FormData();

fdheight = ;

fdtop = new FormAttachment();

fdleft = new FormAttachment();

btSortsetLayoutData(fd);

// The Input numbers group

Group numGroup = new Group(shell SWTNONE);

numGroupsetText(Input numbers:);

numGroupsetLayout(layout);

fd = new FormData();

fdtop = new FormAttachment( );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

fdbottom = new FormAttachment(btSort );

numGroupsetLayoutData(fd);

// Label for input numbers

Label numLabel = new Label(numGroup SWTWRAP);

numLabel

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

fd = new FormData();

fdtop = new FormAttachment( );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

numLabelsetLayoutData(fd);

// Text for input numbers

numText = new Text(numGroup SWTBORDER | SWTMULTI | SWTV_SCROLL

| SWTWRAP);

numTextsetToolTipText(Numbers need to be seperated by space);

fd = new FormData();

fdtop = new FormAttachment(numLabel );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

fdbottom = new FormAttachment( );

numTextsetLayoutData(fd);

// The results group

Group resGroup = new Group(shell SWTNONE);

resGroupsetText(The results:);

resGroupsetLayout(layout);

fd = new FormData();

fdtop = new FormAttachment(btSort );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

fdbottom = new FormAttachment( );

resGroupsetLayoutData(fd);

// Label for results

Label resLabel = new Label(resGroup SWTWRAP);

resLabel

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

fd = new FormData();

fdtop = new FormAttachment( );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

resLabelsetLayoutData(fd);

// Text for results

resText = new Text(resGroup SWTBORDER | SWTMULTI | SWTV_SCROLL

| SWTWRAP);

resTextsetToolTipText(Results are seperated by space);

resTextsetEditable(false);

fd = new FormData();

fdtop = new FormAttachment(resLabel );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

fdbottom = new FormAttachment( );

resTextsetLayoutData(fd);

// Label for showing error message

errorLabel = new Label(shell SWTNONE);

fd = new FormData();

fdtop = new FormAttachment( );

fdleft = new FormAttachment( );

fdright = new FormAttachment( );

fdbottom = new FormAttachment( );

errorLabelsetLayoutData(fd);

errorLabelsetForeground(displaygetSystemColor(SWTCOLOR_RED));

// Listen to the numText change

numTextaddModifyListener(new ModifyListener() {

@Override

public void modifyText(ModifyEvent e) {

numString = numTextgetText()trim();

hasError = false;

}

});

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

numTextaddKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

if (ekeyCode == \r) {

edoit = false;

btSortsetFocus();

startSort();

}

}

});

// Listen to the button selection

btSortaddSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

startSort();

}

});

shellopen();

while (!shellisDisposed()) {

if (!displayreadAndDispatch())

displaysleep();

}

displaydispose();

}

/**

* Get double values from string

*/

public void getDoubleFromString() {

int index = ;

// Split string using space

String[] splitedNumbers = numStringsplit( );

if (numListsize() != )

// Clear the arrayList for last used

numListclear();

for (int i = ; i < splitedNumberslength; i++) {

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

try {

numListadd(index++ DoublevalueOf(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 (numStringtrim()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 < numListsize(); i++)

if (i != numListsize() )

resString = resString + numListget(i) + ;

else

// If be the last string

resString = resString + numListget(i);

resTextsetText(resString);

// Clear errorLabel

errorLabelsetText();

}

}

/**

* Sort the numbers using Straight selection Sort algorithm

*/

public void startStraightSelectionSort() {

int minPosition = ;

for (int j = ; j < numListsize() ; j++) {

minPosition = j;

for (int i = j + ; i < numListsize(); i++) {

if (numListget(i) < numListget(minPosition)) {

minPosition = i;

}

}

if (minPosition != j) {

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

// waited for sort

double temp = numListget(j);

numListset(j numListget(minPosition));

numListset(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) {

errorLabelsetText(errorString);

// Clear the text of results

resTextsetText();

hasError = true;

}

}

Blackbox Test Case:

) All numbers are zero:

&& imageheight>){if(imagewidth>=){thiswidth=;thisheight=imageheight*/imagewidth;}} border=>

) All numbers are integer:

) All number are double:

) Numbers with characters which are not digit numbers:

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

) Numbers separated by other signs:

) Numbers are too more

) Drag the dialog

               

上一篇:浅谈Java的输入输出流

下一篇:如何计算java对象占用的内存