Week 3
Search
Search in the array could conduct in different methods and algorithms. As far as you learn principal about the algorithms, you can develop it in any most programming languages.
Linear search
Linear search is one of the easiest algorithms; principal behind the linear search checks all the elements of the array. It starts with the first element if it matches the target will be returned if not next element of the array will be checked until the end of the array.
Example:
In the following list, we want to search the element that has value ‘15’, based on linear search.
Pseudo code
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
Python Code
List = [1, 2, 3, 15, 0, 125, 321, 9]
for i in range (len(List)):
if List [i] == 15:
print i
The number 15 is the third element of the array ‘List’ Len (List) returns the length of the array ‘List’ which here is 7 ‘0…7’ In for loop check every single element if the element matches the target will print the index of the element in the array. The output of this code will be ‘3’.
Tutorial:
Based on the linear algorithm, whenever the target has been matched search should stop, while in the above algorithm when ‘15’ has found in element 3, loop continue until the end of the array. Modify the algorithm to stop the loop as soon as finding the target value in the array.
Finding Maximum and minimum value in arrays
Now we go further in the search algorithm, with a litter change to the linear search, we can find the maximum and the minimum value in an array. Find the Maximum and the minimum value of the below array.
In order to find the maximum and minimum number in the unsorted list, we need to check all the elements of the list, the value of each element needs to be checked if it is smaller than the value of variable Min then we change the value of the variable Min to the value of the current element in the list, likewise for the variable Max. By the end of the loop, all elements of the list have been examined and variables Min and Max will be contained of the maximum and minimum numbers in the list.
Following Python script will find the maximum and minimum element in the given list and print it.
List = [1, 2, 3, 15, 0, 125, 321, 9]
Max = 0
Min = 0
for i in range (len(List)):
if Max < List[i]:
Max = List [i]
if Min > List[i]:
Max = List [i]
print 'Maximum Number in the list :', Max
print 'Minimum Number in the list :', Min
Tutorial:
Research to find out how you can add the new element to a list by using the Python then write a script with the following requirements:
-
With a message, ask the user how many elements they want to insert.
-
Ask the user, to enter the elements and store them in a list.
-
Then print all the even numbers have been entered by the user.
Social and ethical Issues
In IT there is a big debate regarding the social and ethical issues, simply social issues are about the right and wrong things and their impacts on social like the impact of social media to change the local cultures, countries law for the unappropriated context of media, information censorship, and search filtering.
In ethical issue area, the concern is mostly relying on intellectual properties such as an illegal download of music, and use of another person’s job without referencing or their permission.
Activity ‘Web search’:
Australian Security Intelligence Organization (ASIO) looking to control and monitor all the devices on the Internet and their content, they also will be able to modify their data with just a single warning. Read the following article and write your own idea about this decision, what do you think about the personal data security in this case? Can anti-terror be a nice label to legislate this law? You may want to go further and do a web search about the government censorship in China to control the people and linked to this article. ASIO Powers to spy over the entire internet pass the Senate
ASIO Powers to spy over the entire internet pass the Senate
http://www.zdnet.com/article/asio-powers-to-spy-over-the-entire-internet-pass-the-senate/