Toshkent Axborot Texnalogiyalri Universiteti
Algoritmlarni Loyihalash Fani
Amaliy ish 3
Bajardi:To’lisov Shaxzod
Tekshirdi :Nazarov Azizbek
Guruhi:cal013_l2
14. Tasodifiy elementlar bilan to’ldirilgan massivda o’rniga qo’yish, pufakchali, tanlash saralash usullarini amalga oshiring.
Massiv elementlar soni: 350; 2000; 6000.
Pufakchali saralash:
from random import randrange
list=[]
n=int(input("Nechta element saralaysiz: "))
for cd in range(1,n+1):
sf=randrange(1,n,1)
list.append(sf)
print("Boshlangish massiv: ",list)
for pufak in range(len(list)-1,0,-1):
for s in range(pufak):
if list[s]>list[s+1]:
terry=list[s]
list[s]=list[s+1]
list[s+1]=terry
print("Saralangan massiv: ",list)
Bu yerda massiv tasodifiy sonlar bilan to’ldirildi. Bu to’ldirishda python tilining ‘from random import randrange’ kutibxonasi yordam berdi.Shundan so’ng massiv bublesort algoritmi yordamida saralandi.
2.Tanlash usuli:
from random import randrange
A=[]
qs=int(input("Elementlar soni: "))
for cd in range(1,qs+1):
cd=randrange(1,qs+1)
A.append(cd)
print("Massiv:",A)
print("Saralangan massiv elementlari: ")
for i in range(len(A)):
min_x=i
for j in range(i+1,len(A)):
if A[min_x]>A[j]:
min_x=j
A[i],A[min_x]=A[min_x],A[i]
for i in range(len(A)):
print(A[i],end=",")
3. O’rniga Qo’yish usuli:
from random import randint
list=[]
n=int(input("Soni : "))
for s in range(1,n+1):
d=randint(1,n+1)
list.append(d)
print("Boshlang'ich massiv: ",list)
for i in range(1, len(list)):
j = i - 1
nxt_element = list[i]
while (list[j] > nxt_element) and (j >= 0):
list[j + 1] = list[j]
j = j - 1
list[j + 1] = nxt_element
print("Saralangan massiv: ",list)
2 – topshiriq
C++ (Python, Java) tilida quyida keltirilgan amallarni bajargan holda ustuvor navbat tashkil qiling:
Berilgan N ta elementdan iborat ustuvor navbat hosil qiling;
Yangi element qo’shing;
Eng katta elementni yechib oling;
Berilgan qandaydir elementning prioritetini o’zgartiring;
Berilgan qandaydir elementni yechib oling;
Ikkita ustuvor navbatni birlashtiring.
from queue import PriorityQueue
import copy
def change_prior(item, priority, pq):
tmp = PriorityQueue()
tmp.queue = copy.deepcopy(pq.queue)
new_pq = PriorityQueue()
while not tmp.empty():
cp, cv = tmp.get()
if cv == item:
new_i = (priority, cv)
else:
new_i = (cp, cv)
new_pq.put(new_i)
return new_pq
def extract_item(item_value, pq):
tmp = PriorityQueue()
tmp.queue = copy.deepcopy(pq.queue)
while not tmp.empty():
cp, cv = tmp.get()
if cv == item_value:
return (cp, cv)
print("Item not found")
return False
def get_max(pq):
tmp = PriorityQueue()
tmp.queue = copy.deepcopy(pq.queue)
_, max_v = pq.get()
while not tmp.empty():
cp, cv = tmp.get()
if cv > max_v:
max_v = cv
return max_v
def join(pq1, pq2):
new_pq = PriorityQueue()
overall_size = pq1.qsize() + pq2.qsize()
while not pq1.empty():
new_pq.put(pq1.get())
while not pq2.empty():
new_pq.put(pq2.get())
return new_pq
cars = PriorityQueue()
cars.put((2, "Nexia"))
cars.put((3, "Damas"))
cars.put((1, "Tiko"))
cars.put((4, "Matiz"))
laptops = PriorityQueue()
laptops.put((2, "HP"))
laptops.put((1, "Dell"))
laptops.put((4, "Apple"))
laptops.put((3, "Acer"))
# Yangi element kiritish
cars.put((8, "Malibu"))
laptops.put((6, "Samsung"))
# Eng katta elementni topish
print(get_max(cars))
print(get_max(laptops))
# Ustuvorlikni o'zgartirish
cars = change_prior("Malibu", 1, cars)
# Elementni olish
print(extract_item("Apple", laptops))
# Birlashtirish
birlashma = join(cars, laptops)
Do'stlaringiz bilan baham: |