Python Forum
Bubble Sort Algorithm Not Sorting LikedList Contents
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bubble Sort Algorithm Not Sorting LikedList Contents
#1
Hi, I am currently trying to code for this particular question which is
Uncle Tim started his Secondhand Bookstore in 2000. As years passed, the number of books in his store increased significantly. He can no longer keep track of their location in his store. He has approached you to develop a program (in Python) that could help him to keep track of the books.
My Current Codes Are :
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

    def getData(self):
        return self.data

    def setData(self, newData):
        self.data = newData

    def getNext(self):
        return self.next

    def setNext(self,newNode):
        self.next=newNode

class Book:
    def __init__(self, author, title, bookID):
        self.author = author
        self.title = title
        self.bookID = bookID
        
        
    def __str__(self):
        return 'Author: ' + self.author + ', Title: ' + \
            self.title + ', Book ID: ' + str(self.bookID);
        
    def setAuthor(self, author):
        self.author = author
        
    def getAuthor(self):
        return self.author
        
    def setTitle(self, title):
        self.book = book
        
    def getTitle(self):
        return self.title

    def setBookID(self, bookID):
        self.bookID = bookID
    
    def getBookID(self):
        return self.bookID

class LinkedList:
    def __init__(self):
        self.head = None
        self.size = 0

    def AddBookToFront(self, data):
        print("1")
        newNode = Node(data)
        newNode.setNext(self.head)
        self.head = newNode
        self.size+=1

    def DisplayBook(self):
        print("3")
        tempo = self.head
        while tempo is not None:
            print ("Author Name: " + tempo.getData().getAuthor() +" " +"Title: " + tempo.getData().getTitle()+ " "+"Book ID: "+tempo.getData().getBookID())
            tempo = tempo.getNext()
    
    def RemoveBookAtPosition(self,position):
          # If linked list is empty
          if self.head == None:
              return

          # Store head node
          temp = self.head

          # If head needs to be removed
          if position == 0:
              head = temp.next
              temp = None
              return

          # Find previous node of the node to be deleted
          for i in range(position -1 ):
              temp = temp.next
              if temp is None:
                  break

          # If position is more than number of nodes
          if temp is None:
              return
          if temp.next is None:
              return

          # Node temp.next is the node to be deleted
          # store pointer to the next of node to be deleted
          next = temp.next.next

          # Unlink the node from linked list
          temp.next = None

          temp.next = next
    def AddBookAtPosition(self, data , position):
        cursor= self.head
        temps = Node(data)
        index = 0
        prev = None
        if (position ==0):
            temps.setNext(cursor)
            self.head = temp
        else:
            while (index < position):
                prev = cursor
                cursor = cursor.getNext()
                index +=1
        prev.getNext()
        prev.setNext(temps)
    
    def bubbleSort(self):
        cursor= self.head
        one = cursor.getNext()
        two = one.getNext()
        for i in range(0,self.size):
            for j in range(i):
                if(one.getData().getAuthor() > two.getData().getAuthor()):
                    temp2 = cursor
                    one = two
                    two =temp2

ll=LinkedList()
book1 = Book("AAA","BBB","0001")
#n1=Node(book1)
book2 = Book("CCC", "BBB", "0002")
#n2=Node(book2)
book3 = Book("DDD", "CCC", "0003")
#n3=Node(book3)
book4 = Book("GGG", "FFF", "0004")
book5 = Book("EEE","LLL", "0005")

ll.AddBookToFront(book1)
ll.AddBookToFront(book2)
ll.AddBookToFront(book3)
#ll.AddBookAtPosition(book4, 2)
ll.DisplayBook()
print("After Removing")
ll.RemoveBookAtPosition(1)
ll.DisplayBook()
print("After Adding Value At Position 2")
ll.AddBookAtPosition(book4,2)
ll.DisplayBook()
print("After Adding Value At Position 3")
ll.AddBookAtPosition(book5,3)
ll.DisplayBook()
print("After Sorting")
ll.bubbleSort()
ll.DisplayBook()
The Bubble Sort Code Is Here
   def bubbleSort(self):
        cursor= self.head
        one = cursor.getNext()
        two = one.getNext()
        for i in range(0,self.size):
            for j in range(i):
                if(one.getData().getAuthor() > two.getData().getAuthor()):
                    temp2 = cursor
                    one = two
                    two =temp2
This Is My Output
Output:
1 1 1 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: CCC Title: BBB Book ID: 0002 Author Name: AAA Title: BBB Book ID: 0001 After Removing 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 After Adding Value At Position 2 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 Author Name: GGG Title: FFF Book ID: 0004 After Adding Value At Position 3 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 Author Name: GGG Title: FFF Book ID: 0004 Author Name: EEE Title: LLL Book ID: 0005 After Sorting 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 Author Name: GGG Title: FFF Book ID: 0004 Author Name: EEE Title: LLL Book ID: 0005 [Done] exited with code=0 in 0.12 seconds [Running] python "c:\Users\TP_baseline\Desktop\DSAG\ProjectTest9-2.py" 1 1 1 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: CCC Title: BBB Book ID: 0002 Author Name: AAA Title: BBB Book ID: 0001 After Removing 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 After Adding Value At Position 2 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 Author Name: GGG Title: FFF Book ID: 0004 After Adding Value At Position 3 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 Author Name: GGG Title: FFF Book ID: 0004 Author Name: EEE Title: LLL Book ID: 0005 After Sorting 3 Author Name: DDD Title: CCC Book ID: 0003 Author Name: AAA Title: BBB Book ID: 0001 Author Name: GGG Title: FFF Book ID: 0004 Author Name: EEE Title: LLL Book ID: 0005
Any Guidance Or Advice In Rectifying This Issue Would Be Greatly Appreciated
Thanks Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,249 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  how to sort a list without .sort() function letmecode 3 3,372 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,118 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 48,623 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,256 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  Class sort not sorting corretly chrissowden14 7 3,516 Dec-09-2018, 11:53 PM
Last Post: chrissowden14
  bubble sort random list atux_null 7 7,833 Nov-03-2017, 07:28 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020