重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

选择排序算法代码java 选择排序算法代码C语言

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} 排序算法是《数据结构与算法》中最基本的算法之一。排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。常见的内部排序算法有:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。以下是快速排序算法:

创新互联公司专注于澄海企业网站建设,响应式网站开发,商城系统网站开发。澄海网站建设公司,为澄海等地区提供建站服务。全流程定制网站开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 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 这是选择排序算法吗?

这是冒泡排序。

下面是选择排序的代码:

for(int i=0;ia.length;i++){

max = i;

/**查找第 i大的数,直到记下第 i大数的位置***/

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

if(a[max]a[j])

max = j; //记下较大数位置,再次比较,直到最大

}

/***如果第 i大数的位置不在 i,则交换****/

if(i!=max){

tmp = a[i];

a[i] = a[max];

a[max] = tmp;

}

}

Java几种简单的排序源代码

给你介绍4种排序方法及源码,供参考

1.冒泡排序

主要思路: 从前往后依次交换两个相邻的元素,大的交换到后面,这样每次大的数据就到后面,每一次遍历,最大的数据到达最后面,时间复杂度是O(n^2)。

public static void bubbleSort(int[] arr){

for(int i =0; i  arr.length - 1; i++){

for(int j=0; j  arr.length-1; j++){

if(arr[j]  arr[j+1]){

arr[j] = arr[j]^arr[j+1];

arr[j+1] = arr[j]^arr[j+1];

arr[j] = arr[j]^arr[j+1];

}

}

}

}

2.选择排序

主要思路:每次遍历序列,从中选取最小的元素放到最前面,n次选择后,前面就都是最小元素的排列了,时间复杂度是O(n^2)。

public static void selectSort(int[] arr){

for(int i = 0; i arr.length -1; i++){

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

if(arr[j]  arr[i]){

arr[j] = arr[j]^arr[i];

arr[i] = arr[j]^arr[i];

arr[j] = arr[j]^arr[i];

}

}

}

}

3.插入排序

主要思路:使用了两层嵌套循环,逐个处理待排序的记录。每个记录与前面已经排好序的记录序列进行比较,并将其插入到合适的位置,时间复杂度是O(n^2)。

public static void insertionSort(int[] arr){

int j;

for(int p = 1; p  arr.length; p++){

int temp = arr[p];   //保存要插入的数据

//将无序中的数和前面有序的数据相比,将比它大的数,向后移动

for(j=p; j0  temp arr[j-1]; j--){

arr[j] = arr[j-1];

}

//正确的位置设置成保存的数据

arr[j] = temp;

}

}

4.希尔排序

主要思路:用步长分组,每个分组进行插入排序,再慢慢减小步长,当步长为1的时候完成一次插入排序,  希尔排序的时间复杂度是:O(nlogn)~O(n2),平均时间复杂度大致是O(n^1.5)

public static void shellSort(int[] arr){

int j ;

for(int gap = arr.length/2; gap  0 ; gap/=2){

for(int i = gap; i  arr.length; i++){

int temp = arr[i];

for(j = i; j=gap  temparr[j-gap]; j-=gap){

arr[j] = arr[j-gap];

}

arr[j] = temp;

}

}

}

直接选择排序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 选择排序算法代码C语言
分享路径:http://cqcxhl.cn/article/ddoijsi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP