Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
sorting a linked list in data structure

Sorting a Linked list in Data structures

Posted on 12 May 202023 April 2023 By YASH PAL No Comments on Sorting a Linked list in Data structures

Sorting – linked list: there are many algorithms to sort a linked list like the bubble sort, merge sort, etc. so today we are going to sort a linked list by exchanging the values of nodes using the bubble sort algorithm.

Sorting a linked list using the bubble sort algorithm

In a bubble to sort a linked list that has n items, we need to iterate over the list n-1 times. like in the example we have a list of 5 items then we need to iterate over the list for 4 times.

to sort a linked list first we need three references p, q, and end. in the first iteration, we set the end variable to None. and in every iteration, we set the p variable to the first node of the list and the q variable to the second node of the list.

sorting a Linked list Data structures

if in the iteration the value of the linked part of node p is equal to the end then we break the iteration and continue to the next iteration.

if the info part of node p is greater than the info part of node q then we swap the values of node p and q otherwise we refer p and q to the next node of the list. so in the given example list, the value of node p is not greater than the value of the q node so we refer p and q to the next node of the list.

sorting a Linked list Data structures

here also the value of node p is not greater than the value of node q so we go to the next nodes.

sorting a Linked list Data structures

here the value of node p is greater than the value of node q so we swap them.

sorting a Linked list Data structures

next, we go to the next nodes here the value of node p is not greater than the value of node q so we don’t swap them.

sorting a Linked list Data structures

now we go to the next nodes of the list. here the value of the linked part of node p is equal to the end variable. so here the first iteration ends.

sorting a Linked list Data structures

now after iteration one ends, the last node of the list becomes sorted so we refer the end variable to the last node of the list.

sorting a Linked list Data structures

now the second iteration starts and in the second iteration, we refer the variables p and q to the first and second node of the list and swap values of nodes p and q if the values of node p are greater than the value of node q.

this condition will run till the linked part of node p does not become equal to the end variable. as you see in the images given below.

sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures

and after every iteration, we update the value of the variable end. because if we iterate the list over two times then the last two nodes of the list become sorted. so we don’t need to check them again.

sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures
sorting a Linked list Data structures

so after performing the n-1 iterations over the linked list. where n is the size of the list. now our list becomes sorted.

def bubble_sort_exdata(self):
    end = None
    while end != self.start.link:
        p = self.start
        while p.link != end:
            q = p.link
            if p.info > q.info:
                p.info, q.info = q.info, p.info
            p = p.link
        end = p
Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Reversing the Linked List
Next Post: Find and remove the loop in a linked list

Related Tutorials

vi editing commands VI Editing Commands For Linux Computer Science Tutorials
modes of vi editor VI Editor in Linux Computer Science Tutorials
environment and path setting in linux Environment and Path Setting in Linux Computer Science Tutorials
hard and synbolic links in linux Hard links and Symbolic links Computer Science Tutorials
changing file access permissions Changing File Access Permissions in Linux Computer Science Tutorials
access permissions in linux Access permissions in Linux Computer Science Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme