-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
108 lines (94 loc) · 2.82 KB
/
bubbleSort.cpp
File metadata and controls
108 lines (94 loc) · 2.82 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/********************************************************************
Time: 2015/11/11
Filename: bubbleSort
Author: dinglj
Purpose: 冒泡排序
*********************************************************************/
#include <stdio.h>
#include "sort.h"
/********************************************************************
Method: bubbleSort
Parameter:
Returns:
Purpose: 对数组进行冒泡排序
*********************************************************************/
//////////////////////////////////////////////////////////////////////
void bubbleSort_1(int arr[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = 1; j < n - i; ++j) // 头部开始排序,尾部保存结果
{
if (arr[j - 1] > arr[j])
{
swap(arr[j - 1], arr[j]);
}
}
}
}
//////////////////////////////////////////////////////////////////////
void bubbleSort_2(int arr[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = n - 1; j > i; --j) // 尾部开始排序,头部保存结果
{
if (arr[j - 1] > arr[j])
{
swap(arr[j - 1], arr[j]);
}
}
}
}
//////////////////////////////////////////////////////////////////////
void bubbleSort_3(int arr[], int n)
{
for (int i = 0; i < n - 1; ++i)
{
for (int j = i; j < n - 1; ++j) // 头部开始排序,此时结果保存在尾部
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
swap(arr[i], arr[n - 1]); // 将尾部结果保存到头部,此时结果从大到小排序
}
}
//////////////////////////////////////////////////////////////////////
// 此外。还有尾部开始排序,结果保存到尾部
// bubbleSort_4
//////////////////////////////////////////////////////////////////////
// OperationTest
void OperationTest_1()
{
int arr[10] = {5,3,7,7,9,2,5,1,6,7};
bubbleSort_3(arr, 10);
for (int i = 0; i < 10; i++)
{
printf("%d\t", arr[i]);
}
}
/********************************************************************
Method: bubbleSort
Parameter:
Returns:
Purpose: 对单链表进行冒泡排序,从小到大
*********************************************************************/
// 实际上是头部排序,将结果保存到头部,类似于bubble_sort3
void bubbleSort_LinkList(LinkList &L)
{
for (Node *pOuterLoop = L->next; NULL != pOuterLoop; pOuterLoop = pOuterLoop->next)
{
Node *pInnerLoop = pOuterLoop;
for (; NULL != pInnerLoop->next; pInnerLoop = pInnerLoop->next)
{
if (pInnerLoop->data < pInnerLoop->next->data)
{
swap(pInnerLoop->data, pInnerLoop->next->data);
}
}
// 将尾部最小值保存到头部,此时的pInnerLoop指向最后一个节点
swap(pOuterLoop->data, pInnerLoop->data);
}
}