Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Calculate the time complexity of this program code in each line, especially in the merge sort process, the total, and how the Big O notation
Calculate the time complexity of this program code in each line, especially in the merge sort process, the total, and how the Big O notation is in the end. Give an explanation of the time complexity of each line, why it has that time complexity, and the summation flow of the time complexity until it ends up having the Big O that you got.
import sys
from PyQtQtWidgets import QApplication, QMainWindow, QComboBox, QPushButton, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget
from PyQtQtCore import Qt
class MainWindowQMainWindow:
def initself:
superinit
self.setWindowTitleMerge Sort GUI"
self.centralwidget QWidget
self.setCentralWidgetselfcentralwidget
self.layout QVBoxLayout
self.centralwidget.setLayoutselflayout
# Combo box for sorting options
self.sortcombo QComboBox
self.sortcombo.addItemsID "Name", "GPA"
self.layout.addWidgetselfsortcombo
# Combo box for grouping by angkatan
self.groupcombo QComboBox
self.groupcombo.addItemsAll Generations"stryear for year in range
self.layout.addWidgetselfgroupcombo
# Button to perform sorting
self.sortbutton QPushButtonSort
self.sortbutton.clicked.connectselfperformsort
self.layout.addWidgetselfsortbutton
# Table to display sorted data
self.table QTableWidget
self.layout.addWidgetselftable
def mergesortself arr, key:
if lenarr: #O
return arr
mid lenarr
lefthalf arr:mid
righthalf arrmid:
lefthalf self.mergesortlefthalf, key
righthalf self.mergesortrighthalf, key
return self.mergelefthalf, righthalf, key
def mergeself left, right, key:
result
leftindex, rightindex
while leftindex lenleft and rightindex lenright:
if key ID:
leftval leftleftindex
rightval rightrightindex
elif key "Name":
leftval leftleftindex
rightval rightrightindex
elif key "GPA":
rightval leftleftindex #Descending
leftval rightrightindex
if leftval rightval:
result.appendleftleftindex
leftindex
else:
result.appendrightrightindex
rightindex
while leftindex lenleft:
result.appendleftleftindex
leftindex
while rightindex lenright:
result.appendrightrightindex
rightindex
return result
def performsortself:
key self.sortcombo.currentText
groupoption self.groupcombo.currentText
if groupoption "All Generations":
filtereddata self.loadstudentdata
else:
angkatan intgroupoption
filtereddata student for student in self.loadstudentdata if intstudent: angkatan
# Sort data
sorteddata self.mergesortfiltereddata, key
# Display sorted data in table
self.displaydatasorteddata
def displaydataself data:
self.table.setRowCountlendata
self.table.setColumnCount
self.table.setHorizontalHeaderLabelsID "Name", "GPA"
for row, nim nama, ipk in enumeratedata:
nimitem QTableWidgetItemstrnim
nimitem.setTextAlignmentQtAlignCenter
self.table.setItemrow nimitem
self.table.setItemrow QTableWidgetItemnama
ipkitem QTableWidgetItemstripk
ipkitem.setTextAlignmentQtAlignCenter
self.table.setItemrow ipkitem
# Resize the columns to fit content
self.table.resizeColumnsToContents
def loadstudentdataself:
try:
with openstudentdata.txtr as file:
lines file.readlines
studentdata tuplelinestripsplit for line in lines
return studentdata
except FileNotFoundError:
printFile not found."
return
if namemain:
app QApplicationsysargv
window MainWindow
window.setGeometry
window.show
sysexitappexec
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started