-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathselectionsortlinkedlist.cpp
172 lines (159 loc) · 6.14 KB
/
selectionsortlinkedlist.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
using namespace std;
// node defined
class node {
public:
int data;
node *link;
node(int d) {
data = d;
link = NULL;
}
};
// printing the linked list
void print(node *head) {
node *current = head;
while (current != NULL) {
cout << current->data << " ";
current = current->link;
}
cout << endl;
}
// creating the linked list with 'n' nodes
node *createlist(int n) {
node *head = NULL;
node *t = NULL;
for (int i = 0; i < n; i++) {
node *temp = NULL;
int num;
cin >> num;
temp = new node(num);
if (head == NULL) {
head = temp;
t = temp;
continue;
}
if (t->link == NULL)
t->link = temp;
t = temp;
}
return head;
}
// performing selection sort on the linked list in an iterative manner
void my_selection_sort_linked_list(node *&head) {
node *min = head; // throughout the algorithm 'min' is used to denote the
// node with min value out of all the nodes left for
// scanning while scanning if we find a node 'X' with
// value lesser than min, then we update the pointers in
// such a way that 'X' becomes the predecessor of 'min'
node *current =
min->link; // 'current' refers to the current node we are scanning
node *previous = min; //'previous' refers to the node that is previous to
// the current node
node *temp =
NULL; // 'temp' in this algo is used to point to the last node of the
// sorted part of the linked list.
// eg. If at any time instance the state of the linked list is
// suppose 1->2->5->3->8->NULL then, we see that "1->2" is the
// sorted part of the LL, and therefore temp will be pointing to
// the last node of the sorted part,i.e,'2' We keep on arranging
// the Linked list in such a way that after each iteration the
// node with 'min' value is placed at its correct position. Eg.
// Let suppose initially we have 5->4->1->3->2->NULL After 1st
// iteration : 1->4->5->3->2->NULL and so on
while (
min->link !=
NULL) // so that all the nodes are scanned or until there exists a node
{
// pick the first node from the unsorted part and assume that it is the
// minimum and then start scanning from the next node
while (current != NULL) // suppose you choose the min node to be X,
// then scan starts from the (X+1)th node until
// its NULL. current = (X+1)th node and min = X
{
if (current->data < min->data) // if the current node is smaller
// than the presumed node 'min'
{
if (temp == NULL) // temp stays null for the first iteration,
// therefore it symbolizes that we are
// scanning for the first time
{
if (previous ==
min) // if the 'previous' is pointing to the 'min' node
{
// Update the pointers
head = current; // update the head pointer with the
// current node
min->link = current->link;
current->link = previous;
min = current;
current = previous->link;
} else // if the 'previous' is not pointing to the 'min'
// node
{
// Update the pointers
head = current; // update the head pointer with the
// current node
previous->link = current->link;
current->link = min;
min = current;
current = previous->link;
}
} else // if 'temp' is not NULL, i.e., its not the 1st
// iteration
{
temp->link = current;
previous->link = current->link;
current->link = min;
min = current;
current = previous->link;
}
} else // if the current node is greater than min, just move the
// previous and the current pointer a step further
{
previous = previous->link;
current = current->link;
}
}
// update the pointers. Set 'temp' to the last node in the sorted part.
// Make 'min' move a step further so that 'min' points to the 1st node
// of the unsorted part start the iteration again
temp = min;
min = min->link;
previous = min;
current = min->link;
}
}
// Test cases:
// enter the no. of nodes : 5
// 8 9 3 1 4
// original list is : 8 9 3 1 4
// sorted list is : 1 3 4 8 9
// enter the no. of nodes : 3
// -1 -2 -3
// original list is : -1 -2 -3
// sorted list is : -3 -2 -1
// enter the no. of nodes : 8
// 8 7 6 5 4 3 2 1
// original list is : 8 7 6 5 4 3 2 1
// sorted list is : 1 2 3 4 5 6 7 8
// enter the no. of nodes : 6
// 5 3 4 1 -2 -4
// original list is : 5 3 4 1 -2 -4
// sorted list is : -4 -2 1 3 4 5
int main() {
node *head = NULL;
int n;
cout << "enter the no. of nodes : "; // taking input from user about the
// number of nodes in linked list
cin >> n;
if (n == 0)
return 0;
head = createlist(n); // creating the list
cout << "original list is : ";
print(head); // printing the original linked list
my_selection_sort_linked_list(head); // applying selection sort
cout << "sorted list is : ";
print(head); // printing the sorted linked list
return 0;
}