forked from sanidhy-7/Data_structure_Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.c
86 lines (79 loc) · 1.5 KB
/
bubblesort.c
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
#include <stdio.h>
#include<stdlib.h>>
struct node
{
int data;
struct node *next;
};
struct node*head;
void insertdata()
{
struct node*newnode,*temp;
head=NULL;
int choice=1;
while(choice!=0)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",& newnode->data);
newnode->next=NULL;
if (head==NULL)
{
temp=head=newnode;
}
else
{
temp->next =newnode;
temp = newnode;
}
printf("Do You Want to Insert data(0,1)");
scanf("%d",&choice);
}
bubblesort();
}
void bubblesort()
{
struct node*temp,*p;
int c,swapped;
p=NULL;
if(head==NULL)
{
printf("Underflow");
return;
}
do
{
swapped=0;
temp=head;
while(temp->next != p)
{
if(temp->data > temp->next->data)
{
swapped=1;
c=temp->data;
temp->data=temp->next->data;
temp->next->data=c;
}
temp=temp->next;
}
p=temp;
}
while(swapped);
}
void print()
{
struct node * ptr=head;
while(ptr->next!= NULL)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
int main()
{
insertdata();
bubblesort();
print();
return 0;
}