sort linked list c

Solutions on MaxInterview for sort linked list c by the best coders in the world

showing results for - "sort linked list c"
Chiara
15 May 2020
1void BubbledSort_linked_list(struct Node **head)
2{
3    Node * curr = *head;
4    Node * next;
5    int temp;
6
7    while (curr && curr->next)
8    {
9
10        Node * next = curr->next;
11        while (next)
12        {
13            if (curr->data > next->data)
14            {
15                std::swap(next->data, curr->data);
16            }
17            next = next->next;
18        }
19        curr = curr->next;
20    }
21}
22
Alberto
24 Feb 2017
1// Using array
2    for(int i=0;i<ar.length;i++){
3        for(int j=0;j<ar.length-1;j++){
4            if(ar[j]>ar[j+1]){
5                int temp = ar[j];
6                ar[j]=ar[j+1];
7                ar[j+1] = temp;
8            }
9        }
10    }
11
12// Using linkedlist
13    void bubblesortlinkedlist(Node head){
14        Node i= head,j=head;
15        while(i!=null){
16            while(j.next!=null){
17                if(j.data>j.next.data){
18                    int temp = j.data;
19                    j.data = j.next.data;
20                    j.next.data = temp;
21                }
22                j=j.next;
23            }
24            j=head;
25            i=i.next;
26        }
27    }
28