You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//performing selection sort on the linked list in an iterative manner
56
+
voidmy_selection_sort_linked_list(node* &head)
57
+
{
58
+
node* min = head; //throughout the algorithm 'min' is used to denote the node with min value out of all the nodes left for scanning
59
+
//while scanning if we find a node 'X' with value lesser than min,
60
+
//then we update the pointers in such a way that 'X' becomes the predecessor of 'min'
61
+
node* current = min->link; // 'current' refers to the current node we are scanning
62
+
node* previous = min; //'previous' refers to the node that is previous to the current node
63
+
node* temp = NULL; // 'temp' in this algo is used to point to the last node of the sorted part of the linked list.
64
+
//eg. If at any time instance the state of the linked list is suppose 1->2->5->3->8->NULL
65
+
//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'
66
+
//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.
67
+
//Eg. Let suppose initially we have 5->4->1->3->2->NULL
68
+
//After 1st iteration : 1->4->5->3->2->NULL and so on
69
+
70
+
while (min->link != NULL) //so that all the nodes are scanned or until there exists a node
71
+
{
72
+
//pick the first node from the unsorted part and assume that it is the minimum and then start scanning from the next node
73
+
74
+
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
75
+
{
76
+
if (current->data < min->data) //if the current node is smaller than the presumed node 'min'
77
+
{
78
+
if (temp == NULL) //temp stays null for the first iteration, therefore it symbolizes that we are scanning for the first time
79
+
{
80
+
if (previous == min) //if the 'previous' is pointing to the 'min' node
81
+
{
82
+
//Update the pointers
83
+
head = current; //update the head pointer with the current node
84
+
min->link = current->link;
85
+
current->link = previous;
86
+
min = current;
87
+
current = previous->link;
88
+
}
89
+
else//if the 'previous' is not pointing to the 'min' node
90
+
{
91
+
//Update the pointers
92
+
head = current; //update the head pointer with the current node
93
+
previous->link = current->link;
94
+
current->link = min;
95
+
min = current;
96
+
current = previous->link;
97
+
}
98
+
}
99
+
else//if 'temp' is not NULL, i.e., its not the 1st iteration
100
+
{
101
+
temp->link = current;
102
+
previous->link = current->link;
103
+
current->link = min;
104
+
min = current;
105
+
current = previous->link;
106
+
}
107
+
}
108
+
else//if the current node is greater than min, just move the previous and the current pointer a step further
109
+
{
110
+
previous = previous->link;
111
+
current = current->link;
112
+
}
113
+
}
114
+
115
+
//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
116
+
//start the iteration again
117
+
temp = min;
118
+
min = min->link;
119
+
previous = min;
120
+
current = min->link;
121
+
}
122
+
}
123
+
124
+
// Test cases:
125
+
126
+
// enter the no. of nodes : 5
127
+
// 8 9 3 1 4
128
+
// original list is : 8 9 3 1 4
129
+
// sorted list is : 1 3 4 8 9
130
+
131
+
// enter the no. of nodes : 3
132
+
// -1 -2 -3
133
+
// original list is : -1 -2 -3
134
+
// sorted list is : -3 -2 -1
135
+
136
+
137
+
// enter the no. of nodes : 8
138
+
// 8 7 6 5 4 3 2 1
139
+
// original list is : 8 7 6 5 4 3 2 1
140
+
// sorted list is : 1 2 3 4 5 6 7 8
141
+
142
+
// enter the no. of nodes : 6
143
+
// 5 3 4 1 -2 -4
144
+
// original list is : 5 3 4 1 -2 -4
145
+
// sorted list is : -4 -2 1 3 4 5
146
+
147
+
148
+
intmain()
149
+
{
150
+
node* head = NULL;
151
+
int n;
152
+
cout << "enter the no. of nodes : "; //taking input from user about the number of nodes in linked list
0 commit comments