数据结构&算法实践—【排序|选择排序】选择排序
2012-05-27 22:22
417 查看
转载请注明出处:http://blog.csdn.net/wklken
回主目录
排序>>选择排序>>选择排序
List:
0.概念+伪代码+示例分析
1.选择排序实现
2.Question
0 start
基本概念:
维基百科http://zh.wikipedia.org/wiki/%E9%81%B8%E6%93%87%E6%8E%92%E5%BA%8F
伪代码:
示例:
[49, 38, 65, 97, 76, 13, 27]
Current index 6 value= 27 Max index: 3 value= 97
exchange -> [49, 38, 65, 27, 76, 13, 97]
Current index 5 value= 13 Max index: 4 value= 76
exchange -> [49, 38, 65, 27, 13, 76, 97]
Current index 4 value= 13 Max index: 2 value= 65
exchange -> [49, 38, 13, 27, 65, 76, 97]
Current index 3 value= 27 Max index: 0 value= 49
exchange -> [27, 38, 13, 49, 65, 76, 97]
Current index 2 value= 13 Max index: 1 value= 38
exchange -> [27, 13, 38, 49, 65, 76, 97]
Current index 1 value= 13 Max index: 0 value= 27
exchange -> [13, 27, 38, 49, 65, 76, 97]
Done
1 start
实现代码
2.start
A.概念,过程描述?
B.交换次数,比较次数,赋值次数?
C. 时间复杂度?空间复杂度?是否是稳定排序?
D.适用场景,何种情况下表现最优
回主目录
排序>>选择排序>>选择排序
List:
0.概念+伪代码+示例分析
1.选择排序实现
2.Question
0 start
基本概念:
维基百科http://zh.wikipedia.org/wiki/%E9%81%B8%E6%93%87%E6%8E%92%E5%BA%8F
伪代码:
function selectSort(A : list[1..n]) { index = n while (index > 1): #共有n-1次选择 { max_index = index for i from index downto 1 { #每次从剩余序列选出最大的 if(A[i] > A[max_index) { max_index = i } } swap(A[index], A[max_index ]) #将最大的换到后面 index = index -1 } }
示例:
[49, 38, 65, 97, 76, 13, 27]
Current index 6 value= 27 Max index: 3 value= 97
exchange -> [49, 38, 65, 27, 76, 13, 97]
Current index 5 value= 13 Max index: 4 value= 76
exchange -> [49, 38, 65, 27, 13, 76, 97]
Current index 4 value= 13 Max index: 2 value= 65
exchange -> [49, 38, 13, 27, 65, 76, 97]
Current index 3 value= 27 Max index: 0 value= 49
exchange -> [27, 38, 13, 49, 65, 76, 97]
Current index 2 value= 13 Max index: 1 value= 38
exchange -> [27, 13, 38, 49, 65, 76, 97]
Current index 1 value= 13 Max index: 0 value= 27
exchange -> [13, 27, 38, 49, 65, 76, 97]
Done
1 start
实现代码
def select_sort(l): index = len(l) -1 while index: max_index = index for i in range(index): if l[i] > l[max_index]: max_index = i if l[max_index] > l[index]: l[index],l[max_index] = l[max_index], l[index] index -= 1
2.start
A.概念,过程描述?
B.交换次数,比较次数,赋值次数?
C. 时间复杂度?空间复杂度?是否是稳定排序?
D.适用场景,何种情况下表现最优
相关文章推荐
- 数据结构&算法实践—【排序|交换排序】地精排序及改进
- 数据结构&算法实践—【排序|交换排序】梳子排序
- 数据结构&算法实践—【排序|选择排序】堆排序
- 数据结构&算法实践—【排序|交换排序】冒泡排序及改进
- 数据结构&算法实践—【排序|交换排序】Bogo排序
- 数据结构&算法实践—【排序|选择排序】堆排序
- 数据结构&算法实践—【排序|交换排序】奇偶排序
- 数据结构&算法实践—【排序|插入排序】插入排序
- 数据结构&算法实践—【排序|选择排序】选择排序
- (原创)选择排序与快速排序及D&C算法的延伸
- 数据结构&算法实践—【排序|交换排序】地精排序及改进
- 快速排序->快速选择算法
- 数据结构&算法实践—【排序|交换排序】梳子排序
- 数据结构&算法实践—【排序|交换排序】冒泡排序及改进
- 数据结构&算法实践—【排序|交换排序】奇偶排序
- 数据结构&算法实践—【排序|交换排序】鸡尾酒排序
- 数据结构&算法实践—【排序|交换排序】Bogo排序
- 算法分析-选择排序(直接选择排序 & 堆排序)
- 数据结构&算法实践—【排序|插入排序】插入排序
- 第十六周上机实践—项目1(4)—验证算法 冒泡排序 直接选择排序