from
PyQt5.QtWidgets
import
*
from
PyQt5
import
QtCore, QtGui
from
PyQt5.QtGui
import
*
from
PyQt5.QtCore
import
*
import
sys
class
Window(QMainWindow):
number
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
]
def
__init__(
self
):
super
().__init__()
self
.setWindowTitle("Jump search ")
self
.setGeometry(
100
,
100
,
600
,
400
)
self
.UiComponents()
self
.show()
def
UiComponents(
self
):
self
.start
=
False
self
.linear
=
False
self
.jump
=
True
self
.label_list
=
[]
self
.desired
=
11
self
.step
=
4
self
.counter
=
0
self
.index
=
0
self
.linear_index
=
0
c
=
0
for
i
in
self
.number:
label
=
QLabel(
str
(i),
self
)
label.setStyleSheet("border :
1px
solid black; background : white;")
label.setAlignment(Qt.AlignTop)
label.setGeometry(
50
+
c
*
30
,
50
,
20
, i
*
10
+
10
)
self
.label_list.append(label)
c
=
c
+
1
self
.search_button
=
QPushButton("Start Search",
self
)
self
.search_button.setGeometry(
100
,
270
,
100
,
30
)
self
.search_button.clicked.connect(
self
.search_action)
pause_button
=
QPushButton("Pause",
self
)
pause_button.setGeometry(
100
,
320
,
100
,
30
)
pause_button.clicked.connect(
self
.pause_action)
self
.result
=
QLabel("To search : "
+
str
(
self
.desired),
self
)
self
.result.setGeometry(
350
,
280
,
200
,
40
)
self
.result.setStyleSheet("border :
3px
solid black;")
self
.result.setFont(QFont(
'Times'
,
10
))
self
.result.setAlignment(Qt.AlignCenter)
timer
=
QTimer(
self
)
timer.timeout.connect(
self
.showTime)
timer.start(
500
)
def
showTime(
self
):
if
self
.start:
if
self
.jump:
if
self
.counter
*
self
.step >
=
len
(
self
.number):
self
.linear_index
=
self
.index
-
self
.step
self
.counter
=
0
self
.jump
=
False
self
.linear
=
True
self
.index
=
self
.step
*
self
.counter
if
self
.number[
self
.index]
=
=
self
.desired:
self
.start
=
False
self
.jump
=
False
self
.result.setText("Found at index : "
+
str
(
self
.index))
self
.label_list[
self
.index].setStyleSheet("border :
2px
solid green;"
"background
-
color : lightgreen;")
else
:
self
.label_list[
self
.index].setStyleSheet("border :
1px
solid black;"
"background
-
color : grey;")
if
self
.number[
self
.index] >
self
.desired:
self
.linear_index
=
self
.index
-
self
.step
self
.counter
=
0
self
.linear
=
True
self
.jump
=
False
self
.counter
+
=
1
self
.index
=
self
.step
*
self
.counter
if
self
.counter
*
self
.step >
=
len
(
self
.number):
self
.linear_index
=
self
.index
-
self
.step
self
.counter
=
0
self
.jump
=
False
self
.linear
=
True
if
self
.linear:
if
self
.counter
=
=
self
.step:
self
.start
=
False
self
.result.setText("Not found")
if
self
.linear_index <
0
:
self
.linear_index
=
0
if
self
.number[
self
.counter
+
self
.linear_index]
=
=
self
.desired:
self
.linear
=
False
self
.start
=
False
self
.result.setText("Found at index : "
+
str
(
self
.counter
+
self
.linear_index))
self
.label_list[
self
.counter
+
self
.linear_index].setStyleSheet(
"border :
2px
solid green;"
"background
-
color : lightgreen;")
else
:
self
.label_list[
self
.counter
+
self
.linear_index].setStyleSheet(
"border :
1px
solid black;"
" background
-
color : grey;")
self
.counter
+
=
1
def
search_action(
self
):
self
.start
=
True
self
.result.setText("Started searching...")
def
pause_action(
self
):
self
.start
=
False
self
.result.setText("Paused")
App
=
QApplication(sys.argv)
window
=
Window()
sys.exit(App.
exec
())