每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。
更多信息请参考:http://baike.baidu.com/view/547263.htm
C语言代码:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <time.h>
void init(int *array, int count)
{
int n = 0;
srand((unsigned int)time(NULL));
for (n=0; n<count; n++)
{
array[n] = rand()%100 + 1;
}
}
void output(int *array, int count)
{
int n = 0;
for (n=0; n<count; n++)
{
printf("%5d", array[n]);
}
printf("\n");
}
/* 选择排序 */
void selectsort(int* array, int count, int type)
{
int i = 0;
int j = 0;
int index = 0;
int temp = 0;
if (type == 0)
{
//正排序,从小排到大
for (i = 0; i < count; i++)
{
index = i;//这里不是index=0
for (j = i+1; j<count; j++)
{
if (array[index]>array[j])
{
index = j;
}
}
if(index!=i)
{
temp=array[i];
array[i]=array[index];
array[index]=temp;
}
}
}
else
{
//倒排序,从大排到小
for (i = 0; i<count; i++)
{
index = i;//这里不是index=0
for (j =i+1;j<count; j++)
{
if (array[index]<array[j])
{
index = j;
}
}
//交换在位置i和index(最大值)两个数
if(index!=i)
{
temp=array[i];
array[i]=array[index];
array[index]=temp;
}
}
}
}
int main()
{
const int count = 10;
int array[count];
memset(array, 0, sizeof(int)*count);
init(array, count);
printf("data before sort: ");
output(array, count);
selectsort(array, count, 0); // 正排序,从小排到大
printf("data after sort(asc): ");
output(array, count);
selectsort(array, count, 1); // 倒排序,从大排到小
printf("data after sort(desc): ");
output(array, count);
return 0;
}
运行结果如下:
data before sort: 96 75 98 99 95 21 76 22 68 57
data after sort(asc): 21 22 57 68 75 76 95 96 98 99
data after sort(desc): 99 98 96 95 76 75 68 57 22 21
Java代码:
import java.util.Random;
/* 直接选择排序法----选择排序的一种
* 方法:每一趟从待排序的数据元素中选出最小(或最大)的一个元素,
* 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
*/
public class Sort
{
/*
* 输出数组中的数据
*/
public static void OutputArray(int[] array)
{
for (int data : array)
{
System.out.print(data + "\t");
}
System.out.println();
}
/*
* 生成需要排序的数组
*/
public static int[] createArray(int count)
{
int array[] = new int[count];
Random r = new Random();
for (int i = 0; i < count; i++)
{
array[i] = r.nextInt(100);
}
System.out.println("");
System.out.print("data before sort:\t");
OutputArray(array);
return array;
}
/*
* 选择排序
*/
public static void selectSort(int[] array, String sortType)
{
int temp,index;
if (sortType.equals("asc"))
{
//正排序,从小排到大
for (int i = 0; i < array.length; i++)
{
index = i;//这里不是index=0
for (int j = i+1; j <array.length; j++)
{
if (array[index]>array[j])
{
index = j;
}
}
if(index!=i)
{
temp=array[i];
array[i]=array[index];
array[index]=temp;
}
}
}
else if (sortType.equals("desc"))
{
//倒排序,从大排到小
for (int i = 0; i <array.length; i++)
{
index = i;//这里不是index=0
for (int j =i+1;j <array.length; j++)
{
if (array[index]<array[j])
{
index = j;
}
}
//交换在位置i和index(最大值)两个数
if(index!=i)
{
temp=array[i];
array[i]=array[index];
array[index]=temp;
}
}
}
else
{
System.out.println("您输入的排序类型错误!");
}
}
public static void main(String[] args)
{
int[] arr1=createArray(10);
System.out.print("data after sort(asc):\t");
selectSort(arr1, "asc");
OutputArray(arr1);
System.out.print("data after sort(desc):\t");
selectSort(arr1, "desc");
OutputArray(arr1);
}
}
输出结果如下:
data before sort: 83 49 67 32 41 4 9 15 24 21
data after sort(asc): 4 9 15 21 24 32 41 49 67 83
data after sort(desc): 83 67 49 41 32 24 21 15 9 4