w3resource

C Exercises: Sort a given linked list by bubble sort


20. Bubble Sort on Linked List

Write a C program to sort a given linked list by bubble sort.

Sample Solution:

C Code:

// License: https://bit.ly/2JK1psc

#include<stdio.h>
#include <stdlib.h>

// Structure for a node in a linked list
struct node {
  int data;
  struct node *next;
};

int main() {
    struct node *temp1, *temp2, *t, *newNode, *startList;
    int n, k, i, j;

    startList = NULL; // Initializing the start of the linked list to NULL

    // Asking user to input the number of elements in the linked list
    printf("Input number of elements in the linked list?");
    scanf("%d", &n);

    // Asking user to input the elements in the linked list
    printf("Input the elements in the linked list:\n");
    for (i = 1; i <= n; i++) {
        if (startList == NULL) {
            // Creating a new node if the list is empty
            newNode = (struct node *)malloc(sizeof(struct node));
            scanf("%d", &newNode->data);
            newNode->next = NULL;
            startList = newNode;
            temp1 = startList;
        } else {
            // Creating a new node if the list is not empty
            newNode = (struct node *)malloc(sizeof(struct node));
            scanf("%d", &newNode->data);
            newNode->next = NULL;
            temp1->next = newNode;
            temp1 = newNode;
        }
    }

    // Sorting the elements in ascending order using bubble sort
    for (i = n - 2; i >= 0; i--) {
        temp1 = startList;
        temp2 = temp1->next;
        for (j = 0; j <= i; j++) {
            if (temp1->data > temp2->data) {
                // Swapping data if the current node has a greater value than the next node
                k = temp1->data;
                temp1->data = temp2->data;
                temp2->data = k;
            }
            temp1 = temp2;
            temp2 = temp2->next;
        }
    }

    // Displaying the sorted linked list
    printf("Sorted order is: \n");
    t = startList;
    while (t != NULL) {
        printf("%d\t", t->data);
        t = t->next;
    }

    return 0;
}

Sample Output:

 Input number of elements in the linked list? 5
 Input the elements in the linked list: 15
33
49
6
65

Sorted order is: 
6	15	33	49	65

Flowchart :

Flowchart: Sort a given linked list by bubble sort.

For more Practice: Solve these Related Problems:

  • Write a C program to sort a linked list using bubble sort and count the number of swaps performed.
  • Write a C program to sort a linked list using bubble sort and then display the sorted list in both ascending and descending orders.
  • Write a C program to perform bubble sort on a linked list by only swapping node data rather than the pointers.
  • Write a C program to optimize bubble sort on a linked list by terminating early if no swaps are made during a pass.

Go to:


PREV : Search in Circular Linked List.
NEXT : Convert a Doubly Linked list into a string.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.