Answer to Question #268900 in Python for Isabel Cortez

Question #268900

1. Data Structures and Algorithms (Project 1)


INSTRUCTIONS:


Create a program using the following features:

* Array

* Class

* Specific Sorting Algorithm

* Specific Searching Algorithm



1
Expert's answer
2021-11-19T14:38:56-0500
class Array:
   def __init__(self, array: list):
      self.array = array

   def binary(self, num):
      low = 0
      high = len(self.array) - 1
      while low <= high:
         mid = low + high
         guess = self.array[mid]
         if guess == num:
            return mid
         if guess > num:
            high = mid - 1
         else:
            low = mid + 1
      return None

   def _smallest(self):
      smallest = self.array[0]
      smallest_id = 0
      for i in range(1, len(self.array)):
         if self.array[i] < smallest:
            smallest = self.array[i]
            smallest_id = i
      return smallest_id

   def selection(self):
      temp = []
      for i in range(len(self.array)):
         smallest = self._smallest()
         temp.append(self.array.pop(smallest))
      self.array = temp


arr = Array([0, 3, 5, 6, 8, 44, 11, 4, 88, 55])
arr.selection()
print(arr.array)
print(arr.binary(55))
print(arr.binary(5))
print(arr.binary(66))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS