182k views
3 votes
Write a program generates an array of size 1001 and populates it with random real numbers with values between 1000 and 1500. It then makes use of the functions in the statistics library to find the mean, maximum, minimum, standard deviation, variance, and median. Before finding the median, make sure to use the selection sort function to order the array. Finally, the program makes use of the linear and binary search functions to search for the median value. Because the array has 1001 values, both search functions should indicate that the index of the median values is 500 (the middle value in the array of size 1001

User Huzeyfe
by
5.0k points

1 Answer

1 vote

Answer:

see explaination

Step-by-step explanation:

import random

import statistics

import sys

arr=[];

#initializing array of size 1001 with random integers from 1000 to 1500

for i in range(1001):

arr=arr+[random.randrange(1000,1500)]

#printing mean,max,min,standard dev,variance using statistics library

#please giv a like

print("Mean is : ", statistics.mean(arr))

print("max is : ",max(arr))

print("min is : ",min(arr))

print("Standard Deviation is % s " %(statistics.stdev(arr)));

print("Variance of the is % s " %(statistics.variance(arr)))

#Sorting the array using selection sort

for i in range(len(arr)):

min_idx = i

for j in range(i+1, len(arr)):

if arr[min_idx] > arr[j]:

min_idx = j

arr[i], arr[min_idx] = arr[min_idx], arr[i]

#finding median using statistics

medianvalue=statistics.median(arr)

print("Median is : % s "% (medianvalue))

#Finding the index of median using linear search

print("The median value is found at using linear search is : ")

for i in range(len(arr)):

if arr[i] == medianvalue:

print(i)

#Finding the index of median using binary search

def binarySearch (arr, l, r, x):

# Check base case

if r >= l:

mid = l + (r - l)//2

# If element is present at the middle itself

if arr[mid] == x:

return mid

# If element is smaller than mid, then it can only

# be present in left subarray

elif arr[mid] > x:

return binarySearch(arr, l, mid-1, x)

# Else the element can only be present in right subarray

else:

return binarySearch(arr, mid+1, r, x)

else:

# Element is not present in the array

return -1

res=binarySearch(arr, 0, len(arr)-1,medianvalue )

print("The median value is found at using binary search is : ",res)

see attac

Write a program generates an array of size 1001 and populates it with random real-example-1
Write a program generates an array of size 1001 and populates it with random real-example-2
User Mozammil
by
5.1k points