选择排序(selection sort)算法实现
来源:互联网 发布:javascript 抢购 编辑:程序博客网 时间:2024/04/26 10:43
从字面上理解,就是通过不断地选择数组元素,从而达到排序的目的。我插入排序类似,假设第i(i<n)个数组元素data[0]是最大的(从大到小排序),然后依次扫描i + 1 到 n - 1的元素,找到比第i个元素大的元素。最后将它们交换。
算法的时间复杂度为O(n^2)。
算法的实现如下
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购
算法的时间复杂度为O(n^2)。
算法的实现如下
#include <stdio.h>
void output_array(int data[], int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ", data[i]);
printf("/n");
}
void swap(int *a, int *b)
{
int x;
x = *a;
*a = *b;
*b = x;
}
//算法实现
void selection(int data[], int b, int e)
{
int i, j, high_index;
for(i = b; i < e; i++)
{
high_index = i;
for(j = e; j > i; j--)
if(data[j] > data[high_index])
high_index = j;
swap(&data[i], &data[high_index]);
}
}
int main()
{
int data[] = {5, 3, 1, 665, 77, 66, 44, 11, 10, 9, 8, 6};
output_array(data, 12);
selection(data, 0, 11);
output_array(data, 12);
return 0;
}
void output_array(int data[], int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ", data[i]);
printf("/n");
}
void swap(int *a, int *b)
{
int x;
x = *a;
*a = *b;
*b = x;
}
//算法实现
void selection(int data[], int b, int e)
{
int i, j, high_index;
for(i = b; i < e; i++)
{
high_index = i;
for(j = e; j > i; j--)
if(data[j] > data[high_index])
high_index = j;
swap(&data[i], &data[high_index]);
}
}
int main()
{
int data[] = {5, 3, 1, 665, 77, 66, 44, 11, 10, 9, 8, 6};
output_array(data, 12);
selection(data, 0, 11);
output_array(data, 12);
return 0;
}
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购
- 选择排序(selection sort)算法实现
- 选择排序(selection sort)算法实现
- 算法--选择排序 Selection Sort
- 简单选择排序算法(Simple Selection Sort) Java实现
- 经典排序算法 - 选择排序Selection sort
- 经典排序算法 - 选择排序Selection sort
- 经典排序算法 - 选择排序Selection sort
- 排序算法-选择排序 Selection Sort
- 经典排序算法 - 选择排序Selection sort
- 排序算法---选择排序(Selection Sort)
- 选择排序算法——Selection Sort
- 选择排序算法(Selection Sort)
- 选择排序的php实现 Selection Sort
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- selection sort 选择排序
- 选择排序 Selection Sort
- 选择排序(Selection Sort)
- 使用Transact-SQL进行数据导入导出方法详解
- Struts2教程10:国际化
- 将网页变成灰色(只限IE)
- 通过MVC模式将Web视图和逻辑代码分离
- 一个智能机器人的语录
- 选择排序(selection sort)算法实现
- 希尔排序(shellsort)算法实现
- 在Firefox中通过AJAX跨域访问Web资源
- 归并排序(merge sort)算法实现
- 快速排序(quicksort)算法实现
- Java 6 Hotspot的性能将有可能超越编译型语言
- SQL Server各种导入导出数据方式的比较
- 【转载】如何从课堂与教学内容两方面提高培训效率
- 【转载】李开复宣布与金山合作 谷歌迈出中国第三步