-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathBubble Sort LL.cpp
86 lines (73 loc) · 1.85 KB
/
Bubble Sort LL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Bubble Sort (Iterative) LinkedList
Send Feedback
Sort a given linked list using Bubble Sort (iteratively). While sorting, you need to swap the entire nodes, not just the data.
You don't need to print the elements, just sort the elements and return the head of updated LL.
Input format : Linked list elements (separated by space and terminated by -1)`
Sample Input 1 :
1 4 5 2 -1
Sample Output 1 :
1 2 4 5
*/
//head is the head of the linked list
// Following is the node structure
/**************
class node{
public:
int data;
node * next;
node(int data){
this->data=data;
this->next=NULL;
}
};
***************/
// int length(node *head) {
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input is handled automatically.
*/
/*
if(head == NULL)
return 0;
node *temp = head;
int size = 1+length(temp->next);
return size;
}
node* bubble_sort_LinkedList_itr(node* head)
{
//write your code here
int len=length(head);
for(int k=0; k<len; k++)
{
node* prev=NULL;
node* current=head;
while(current->next!=NULL)
{
if(current->data > current->next->data )
{
if(prev!=NULL)
{
node * temp=current->next->next;
current->next->next = current;
prev->next=current->next;
current->next = temp;
prev=prev->next;
}
else
{
head= current->next;
current->next = head->next;
head->next = current;
prev = head;
}
}
else{
prev=current;
current=current->next;
}
}
}
return head;
}