Sort linked list C code example

Example 1: Sort linked list C

void BubbledSort_linked_list(struct Node **head)
{
    Node * curr = *head;
    Node * next;
    int temp;

    while (curr && curr->next)
    {

        Node * next = curr->next;
        while (next)
        {
            if (curr->data > next->data)
            {
                std::swap(next->data, curr->data);
            }
            next = next->next;
        }
        curr = curr->next;
    }
}

Example 2: Sort linked list C

// Using array
    for(int i=0;i<ar.length;i++){
        for(int j=0;j<ar.length-1;j++){
            if(ar[j]>ar[j+1]){
                int temp = ar[j];
                ar[j]=ar[j+1];
                ar[j+1] = temp;
            }
        }
    }

// Using linkedlist
    void bubblesortlinkedlist(Node head){
        Node i= head,j=head;
        while(i!=null){
            while(j.next!=null){
                if(j.data>j.next.data){
                    int temp = j.data;
                    j.data = j.next.data;
                    j.next.data = temp;
                }
                j=j.next;
            }
            j=head;
            i=i.next;
        }
    }

Tags:

Misc Example