对10个数进行排序(可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。)
时间: 2024-04-04 10:34:18 浏览: 12
以下是使用选择排序算法对10个数进行排序的步骤:
1. 假设要排序的数组为 arr,长度为 n。
2. 对于 i 从 0 到 n-2 进行循环,在每次循环中执行以下步骤:
- 假设当前 i 位置上的元素为最小值,将 minIndex 设为 i。
- 对于 j 从 i+1 到 n-1 进行循环,在每次循环中执行以下步骤:
- 如果当前 j 位置上的元素小于 minIndex 位置上的元素,则将 minIndex 设为 j。
- 如果 minIndex 不等于 i,则交换 arr[i] 和 arr[minIndex] 的值。
3. 循环结束后,arr 数组中的元素就已经按照从小到大的顺序排列好了。
下面是使用选择排序算法对 [5, 2, 9, 3, 7, 6, 1, 8, 4, 10] 进行排序的具体过程:
1. i = 0,minIndex = 0,arr = [5, 2, 9, 3, 7, 6, 1, 8, 4, 10]。
- j = 1,arr[1] = 2,2 < 5,minIndex = 1。
- j = 2,arr[2] = 9,9 > 2,minIndex = 1。
- j = 3,arr[3] = 3,3 < 2,minIndex = 3。
- j = 4,arr[4] = 7,7 > 2,minIndex = 3。
- j = 5,arr[5] = 6,6 > 2,minIndex = 3。
- j = 6,arr[6] = 1,1 < 2,minIndex = 6。
- j = 7,arr[7] = 8,8 > 1,minIndex = 6。
- j = 8,arr[8] = 4,4 < 1,minIndex = 8。
- j = 9,arr[9] = 10,10 > 1,minIndex = 8。
- minIndex != i,交换 arr[0] 和 arr[6] 的值,arr = [1, 2, 9, 3, 7, 6, 5, 8, 4, 10]。
2. i = 1,minIndex = 1,arr = [1, 2, 9, 3, 7, 6, 5, 8, 4, 10]。
- j = 2,arr[2] = 9,9 > 2,minIndex = 1。
- j = 3,arr[3] = 3,3 < 2,minIndex = 3。
- j = 4,arr[4] = 7,7 > 2,minIndex = 3。
- j = 5,arr[5] = 6,6 > 2,minIndex = 3。
- j = 6,arr[6] = 5,5 < 2,minIndex = 6。
- j = 7,arr[7] = 8,8 > 2,minIndex = 6。
- j = 8,arr[8] = 4,4 < 2,minIndex = 8。
- j = 9,arr[9] = 10,10 > 2,minIndex = 8。
- minIndex != i,交换 arr[1] 和 arr[8] 的值,arr = [1, 2, 4, 3, 7, 6, 5, 8, 9, 10]。
3. i = 2,minIndex = 2,arr = [1, 2, 4, 3, 7, 6, 5, 8, 9, 10]。
- j = 3,arr[3] = 3,3 < 4,minIndex = 3。
- j = 4,arr[4] = 7,7 > 3,minIndex = 3。
- j = 5,arr[5] = 6,6 > 3,minIndex = 3。
- j = 6,arr[6] = 5,5 < 3,minIndex = 6。
- j = 7,arr[7] = 8,8 > 3,minIndex = 6。
- j = 8,arr[8] = 9,9 > 3,minIndex = 6。
- j = 9,arr[9] = 10,10 > 3,minIndex = 6。
- minIndex != i,交换 arr[2] 和 arr[6] 的值,arr = [1, 2, 3, 4, 7, 6, 5, 8, 9, 10]。
4. i = 3,minIndex = 3,arr = [1, 2, 3, 4, 7, 6, 5, 8, 9, 10]。
- j = 4,arr[4] = 7,7 > 4,minIndex = 3。
- j = 5,arr[5] = 6,6 < 4,minIndex = 5。
- j = 6,arr[6] = 5,5 < 4,minIndex = 6。
- j = 7,arr[7] = 8,8 > 4,minIndex = 6。
- j = 8,arr[8] = 9,9 > 4,minIndex = 6。
- j = 9,arr[9] = 10,10 > 4,minIndex = 6。
- minIndex != i,交换 arr[3] 和 arr[6] 的值,arr = [1, 2, 3, 5, 7, 6, 4, 8, 9, 10]。
5. i = 4,minIndex = 4,arr = [1, 2, 3, 5, 7, 6, 4, 8, 9, 10]。
- j = 5,arr[5] = 6,6 < 7,minIndex = 5。
- j = 6,arr[6] = 4,4 < 6,minIndex = 6。
- j = 7,arr[7] = 8,8 > 4,minIndex = 6。
- j = 8,arr[8] = 9,9 > 4,minIndex = 6。
- j = 9,arr[9] = 10,10 > 4,minIndex = 6。
- minIndex != i,交换 arr[4] 和 arr[6] 的值,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
6. i = 5,minIndex = 5,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
- j = 6,arr[6] = 7,7 > 4,minIndex = 5。
- j = 7,arr[7] = 8,8 > 4,minIndex = 5。
- j = 8,arr[8] = 9,9 > 4,minIndex = 5。
- j = 9,arr[9] = 10,10 > 4,minIndex = 5。
- minIndex != i,交换 arr[5] 和 arr[5] 的值,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
7. i = 6,minIndex = 6,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
- j = 7,arr[7] = 8,8 > 6,minIndex = 6。
- j = 8,arr[8] = 9,9 > 6,minIndex = 6。
- j = 9,arr[9] = 10,10 > 6,minIndex = 6。
- minIndex != i,交换 arr[6] 和 arr[6] 的值,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
8. i = 7,minIndex = 7,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
- j = 8,arr[8] = 9,9 > 8,minIndex = 7。
- j = 9,arr[9] = 10,10 > 8,minIndex = 7。
- minIndex != i,交换 arr[7] 和 arr[7] 的值,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
9. i = 8,minIndex = 8,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
- j = 9,arr[9] = 10,10 > 9,minIndex = 8。
- minIndex != i,交换 arr[8] 和 arr[8] 的值,arr = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]。
10. 排序完成,arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)