In: Computer Science
Hello,
This is an Array Search Program. Details will be provided below and please don't hesitate to contact me if you need anything.
1- Brief Introduction:
Swapping Values
Swapping consecutive values in an array:
... Before After |---| |---| arr[2] | 8 | | 1 | |---| |---| arr[3] | 1 | | 8 | |---| |---| ...
# Define an array, print the array before the swap arr = [4, 0, 8, 1, 7] print(arr) # Output: [4, 0, 8, 1, 7] # Use 2 for the example, the SWAP is probably in a loop though idx = 2 # SWAP (arr[idx], arr[idx + 1]) = (arr[idx + 1], arr[idx]) # Print the array after the swap print(arr) # Output: [4, 0, 1, 8, 7]
The first value on the right goes into the first on the left and the second value on the right goes into the second on the left.
For more details see the class notes on Tuples.
Be careful you don't subscript the array out of bounds.
Loops that access arr[idx + 1] must not let idx equal the last valid subscript.
2- Bubble sort:
We will sort least to greatest.
Bubble sort is often looked at because it is conceptually very simple.
To bubble sort do:
This will need to be done repeatedly, an element will move down in the array quickly but can only move up one array location in a single pass.
Pseudocode:
while ... while ... if the current element is > than the one that follows it swap the two
Be careful you don't subscript the array out of bounds.
3- What I mentioned above was some useful information, now here is what you need to do. Copy this program into python 3 and don't change the input values.
import random # Input data size, validate the input, exit if bad data_size = input('Please enter the number of data values: ') if data_size.isdigit(): data_size = int(data_size) else: print("Exiting, input is not a number:", data_size) exit(1) # Input upper limit of random value, validate the input, exit if bad rand_limit = input('Please enter the upper limit for random values: ') if rand_limit.isdigit(): rand_limit = int(rand_limit) else: print("Exiting, input is not a number:", rand_limit) exit(1) # Input whether to show the random values, or not show_data = input('Show the random data values, "y" if yes: ') # Create empty data array data = [] # Generate random numbers ctr = 0 while ctr < data_size: rand_num = random.randrange(0, rand_limit) data.append(rand_num) # Output each random number if the user specified to if show_data == 'y': print(rand_num) ctr = ctr + 1 ################################################################### # ==== Search loop, count occurrences of user_input ==== # ==== Count the number of "mis-sorted" values ==== # ==== Bubble Sort ==== # ==== Count the number of "mis-sorted" values ====
4- please see the instructions that will guide you for the above copied program: Add code to the program in steps:
A section on each of the above follows.
Add code to your program in the above order.
1- search:
Write code that will repeatedly:
This will require nested loops.
Run your code and make sure it performs correctly
Testing:
To test your code:
Remove any extra print statements.
2- Count the Adjacent Out of Order Elements:
Write code that compares each element of the array with the element immediately after.
Keep a count of how times an element was greater than the element immediately after.
Output the count found by the loop.
Be careful you don't subscript the array out of bounds.
Run your code and make sure it performs correctly.
To skip the "show random values" just type an
enter/return. An empty string will be input which will not
match the 'y'.
The same for the search.
DO NOT use the list count method.
3- sort: implement the bubble sort.
# Bubble Sort outline while ... while ... if the current element is > than the one that follows it swap the two
For debugging the sort put print(data) before and after your sort code.
Run your code and make sure it performs correctly.
DO NOT use the list sort built-in function or method.
Finally, the last instruction to do:
4- Count the Adjacent Out of Order Elements
Copy the code from step 2 and put it after the sort.
There should be 0 out-of-place adjacent values.
In general copying code in this manner is BAD.
When we can define functions we will be able to avoid this.
Run your code and make sure it performs correctly.
That's it! The problem is done here.
#NOTE: if you pick a value, let me know what value you picked. Please write the answer here so I can run it without typing it all over. Thank you very much.
# Input upper limit of random value, validate the input, exit if bad
rand_limit = input('Please enter the upper limit for random values: ')
if rand_limit.isdigit():
rand_limit = int(rand_limit)
else:
print("Exiting, input is not a number:", rand_limit)
exit(1)
# Input whether to show the random values, or not
show_data = input('Show the random data values, "y" if yes: ')
# Create empty data array
data = []
# Generate random numbers
ctr = 0
while ctr < data_size:
rand_num = random.randrange(0, rand_limit)
data.append(rand_num)
# Output each random number if the user specified to
if show_data == 'y':
print(rand_num)
ctr = ctr + 1
#your code starts from here
while(input("Enter q to quit or any other key to continue"))!='q':
search = input('Please enter the number to be searched: ')
if search.isdigit():
search = int(search)
else:
print("Exiting, input is not a number:", search)
exit(1)
count = 0
for number in data:
if number==search:
count+=1
print(count)
misplaced=0
for i in range(data_size-1):
if data[i]>data[i+1]:
misplaced+=1
print("misplaced = ",misplaced)
for i in range(data_size-1):
for j in range(0, data_size-i-1):
if data[j] > data[j+1]:
data[j], data[j+1] = data[j+1], data[j]
print(data)
misplaced=0
for i in range(data_size-1):
if data[i]>data[i+1]:
misplaced+=1
print("misplaced = ",misplaced)
OUTPUT :