从数列中挑出一个元素,称为 "基准"(pivot);
重新排序数列 , 所有元素比基准值小的摆放在基准前面 , 所有元素比基准值大的摆在基准的后面(相同的数可以到任一边) 。在这个分区退出之后,该基准就处于数列的中间位置 。这个称为分区(partition)操作;
递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序;
2. 动图演示
代码实现JavaScript实例functionquickSort ( arr ,left ,right ){
varlen=arr. length,
partitionIndex ,
left=typeofleft!='number'?0:left ,
right=typeofright!='number'?len-1:right ;
if( left
用java编写冒泡排序和选择排序 代码???public class TestBaiduKnow {
public static void main(String[] args) {
int[] a = { 3, 5, 6, 1, 2, 8, 9 };
// 冒泡 排序后结果从小到大
for (int i = 0; ia.length; i++)
for (int j = i; ja.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; ia.length; i++) {
int max = a[i];
for (int j = i + 1; ja.length; j++) {
if (maxa[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; ia.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();
推荐阅读
- 怎么用电视直接安装电视家,如何在电视机上安装电视家
- 如何推广开发游戏,游戏推广的方法
- 全集下载地址,全集网app在哪下载
- mysql数据库修改字符集,mysql数据库改变字符编码
- java画圆代码是什么 java如何用代码画圆
- 大象小程序定制,大象的图库小程序
- 电商如何考证书,电商证书怎么考
- 手机游戏直播怎么露脸,手机游戏直播怎么露脸的
- php数据库的链接类型 php连接数据库的方法