TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
selectionsortlinkedlist.cpp
1#include <iostream>
2using namespace std;
3
4// node defined
5class node {
6 public:
7 int data;
8 node *link;
9 node(int d) {
10 data = d;
11 link = NULL;
12 }
13};
14
15// printing the linked list
16void print(node *head) {
17 node *current = head;
18 while (current != NULL) {
19 cout << current->data << " ";
20 current = current->link;
21 }
22 cout << endl;
23}
24
25// creating the linked list with 'n' nodes
26node *createlist(int n) {
27 node *head = NULL;
28 node *t = NULL;
29 for (int i = 0; i < n; i++) {
30 node *temp = NULL;
31 int num;
32 cin >> num;
33 temp = new node(num);
34 if (head == NULL) {
35 head = temp;
36 t = temp;
37 continue;
38 }
39 if (t->link == NULL)
40 t->link = temp;
41 t = temp;
42 }
43 return head;
44}
45
46// performing selection sort on the linked list in an iterative manner
47void my_selection_sort_linked_list(node *&head) {
48 node *min = head; // throughout the algorithm 'min' is used to denote the
49 // node with min value out of all the nodes left for
50 // scanning while scanning if we find a node 'X' with
51 // value lesser than min, then we update the pointers in
52 // such a way that 'X' becomes the predecessor of 'min'
53 node *current =
54 min->link; // 'current' refers to the current node we are scanning
55 node *previous = min; //'previous' refers to the node that is previous to
56 // the current node
57 node *temp =
58 NULL; // 'temp' in this algo is used to point to the last node of the
59 // sorted part of the linked list.
60 // eg. If at any time instance the state of the linked list is
61 // suppose 1->2->5->3->8->NULL then, we see that "1->2" is the
62 // sorted part of the LL, and therefore temp will be pointing to
63 // the last node of the sorted part,i.e,'2' We keep on arranging
64 // the Linked list in such a way that after each iteration the
65 // node with 'min' value is placed at its correct position. Eg.
66 // Let suppose initially we have 5->4->1->3->2->NULL After 1st
67 // iteration : 1->4->5->3->2->NULL and so on
68
69 while (
70 min->link !=
71 NULL) // so that all the nodes are scanned or until there exists a node
72 {
73 // pick the first node from the unsorted part and assume that it is the
74 // minimum and then start scanning from the next node
75
76 while (current != NULL) // suppose you choose the min node to be X,
77 // then scan starts from the (X+1)th node until
78 // its NULL. current = (X+1)th node and min = X
79 {
80 if (current->data < min->data) // if the current node is smaller
81 // than the presumed node 'min'
82 {
83 if (temp == NULL) // temp stays null for the first iteration,
84 // therefore it symbolizes that we are
85 // scanning for the first time
86 {
87 if (previous ==
88 min) // if the 'previous' is pointing to the 'min' node
89 {
90 // Update the pointers
91 head = current; // update the head pointer with the
92 // current node
93 min->link = current->link;
94 current->link = previous;
95 min = current;
96 current = previous->link;
97 } else // if the 'previous' is not pointing to the 'min'
98 // node
99 {
100 // Update the pointers
101 head = current; // update the head pointer with the
102 // current node
103 previous->link = current->link;
104 current->link = min;
105 min = current;
106 current = previous->link;
107 }
108 } else // if 'temp' is not NULL, i.e., its not the 1st
109 // iteration
110 {
111 temp->link = current;
112 previous->link = current->link;
113 current->link = min;
114 min = current;
115 current = previous->link;
116 }
117 } else // if the current node is greater than min, just move the
118 // previous and the current pointer a step further
119 {
120 previous = previous->link;
121 current = current->link;
122 }
123 }
124
125 // update the pointers. Set 'temp' to the last node in the sorted part.
126 // Make 'min' move a step further so that 'min' points to the 1st node
127 // of the unsorted part start the iteration again
128 temp = min;
129 min = min->link;
130 previous = min;
131 current = min->link;
132 }
133}
134
135// Test cases:
136
137// enter the no. of nodes : 5
138// 8 9 3 1 4
139// original list is : 8 9 3 1 4
140// sorted list is : 1 3 4 8 9
141
142// enter the no. of nodes : 3
143// -1 -2 -3
144// original list is : -1 -2 -3
145// sorted list is : -3 -2 -1
146
147// enter the no. of nodes : 8
148// 8 7 6 5 4 3 2 1
149// original list is : 8 7 6 5 4 3 2 1
150// sorted list is : 1 2 3 4 5 6 7 8
151
152// enter the no. of nodes : 6
153// 5 3 4 1 -2 -4
154// original list is : 5 3 4 1 -2 -4
155// sorted list is : -4 -2 1 3 4 5
156
157int main() {
158 node *head = NULL;
159 int n;
160 cout << "enter the no. of nodes : "; // taking input from user about the
161 // number of nodes in linked list
162 cin >> n;
163 if (n == 0)
164 return 0;
165 head = createlist(n); // creating the list
166 cout << "original list is : ";
167 print(head); // printing the original linked list
168 my_selection_sort_linked_list(head); // applying selection sort
169 cout << "sorted list is : ";
170 print(head); // printing the sorted linked list
171 return 0;
172}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
int main()
Main function.
int data[MAX]
test data
struct list * link
pointer to nodes
#define endl