您的位置:首页 > 编程语言 > Java开发

Java算法排序之--选择排序

2013-02-20 10:28 197 查看
每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。


排序简介

排序算法即解决以下问题的算法:
输入:n个数的序列<a1,a2,a3,...,an>。
输出:原序列的一个重排<a1*,a2*,a3*,...,an*>;,使得a1*<=a2*<=a3*<=...<=an*。
排序算法有很多,包括插入排序冒泡排序堆排序归并排序选择排序计数排序基数排序桶排序快速排序等。插入排序堆排序选择排序归并排序快速排序冒泡排序都是比较排序,它们通过对数组中的元素进行比较来实现排序,其他排序算法则是利用非比较的其他方法来获得有关输入数组的排序信息。



基本思想

n个记录的文件的直接选择排序可经过n-1趟直接选择排序得到有序结果:
①初始状态:无序区为R[1..n],有序区为空。
②第1趟排序
在无序区R[1..n]中选出关键字最小的记录R[k],将它与无序区的第1个记录R[1]交换,使R[1..1]和R[2..n]分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。
……
③第i趟排序
第i趟排序开始时,当前有序区和无序区分别为R[1..i-1]和R(i..n)。该趟排序从当前无序区中选出关键字最小的记录 R[k],将它与无序区的第1个记录R交换,使R[1..i]和R分别变为记录个数增加1个的新有序区和记录个数减少1个的新无序区。
这样,n个记录的文件的直接选择排序可经过n-1趟直接选择排序得到有序结果。
常见的选择排序细分为简单选择排序、树形选择排序(锦标赛排序)、堆排序。上述算法仅是简单选择排序的步骤。



复杂度分析





排序算法复杂度对比

选择排序的交换操作介于 0 和 (n - 1) 次之间。选择排序的比较操作为
n (n - 1) / 2 次之间。选择排序的赋值操作介于 0 和 3 (n - 1) 次之间。
比较次数O(n^2),比较次数与关键字的初始状态无关,总的比较次数N=(n-1)+(n-2)+...+1=n*(n-1)/2。交换次数O(n),最好情况是,已经有序,交换0次;最坏情况是,逆序,交换n-1次。交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CPU时间多,n值较小时,选择排序比冒泡排序快。
其他排序算法的复杂度如右图所示。



排序过程

【示例】:
初始关键字 [49 38 65 97 76 13 27 49]
第一趟排序后 13 [38 65 97 76 49 27 49]
第二趟排序后 13 27 [65 97 76 49 38 49]
第三趟排序后 13 27 38 [97 76 49 65 49]
第四趟排序后 13 27 38 49 [76 97 65 49 ]
第五趟排序后 13 27 38 49 49 [97 65 76]
第六趟排序后 13 27 38 49 49 65 [97 76]
第七趟排序后 13 27 38 49 49 65 76 [97]
最后排序结果 13 27 38 49 49 65 76 97
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 int main(void)
6 {
7 int a[10],i,j,tmp,b;
8 srand(time(NULL));
9 for(i=0;i<10;i++)
10 a[i]=rand()%100;
11 for(i=0;i<10;i++)
12 printf("%3d",a[i]);
13 printf("\n");
14 for(i=0;i<9;i++)
15 {
16 tmp=i;
17 for(j=i+1;j<10;j++)
18 if(a[tmp]>a[j])
19 tmp=j;
20 if(i!=tmp)
21 {
22 b=a[tmp];
23 a[tmp]=a[i];
24 a[i]=b;
25 }
26 }
27 for(i=0;i<10;i++)
28 printf("%3d",a[i]);
29 printf("\n");
30 return 0;
31 }



C++语言过程

示例代码:
#include<iostream>
using namespace std;
int main()
{
int num[10] = {9,8,10,3,4,6,4,7,2,1};
cout<<"排序前:"<<endl;
for (int m = 0;m < 10;m++)
{
cout<<num[m]<<" ";
}
for (int i = 0;i < 9;i++)
{
int pos = i;
for (int j = i+1;j < 10;j++)
{
if (num[pos] > num[j])
{
pos = j;
}
}
int tem;
tem = num[pos];
num[pos] = num[i];
num[i] = tem;
}
cout<<endl<<"排序后:"<<endl;
for (int m = 0;m < 10;m++)
{
cout<<num[m]<<" ";
}
return 0;
}
选择排序法的第一层循环从起始元素开始选到倒数第二个元素,主要是在每次进入的第二层循环之前,将外层循环的下标赋值给临时变量,接下来的第二层循环中,如果发现有比这个最小位置处的元素更小的元素,则将那个更小的元素的下标赋给临时变量,最后,在二层循环退出后,如果临时变量改变,则说明,有比当前外层循环位置更小的元素,需要将这两个元素交换.



C语言实现

void select_sort(int*a,int n)
{registerint i, j, min, t;
for( i =0; i < n -1; i ++)
{
min = i;
//查找最小值
for( j = i +1; j < n; j ++)
if( a[ min]> a[ j]) min = j;
//交换
if( min != i)
{
t = a[ min];
a[ min]= a[ i];
a[ i]= t;
}
}
}



Perl语言过程

#!/usr/bin/perl
sub select_sort{
my (*array)=@_;
$length=@array;
for($i=0;$i<$length-1;$i++){
$min=$i;
for($j=$i+1;$j<$length;$j++){
if($array[$j]<$array[$min]){
$min=$j;
}
}
if($min!=$i){
$temp=$array[$i];
$array[$i]=$array[$min];
$array[$min]=$temp;
}
}
return @array;
}



