-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectSort.cpp
More file actions
70 lines (58 loc) · 1.7 KB
/
selectSort.cpp
File metadata and controls
70 lines (58 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/********************************************************************
Time: 2015/11/13
Filename: selectSort
Author: dinglj
Purpose: 简单选择排序
*********************************************************************/
#include "sort.h"
/********************************************************************
Method: selectSort
Parameter:
Returns:
Purpose: 对数组进行简单选择排序
*********************************************************************/
void selectSort(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
// 从剩下的 n - 1 - i个元素中选出最小的一个
int minIndex = i;
for (int j = i + 1; j < n; ++j) // 从 i + 1开始而不是i,减少了与自身的比较
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
// 交换,将最小值放到前面
if (minIndex != i)
{
swap(arr[i], arr[minIndex]);
}
}
}
/********************************************************************
Method: selectSort
Parameter:
Returns:
Purpose: 对单链表进行简单选择排序
*********************************************************************/
void selectSort_LinkList(LinkList &L)
{
for (Node *pOrdered = L->next; NULL != pOrdered; pOrdered = pOrdered->next)
{
Node *pMinIndex = pOrdered;
// 选出最小值结点
for (Node *pUnOrdered = pOrdered->next; NULL != pUnOrdered; pUnOrdered = pUnOrdered->next)
{
if (pUnOrdered->data < pMinIndex->data)
{
pMinIndex = pUnOrdered;
}
}
if (pMinIndex != pOrdered)
{
swap(pMinIndex->data, pOrdered->data);
}
}
}