# Create a Program to Implement Sorting in Python Assignment Solution

June 25, 2024
Dr. Nicholas
🇯🇵 Japan
Python
Dr. Nicholas Scott, a distinguished Computer Science expert, holds a PhD from Tokyo University, Japan. With an impressive 8 years of experience, he has completed over 600 Python assignments, showcasing his unparalleled expertise and dedication to the field.
Key Topics
• Instructions
• Objective
• Requirements and Specifications
Tip of the day
News

## Instructions

### Objective

Write a python assignment program to implement sorting.

## Requirements and Specifications

Source Code

```#!/usr/bin/env python # coding: utf-8 # In[2]: ```

```# In[3]: class ListNode: def __init__(self, data, next=None): self.val = data self.next = next def make_list(elements): # create a linked list from the input list head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head def print_list(head): # print the values in the linked list ptr = head print('[', end="") while ptr: print(ptr.val, end=", ") ptr = ptr.next print(']') class Solution: def solve(self, node): values = [] head = node # extract the values from the linked list into a list while node: values.append(node.val) node = node.next # implement bubble sort on the list for i in range(len(values) - 1): swapped = False for j in range(len(values) - i - 1): if values[j] > values[j + 1]: values[j], values[j + 1] = values[j + 1], values[j] swapped = True if not swapped: break node = head # put the sorted values back into the linked list for val in values: node.val = val node = node.next return head ob = Solution() head = make_list(data) print_list(ob.solve(head)) ```

## Similar Samples

Explore our comprehensive collection of programming homework samples at ProgrammingHomeworkHelp.com. Our samples cover a wide range of languages such as Java, Python, C++, and more, showcasing our proficiency in solving diverse coding challenges. Each example demonstrates our commitment to clarity, accuracy, and academic excellence. Dive into our samples to see how we can assist you in achieving success in your programming assignments.