-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.cpp
125 lines (122 loc) · 2.48 KB
/
bubblesort.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
using namespace std;
class node{
public:
node *next;
node *prev;
int data;
node(int x, node *p, node *n)
{
next = n;
prev = p;
data = x;
}
};
class list{
private:
node *head;
node *tail;
public:
list()
{
head = NULL;
tail = NULL;
}
void addtoend(int x)
{
if(head == NULL)
{
head = new node (x,NULL,NULL);
tail = head;
}
else if(tail->prev == NULL)
{
tail = new node(x,head,NULL);
head->next = tail;
}
else
{
node *temp = tail;
tail = new node(x,tail,NULL);
temp->next = tail;
}
}
void bubblesort()
{
bool is_sorted = false;
node *temp = head;
while (!is_sorted)
{
is_sorted = true;
temp = head;
while (temp->next != NULL)
{
if(temp->next->data < temp->data)
{
is_sorted = false;
if(temp == head)
{
//temp->prev->next = temp->next; // 1. (impossible)
temp->next->prev = NULL; // 2.
head = temp->next;
}
else
{
temp->prev->next = temp->next; // 1.
temp->next->prev = temp->prev; // 2.
}
temp->prev = temp->next; // 3.
if(temp->next == tail)
{
temp->next = NULL; // 4.
tail = temp;
//temp->next->prev = temp2; // 5. (impossible)
}
else
{
temp->next = temp->next->next; // 4.
temp->next->prev = temp; // 5.
}
temp->prev->next = temp; // 6.
temp = temp->prev;
}
temp = temp->next;
}
}
display();
display0();
}
void display()
{
node *temp = head;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void display0()
{
node *temp = tail;
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->prev;
}
cout<<endl;
}
};
int main()
{
list l1;
int x[10] = {17,15,8,12,10,5,4,1,7,2};
for(int i=0;i<10;i++)
{
l1.addtoend(x[i]);
}
l1.display();
l1.bubblesort();
l1.display();
return 0;
}