java写选择排序代码 java选择排序函数

求java选择排序代码及注释

//简单选择排序,按自然顺序

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名虚拟主机、营销软件、网站建设、黔西南州网站维护、网站推广。

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;

}

}

}

java选择排序

你是想要帮你完善一下,然后可以运行是吧??

代码如下:

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

}

}

用java编写冒泡排序和选择排序 代码???

public class TestBaiduKnow {

public static void main(String[] args) {

int[] a = { 3, 5, 6, 1, 2, 8, 9 };

// 冒泡 排序后结果从小到大

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

// 选择排序 结果从大到小

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

}

}

直接选择排序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 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:


当前文章:java写选择排序代码 java选择排序函数
网站地址:http://pwwzsj.com/article/dosdhhc.html