java選擇排序經典代碼 java選擇排序代碼完整

java快速排序簡單代碼

.example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px} 排序算法是《數據結構與算法》中最基本的算法之一。排序算法可以分為內部排序和外部排序,內部排序是數據記錄在內存中進行排序,而外部排序是因排序的數據很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數排序等。以下是快速排序算法:

我們提供的服務有:成都網站建設、成都做網站、微信公眾號開發、網站優化、網站認證、福山ssl等。為近1000家企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的福山網站制作公司

快速排序是由東尼·霍爾所發展的一種排序算法。在平均狀況下,排序 n 個項目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見。事實上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因為它的內部循環(inner loop)可以在大部分的架構上很有效率地被實現出來。

快速排序使用分治法(Divide and conquer)策略來把一個串行(list)分為兩個子串行(sub-lists)。

快速排序又是一種分而治之思想在排序算法上的典型應用。本質上來看,快速排序應該算是在冒泡排序基礎上的遞歸分治法。

快速排序的名字起的是簡單粗暴,因為一聽到這個名字你就知道它存在的意義,就是快,而且效率高!它是處理大數據最快的排序算法之一了。雖然 Worst Case 的時間復雜度達到了 O(n?),但是人家就是優秀,在大多數情況下都比平均時間復雜度為 O(n logn) 的排序算法表現要更好,可是這是為什么呢,我也不知道。好在我的強迫癥又犯了,查了 N 多資料終于在《算法藝術與信息學競賽》上找到了滿意的答案:

快速排序的最壞運行情況是 O(n?),比如說順序數列的快排。但它的平攤期望時間是 O(nlogn),且 O(nlogn) 記號中隱含的常數因子很小,比復雜度穩定等于 O(nlogn) 的歸并排序要小很多。所以,對絕大多數順序性較弱的隨機數列而言,快速排序總是優于歸并排序。

1. 算法步驟

從數列中挑出一個元素,稱為 "基準"(pivot);

重新排序數列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的后面(相同的數可以到任一邊)。在這個分區退出之后,該基準就處于數列的中間位置。這個稱為分區(partition)操作;

遞歸地(recursive)把小于基準值元素的子數列和大于基準值元素的子數列排序;

2. 動圖演示

代碼實現 JavaScript 實例 function quickSort ( arr , left , right ) {

var len = arr. length ,

? ? partitionIndex ,

? ? left = typeof left != 'number' ? 0 : left ,

? ? right = typeof right != 'number' ? len - 1 : right ;

if ( left

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冒泡排序法代碼

冒泡排序是比較經典的排序算法。代碼如下:

for(int i=1;iarr.length;i++){

for(int j=1;jarr.length-i;j++){

//交換位置

} ? ?

拓展資料:

原理:比較兩個相鄰的元素,將值大的元素交換至右端。

思路:依次比較相鄰的兩個數,將小數放在前面,大數放在后面。即在第一趟:首先比較第1個和第2個數,將小數放前,大數放后。然后比較第2個數和第3個數,將小數放前,大數放后,如此繼續,直至比較最后兩個數,將小數放前,大數放后。重復第一趟步驟,直至全部排序完成。

第一趟比較完成后,最后一個數一定是數組中最大的一個數,所以第二趟比較的時候最后一個數不參與比較;

第二趟比較完成后,倒數第二個數也一定是數組中第二大的數,所以第三趟比較的時候最后兩個數不參與比較;

依次類推,每一趟比較次數-1;

??

舉例說明:要排序數組:int[]?arr={6,3,8,2,9,1};?

for(int i=1;iarr.length;i++){

for(int j=1;jarr.length-i;j++){

//交換位置

} ? ?

參考資料:冒泡排序原理

直接選擇排序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選擇排序代碼及注釋

//簡單選擇排序,按自然順序

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選擇排序經典代碼 java選擇排序代碼完整
URL網址:http://m.kartarina.com/article14/hjipde.html

成都網站建設公司_創新互聯,為您提供外貿建站手機網站建設自適應網站服務器托管小程序開發網站維護

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都app開發公司
主站蜘蛛池模板: 性无码专区无码片| 亚洲日韩中文无码久久| 亚洲av永久无码精品表情包| 亚洲av无码一区二区三区天堂| 精品久久久久久无码中文字幕一区| 无码一区二区三区免费视频| 亚洲AV无码第一区二区三区| 精品久久久久久久无码久中文字幕| 国产在线观看无码免费视频 | 色情无码WWW视频无码区小黄鸭 | 亚洲中文字幕无码中文字在线| 亚洲AV无码专区电影在线观看| 蜜芽亚洲av无码一区二区三区| 亚洲午夜福利AV一区二区无码| 人妻少妇看A偷人无码精品 | 成人无码嫩草影院| 人妻丰满熟妇岳AV无码区HD| yy111111少妇影院无码| 无码乱肉视频免费大全合集| 国产成A人亚洲精V品无码性色| 激情无码人妻又粗又大| 无码区日韩特区永久免费系列 | 亚洲av无码专区亚洲av不卡| 亚洲国产精品无码久久一线| 精品无码人妻一区二区三区不卡| 无码精品不卡一区二区三区| 日韩放荡少妇无码视频| 久久精品国产亚洲AV无码娇色 | 无码人妻av一区二区三区蜜臀 | 无码夫の前で人妻を侵犯 | 亚洲男人在线无码视频| 国产精品无码AV天天爽播放器| 啊灬啊别停灬用力啊无码视频| 午夜人性色福利无码视频在线观看 | 午夜精品久久久久久久无码| 无码人妻精品一区二区三区不卡| 中日韩精品无码一区二区三区 | 国产精品无码亚洲精品2021| 丰满少妇被猛烈进入无码| 亚洲AV无码AV男人的天堂不卡| 中文字幕人成无码免费视频|