php语言过程

<?php
function selection_sort($array){
$count = count($array);
for ($i=0;$i<$count-1;$i++){
/* find the minest */
$min = $i;
echo '$min-->'.$array[$min].'-->';
for ($j=$i+1;$j<$count;$j++){
//由小到大排列
if ($array[$min]>$array[$j]) {//表明当前最小的还比当前的元素大
$min = $j;//赋值新的最小的
}
}
echo $array[$min].'coco<br/>';
/* swap $array[$i] and $array[$min] 即将当前内循环的最小元素放在$i位置上*/
$temp = $array[$min];
$array[$min] = $array[$i];
$array[$i] = $temp;
}
return $array;
}
$old_array = array(3,4,1,6,5,2);
$new_array = selection_sort($old_array);
print_r($new_array);
?>



Pascal语言过程

procedure ssort(a:array of integer);{a为元素数组}
var
i,j,k,temp:integer;
begin
for i:=n downto 2 do{共n-1轮选择}
begin
k:=1;
for j:=1 to i do
if a[j]>a[k] then k:=j;{第i轮,记录前i个数中最大的}
begin {复合语句需要加入begin和end}
temp:=a[k];{把最大的与倒数第i个交换}
a[k]:=a[i];
a[i]:=temp;
end;
end;
end.



JAVA语言过程

public class Test {
public static int[] a = { 10,32,1,9,5,7,12,0,4,3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s",a);
System.out.println("");
SelectSort(Index - 1); // 选择排序
// 排序后结果
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s",a);
System.out.println("");
}
public static void SelectSort(int Index) {
int i,j,k; // 循环计数变量
int MinValue; // 最小值变量
int IndexMin; // 最小值索引变量
int Temp; // 暂存变量
for (i = 0; i < Index - 1; i++) {
MinValue = 32767; // 目前最小数值
IndexMin = 0; // 储存最小数值的索引值
for (j = i; j < Index; j++) {
if (a[j] < MinValue) // 找到最小值
{
MinValue = a[j]; // 储存最小值
IndexMin = j;
}
Temp = a; // 交换两数值
a = a;
a = Temp;
}
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.printf("%3s",a[k]);
System.out.println("");
}
}
}



visual
basic语言过程

Private Sub switch(ByRef a,ByRef b)
Dim c As Integer
c = a
a = b
b = c
End Sub
-----------------------------------------------
Private Sub Command1_Click()
Dim i,j As Integer
Dim a As Variant
a = Array(12,25,58,45,33,24) '举个例子
For i = 0 To UBound(a) - 1
For j = i + 1 To UBound(a)
If a(i) > a(j) Then
Call switch(a(i),a(j))
End If
Next j
Next i
For i = 0 To 5
Print a(i)
Next i
End Sub



C#语言过程

private int[] array;
public void input_array(int v)
{
array =new int[v];
Console.WriteLine("原数组为:");
for (int i = 0; i <array.Length; i++)
{
array[i] = Convert.ToInt16(Console.ReadLine());
}
Select_array();
}
public void Select_array()
{
int min,temp,k=0;
for (int i = 0; i < array.Length - 1; i++)
{
min = array[i];
for (int j = i+1; j < array.Length; j++)
{
if (min > array[j])
{
min = array[j];
k = j;
}
}
if(array[i]!=min)
{
temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
print_array();
}
public void print_array()
{
Console.WriteLine("数组为:");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入数组大小:");
int value = Convert.ToInt16(Console.ReadLine());
Program pro = new Program();
pro.input_array(value);
}



Quick
BASIC 语言过程

REM N 为要排序数的总数
FOR I=1 TO N-1
K=I
FOR J=I+1 TO N
IF A(K)>A(J) THEN K=J
NEXT J
IF K<>I THEN SWAP A(K),A(I)
NEXT I
REM END
c语言版本
#include <stdio.h>
//ÅÅÐ;òËã;·¨µÄʵÏÖ
//Ñ¡ÔñÅÅÐ;ò
//È«¾Ö;±äÁ¿µÄ¶;¨Òå
int Min,i,j,n=10,key;
int A[10];
//Êý;×é³õʼ»¯
void chushihua()
{
printf("³õʼ»¯A[10]Êý;×é...\n");
printf("ÇëÊäÈëA[i]µÄÔªËØ!\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
}
//ÅÅÐ;ò²Ù;×÷
void InsertionSort()
{
for(i=0;i<n-1;i++)
{
/*Min = A[i];*/
for(j=i+1;j<n;j++)
{
if(A[i]>A[j])
{
/*Min = A[j];*/
key = A[i];
A[i] = A[j];
A[j] = key;
}
}
}
}
void ShowAnswer()
{
printf("ÅÅÐ;òº;ó,AÊý;×éµÄÔªËØÅÅÁÐÈçÏÂ:\n");
printf("[");
for(i = 0;i<n;i++)
{
printf("%5d",A[i]);
}
printf("]\n");
}
void main()
{
chushihua();
InsertionSort();
ShowAnswer();
}



Python语言代码

1 #/usr/bin/env python
2
3 def select(str):
4 tmplist = list(str)
5 count = len(str)
6 for i in range(0,count -1):
7 flag = i
8 for j in range(i,count):
9 if tmplist[j] < tmplist[flag]:
10 flag = j
11 if flag != i:
12 tmplist[i],tmplist[flag] = tmplist[flag],tmplist[i]
13 return tmplist
14
15 str = "asdfffffffffsadfas123142141"
16
17 print(select(str))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: