Skip to content

Commit 8f468a9

Browse files
authored
Merge pull request #180 from Deepak25101997/master
Selection Sort in Singly Linked List
2 parents 00437ca + 3543096 commit 8f468a9

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
5+
//node defined
6+
class node
7+
{
8+
public:
9+
int data;
10+
node* link;
11+
node(int d)
12+
{
13+
data = d;
14+
link = NULL;
15+
}
16+
17+
};
18+
19+
//printing the linked list
20+
void print(node* head)
21+
{
22+
node* current = head;
23+
while (current != NULL)
24+
{
25+
cout << current->data << " ";
26+
current = current-> link;
27+
}
28+
cout << endl;
29+
}
30+
31+
//creating the linked list with 'n' nodes
32+
node* createlist(int n)
33+
{
34+
node* head = NULL;
35+
node* t = NULL;
36+
for (int i = 0; i < n; i++)
37+
{
38+
node* temp = NULL;
39+
int num;
40+
cin >> num;
41+
temp = new node(num);
42+
if (head == NULL)
43+
{
44+
head = temp;
45+
t = temp;
46+
continue;
47+
}
48+
if (t->link == NULL) t->link = temp;
49+
t = temp;
50+
}
51+
return head;
52+
}
53+
54+
55+
//performing selection sort on the linked list in an iterative manner
56+
void my_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+
int main()
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
153+
cin >> n;
154+
if (n == 0) return 0;
155+
head = createlist(n); //creating the list
156+
cout << "original list is : ";
157+
print(head); //printing the original linked list
158+
my_selection_sort_linked_list(head); //applying selection sort
159+
cout << "sorted list is : ";
160+
print(head); //printing the sorted linked list
161+
return 0;
162+
}

0 commit comments

Comments
 (0)