//簡單選擇排序,按自然順序
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:申請域名、虛擬主機、營銷軟件、網(wǎng)站建設(shè)、黔西南州網(wǎng)站維護、網(wǎng)站推廣。
public static void selectsort(int[] array){
int min, index, temp;
for(int i = 0; i array.length - 1; i++){ // N - 1 趟
min = i;
//查找選擇最小元素值的下標索引值
for(index = i + 1; index array.length; index++){
if(array[min] array[index])
min = index;
}
//交換
if(min != i){
temp = array[min];
array[min] = array[i];
array[i] = temp;
}
}
}
你是想要幫你完善一下,然后可以運行是吧??
代碼如下:
public?class?Test?{
public?void?sortFistname(String[]?args)?{
String?temp;
System.out.println("按首字母排序");
for?(int?i?=?0;?i??args.length?-?1;?i++)?{
int?maxIndex?=?i;
String?max?=?args[i];
for?(int?j?=?i?+?1;?j??args.length;?j++)?{
int?a?=?max.compareTo(args[j]);
if?(a??0)?{
maxIndex?=?j;
}
}
if?(maxIndex?!=?i)?{
temp?=?args[i];
args[i]?=?args[maxIndex];
args[maxIndex]?=?temp;
}
}
for(int?i=0;iargs.length;i++)?{
System.out.println(args[i]);
}
}
public?Test()?{
String[]?args?=?{"abc","afew","ebwe","gverg","nrgerg"};
sortFistname(args);
}
public?static?void?main(String[]?args)?{
new?Test();
}
}
public?class?TestBaiduKnow?{
public?static?void?main(String[]?args)?{
int[]?a?=?{?3,?5,?6,?1,?2,?8,?9?};
//?冒泡?排序后結(jié)果從小到大
for?(int?i?=?0;?i??a.length;?i++)
for?(int?j?=?i;?j??a.length;?j++)?{
if?(a[i]??a[j])?{
a[i]?=?a[i]?+?a[j];
a[j]?=?a[i]?-?a[j];
a[i]?=?a[i]?-?a[j];
}
}
print(a);
//?選擇排序?結(jié)果從大到小
int?pos?=?-1;
for?(int?i?=?0;?i??a.length;?i++)?{
int?max?=?a[i];
for?(int?j?=?i?+?1;?j??a.length;?j++)?{
if?(max??a[j])?{
pos?=?j;
max?=?a[j];
}
}
if?(pos?!=?-1)?{
a[i]?=?a[i]?+?a[pos];
a[pos]?=?a[i]?-?a[pos];
a[i]?=?a[i]?-?a[pos];
pos?=?-1;
}
}
print(a);
}
private?static?void?print(int[]?a)?{
for?(int?i?=?0;?i??a.length;?i++)
System.out.print(a[i]?+?"\t");
System.out.println();
}
}
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 ArrayListDouble numList = new ArrayListDouble();
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:
網(wǎng)站欄目:java寫選擇排序代碼 java選擇排序函數(shù)
當前網(wǎng)址:http://jinyejixie.com/article42/dosdhhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、品牌網(wǎng)站設(shè)計、企業(yè)網(wǎng)站制作、網(wǎng)頁設(shè)計公司、虛擬主機、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)