Skip to content

Commit 3e7cf72

Browse files
Selection Sort in Singly Linked List
1 parent df7cc5d commit 3e7cf72

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

Sorting/selectionSortLinkedList.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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;
59+
node* current = min->link;
60+
node* previous = min;
61+
node* temp = NULL;
62+
while (min->link != NULL)
63+
{
64+
while (current != NULL)
65+
{
66+
if (current->data < min->data)
67+
{
68+
if (temp == NULL)
69+
{
70+
if (previous == min)
71+
{
72+
head = current;
73+
min->link = current->link;
74+
current->link = previous;
75+
min = current;
76+
current = previous->link;
77+
}
78+
else
79+
{
80+
head = current;
81+
previous->link = current->link;
82+
current->link = min;
83+
min = current;
84+
current = previous->link;
85+
}
86+
}
87+
else
88+
{
89+
temp->link = current;
90+
previous->link = current->link;
91+
current->link = min;
92+
min = current;
93+
current = previous->link;
94+
}
95+
}
96+
else
97+
{
98+
previous = previous->link;
99+
current = current->link;
100+
}
101+
}
102+
temp = min;
103+
min = min->link;
104+
previous = min;
105+
current = min->link;
106+
}
107+
}
108+
109+
// Test cases:
110+
111+
// enter the no. of nodes : 5
112+
// 8 9 3 1 4
113+
// original list is : 8 9 3 1 4
114+
// sorted list is : 1 3 4 8 9
115+
116+
// enter the no. of nodes : 3
117+
// -1 -2 -3
118+
// original list is : -1 -2 -3
119+
// sorted list is : -3 -2 -1
120+
121+
122+
// enter the no. of nodes : 8
123+
// 8 7 6 5 4 3 2 1
124+
// original list is : 8 7 6 5 4 3 2 1
125+
// sorted list is : 1 2 3 4 5 6 7 8
126+
127+
// enter the no. of nodes : 6
128+
// 5 3 4 1 -2 -4
129+
// original list is : 5 3 4 1 -2 -4
130+
// sorted list is : -4 -2 1 3 4 5
131+
132+
133+
int main()
134+
{
135+
node* head = NULL;
136+
int n;
137+
cout << "enter the no. of nodes : "; //taking input from user about the number of nodes in linked list
138+
cin >> n;
139+
if (n == 0) return 0;
140+
head = createlist(n); //creating the list
141+
cout << "original list is : ";
142+
print(head); //printing the original linked list
143+
my_selection_sort_linked_list(head); //applying selection sort
144+
cout << "sorted list is : ";
145+
print(head); //printing the sorted linked list
146+
return 0;
147+
}

0 commit comments

Comments
 (0)