0% found this document useful (0 votes)
16 views1 page

Random Array Sorting and Searching

The document contains a Python script that generates an array of random integers, sorts the array using a custom sorting algorithm, and allows the user to search for a specific number using binary search. It includes functions for creating the array, sorting the data, and finding an element in the sorted array. The script also prints the initial and sorted arrays and prompts the user for input to search for a number.

Uploaded by

rhwnwhh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Random Array Sorting and Searching

The document contains a Python script that generates an array of random integers, sorts the array using a custom sorting algorithm, and allows the user to search for a specific number using binary search. It includes functions for creating the array, sorting the data, and finding an element in the sorted array. The script also prints the initial and sorted arrays and prompts the user for input to search for a number.

Uploaded by

rhwnwhh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import random

array_length = 100
lower_bound = 0
upper_bound = 300

def create_array(size=array_length, minimum=lower_bound, maximum=upper_bound):


result = []
for _ in range(size):
[Link]([Link](minimum, maximum))
return result

def sort_data(data):
if len(data) <= 1:
return data
middle_element = data[len(data) // 2]
smaller = []
equal = []
larger = []
for element in data:
if element < middle_element:
[Link](element)
elif element == middle_element:
[Link](element)
else:
[Link](element)
return sort_data(smaller) + equal + sort_data(larger)

def find_element(ordered_data, target_value):


start = 0
end = len(ordered_data) - 1
while start <= end:
middle = (start + end) // 2
if ordered_data[middle] == target_value:
return middle
elif ordered_data[middle] < target_value:
start = middle + 1
else:
end = middle - 1
return -1

data_list = create_array()
print("Початковий набір даних:", data_list)

ordered_list = sort_data(data_list)
print("Відсортований набір даних:", ordered_list)

search_number = int(input("Введіть число для пошуку: "))

result_index = find_element(ordered_list, search_number)


if result_index != -1:
print("Знайдено на позиції:", result_index)
else:
print("Елемент не знайдено в наборі даних")

You might also like