java里的选择排序代码 java中的选择排序( 四 )


@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 = ; isplitedNumbers 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 = ; inumList 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 = ; jnumList size() ; j++) {
minPosition = j;
for (int i = j + ; inumList 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:
求java选择排序代码及注释//简单选择排序,按自然顺序
public static void selectsort(int[] array){
int min, index, temp;
for(int i = 0; iarray.length - 1; i++){// N - 1 趟
min = i;
//查找选择最小元素值java里的选择排序代码的下标索引值
for(index = i + 1; indexarray.length; index++){
if(array[min]array[index])
min = index;
}
//交换
if(min != i){
temp = array[min];
array[min] = array[i];
array[i] = temp;
}
}
}
【java里的选择排序代码 java中的选择排序】java里的选择排序代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java中的选择排序、java里的选择排序代码的信息别忘了在本站进行查找喔 。

推荐阅读