2019-02-14 05:18:58 +08:00
# include <iostream>
using namespace std ;
//node defined
class node
{
public :
int data ;
2019-08-21 10:10:08 +08:00
node * link ;
2019-02-14 05:18:58 +08:00
node ( int d )
{
data = d ;
link = NULL ;
}
} ;
//printing the linked list
2019-08-21 10:10:08 +08:00
void print ( node * head )
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
node * current = head ;
2019-02-14 05:18:58 +08:00
while ( current ! = NULL )
{
cout < < current - > data < < " " ;
2019-08-21 10:10:08 +08:00
current = current - > link ;
2019-02-14 05:18:58 +08:00
}
cout < < endl ;
}
//creating the linked list with 'n' nodes
2019-08-21 10:10:08 +08:00
node * createlist ( int n )
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
node * head = NULL ;
node * t = NULL ;
2019-02-14 05:18:58 +08:00
for ( int i = 0 ; i < n ; i + + )
{
2019-08-21 10:10:08 +08:00
node * temp = NULL ;
2019-02-14 05:18:58 +08:00
int num ;
cin > > num ;
temp = new node ( num ) ;
if ( head = = NULL )
{
head = temp ;
t = temp ;
continue ;
}
2019-08-21 10:10:08 +08:00
if ( t - > link = = NULL )
t - > link = temp ;
2019-02-14 05:18:58 +08:00
t = temp ;
}
return head ;
}
//performing selection sort on the linked list in an iterative manner
2019-08-21 10:10:08 +08:00
void my_selection_sort_linked_list ( node * & head )
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
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
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
//pick the first node from the unsorted part and assume that it is the minimum and then start scanning from the next node
2019-02-14 05:18:58 +08:00
2019-08-21 10:10:08 +08:00
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
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
if ( current - > data < min - > data ) //if the current node is smaller than the presumed node 'min'
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
if ( temp = = NULL ) //temp stays null for the first iteration, therefore it symbolizes that we are scanning for the first time
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
if ( previous = = min ) //if the 'previous' is pointing to the 'min' node
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
//Update the pointers
head = current ; //update the head pointer with the current node
2019-02-14 05:18:58 +08:00
min - > link = current - > link ;
current - > link = previous ;
min = current ;
current = previous - > link ;
}
2019-08-21 10:10:08 +08:00
else //if the 'previous' is not pointing to the 'min' node
2019-02-14 05:18:58 +08:00
{
2019-08-21 10:10:08 +08:00
//Update the pointers
head = current ; //update the head pointer with the current node
2019-02-14 05:18:58 +08:00
previous - > link = current - > link ;
current - > link = min ;
min = current ;
current = previous - > link ;
}
}
2019-08-21 10:10:08 +08:00
else //if 'temp' is not NULL, i.e., its not the 1st iteration
2019-02-14 05:18:58 +08:00
{
temp - > link = current ;
previous - > link = current - > link ;
current - > link = min ;
min = current ;
current = previous - > link ;
}
}
2019-08-21 10:10:08 +08:00
else //if the current node is greater than min, just move the previous and the current pointer a step further
2019-02-14 05:18:58 +08:00
{
previous = previous - > link ;
current = current - > link ;
}
}
2019-08-21 10:10:08 +08:00
//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
2019-02-14 05:18:58 +08:00
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 ( )
{
2019-08-21 10:10:08 +08:00
node * head = NULL ;
2019-02-14 05:18:58 +08:00
int n ;
2019-08-21 10:10:08 +08:00
cout < < " enter the no. of nodes : " ; //taking input from user about the number of nodes in linked list
2019-02-14 05:18:58 +08:00
cin > > n ;
2019-08-21 10:10:08 +08:00
if ( n = = 0 )
return 0 ;
head = createlist ( n ) ; //creating the list
2019-02-14 05:18:58 +08:00
cout < < " original list is : " ;
2019-08-21 10:10:08 +08:00
print ( head ) ; //printing the original linked list
my_selection_sort_linked_list ( head ) ; //applying selection sort
2019-02-14 05:18:58 +08:00
cout < < " sorted list is : " ;
2019-08-21 10:10:08 +08:00
print ( head ) ; //printing the sorted linked list
2019-02-14 05:18:58 +08:00
return 0 ;
}