0% found this document useful (0 votes)
15 views6 pages

AL13ICTcompetency9 13

Uploaded by

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

AL13ICTcompetency9 13

Uploaded by

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

08/04/2020

Grade 13 ICT
Competency Level 9.13: ​Searches and sorts data

Contents:
1.Searching techniques - Sequential search
2. Sorting techniques - Bubble sort

1.Searching techniques - Sequential search


Searching is a very basic necessity when we store data in different data structures(Array,list etc..).
The simplest approach is to go across every element in the data structure and match it with the value
which we are searching for. This is known as ​Linear search/Sequential search.
It is inefficient and rarely used, but creating a program for it gives an idea about how we can implement
some advanced search algorithms.

Linear Search /Sequential Search


Here, a sequential search is made over all items one by one. Every item is checked and if a match is found
then that particular item is returned, otherwise the search continues till the end of the data structure.

Below coding is to check whether number 12 and number 91 are in the given number list(values)..

Ex1:
def linear_search(values, search_for):
search_at = 0
search_res = False

# Match the value with each data element


while search_at < len(values) and search_res is False:
if values[search_at] == search_for:
search_res = True
else:
search_at = search_at + 1

return search_res

Values = [64, 34, 25, 12, 22, 11, 90]


print(linear_search(Values, 12))
print(linear_search(Values, 91))

When the above code is executed, it produces the following result −

True
False
Ex2
GCE(A/L) 2018
Essay Q4(b)
2.Sorting techniques - Bubble sort
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data
in a particular order. Most common orders are in ​numerical or lexicographical​ (arrange the letters like in
a language's dictionary)order.

The importance of sorting

The data searching can be optimized to a very high level, if data is stored in a sorted manner. Sorting is
also used to represent data in more readable formats. Below we see five such implementations of sorting
in python.

● Bubble Sort
● Merge Sort
● Insertion Sort
● Shell Sort
● Selection Sort
Bubble Sort

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if
they are in wrong order.

Ex1:

def bubblesort(list):

# Swap the elements to arrange in order


for iter_num in range(len(list)-1,0,-1):
for idx in range(iter_num):
if list[idx]>list[idx+1]:
temp = list[idx]
list[idx] = list[idx+1]
list[idx+1] = temp

list = [19,2,31,45,6,11,121,27]
bubblesort(list)
print(list)

When the above code is executed, it produces the following result −

[2, 6, 11, 19, 27, 31, 45, 121]

Ex2:
GCE(A/L)2012, 48

In this question they are going sort the names according to Lexicographical ordering for strings.So,
initially it will check the “name [1]=Kamal” kamal’s first letter with the first letter of the
“name[2]=Ruwan” , Since ‘K’ should come before ‘R’ name[1], and name[2] should be placed as they
are.
If the first items are equal, the next two items are compared, and so on, until either sequence is
exhausted.
Ex:3
GCE(A/L)2012, Essay 5

In this question using a processData() function to sort the letters in a file….Using comparison method to
swap the adjacent letters to find the order of the letters.

You might also like