-
Notifications
You must be signed in to change notification settings - Fork 0
/
test48_49.cpp
140 lines (125 loc) · 2.87 KB
/
test48_49.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<ctype.h>
#include<string>
#include<map>
#include<utility>
using namespace std;
struct Node {
int val;
Node* next;
Node(int n, Node* next_ = nullptr) :
val(n), next(next_) {}
};
void addNode(Node* head, int n)
{
Node* p = head;
Node* newNode = new Node{ n, nullptr };
while (p->next)
{
p = p->next;
}
newNode->val = n;
newNode->next = nullptr;
p->next = newNode;
}
Node* crateList()
{
Node* list = new Node{ -1, nullptr };
for (int i = 1; i < 10; i++)
{
addNode(list, rand() % 100 );
//addNode(list, rand() % 26 + 'a');
}
return list;
}
void printNode(Node* head)
{
Node* p = head->next;
while (p)
{
std::cout << p->val << ' ';
p = p->next;
}
std::cout << endl;
}
void selection_sort(Node* head) {
for (Node* i = head; i; i = i->next) {
Node* min_node = i;
for (Node* j = i->next; j; j = j->next) {
if (j->val < min_node->val) min_node = j;
}
swap(i->val, min_node->val);
}
}
Node * insertion_sort(Node * head) {
Node* dummy = new Node{ 0, nullptr };
while (head) {
Node* curr = head;
head = head->next;
Node* prev = dummy;
while (prev->next && ((prev->next->val) > curr->val))
prev = prev->next;
curr->next = prev->next;
prev->next = curr;
}
return dummy->next;
}
void insertion_sort1(Node* head) //ÓеãÎÊÌâ
{
Node* p = head->next,
* start = p,
* move = head,
* temp = nullptr;
while (p) {
if (p->next && p->next->val > p->val)
{
while (move->next && move->next->val > p->next->val)
move = move->next;
temp = p->next;
p->next = temp->next;
temp->next = move->next;
move->next = temp;
move = head->next;
}
else
{
p = p->next;
}
}
}
void insertionSortList(Node* head) {
if (!head || !head->next || !head->next->next) return;
Node* move = head,
* p = head->next,
*temp = nullptr;
while (p) {
if (p->next && p->next->val > p->val) {
while (move->next && move->next->val > p->next->val)
move = move->next;
temp = move->next;
move->next = p->next;
p->next = p->next->next;
move->next->next = temp;
move = head;
}
else {
p = p->next;
}
}
}
int main()
{
//Node* l1 = crateList();
//printNode(l1);
//selection_sort(l1);
//printNode(l1);
//cout << "-----------------------------------" << endl;
Node* l2 = crateList();
printNode(l2);
insertion_sort1(l2);
//insertionSortList(l2);
printNode(l2);
}