-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbubblesort.c
39 lines (39 loc) · 1.12 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
#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
int main() {
struct node *temp1,*temp2, *t,*newNode, *startList; int n,k,i,j;
startList=NULL;printf("Input number of elements in the linked list?");
scanf("%d",&n); printf("Input the elements in the linked list:\n");
for(i=1;i<=n;i++)
{if(startList==NULL)
{newNode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newNode->data);
newNode->next=NULL;startList = newNode;
temp1=startList;
}else
{ newNode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newNode->data);
newNode->next=NULL;temp1->next = newNode;
temp1=newNode;
}
}
for(i=n-2;i>=0;i--) {
temp1=startList;
temp2=temp1->next;
for(j=0;j<=i;j++){
if(temp1->data > temp2->data) {
k=temp1->data;
temp1->data=temp2->data;
temp2->data=k;
}temp1=temp2;
temp2=temp2->next;}
} printf("Sorted order is: \n");
t=startList;while(t!=NULL)
{ printf("%d\t",t->data);
t=t->next;
}
}