Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Searching and Sorting algorithms

Similar presentations


Presentation on theme: "Data Searching and Sorting algorithms"— Presentation transcript:

1 Data Searching and Sorting algorithms
Ahti Lohk Teaching Assistant in Department of Software Science PhD (in Computer Science)

2 Agenda This presentation answers to the following questions:
Why to use searching algorithms? What is the idea of the Linear search? What is the idea of Binary search? What are efficiency indicators of a program? Why to use sorting algorithms? What is the idea of Bubble sort? What is the idea of Shell sort?

3 Why to use searching algorithms?
To check if an item is in a set (included different type of sequences: string, array, collection, file, etc). F.e., to check if the user has permission for certain actions. To get additional information about the searchable. Of course, the result (and its representation) depends on from where do we search – yippy.com, pipl.com or from specific table

4 Background of search algorithms
There are lot of search algorithms in use. Different data structures (array, linked list, search trees, hash tables) use different algorithms. In this lecture we focus on two search algorithms that are used on sequences (an array, sequence of data in Excel worksheet column or row): Linear search (also known as sequential search) Binary search

5 Searching algorithms

6 How the linear search works?
The idea is to go through all items in the sequence and compare each of them with a searchable/lookup value. If the searchable value is equal to an item value in the sequence the result will be the sequence number of found item. If the searchable value is not in the sequence the result will be -1.

7 Linear search VBA code and usage (the lookup value (Siim) is in the sequence (names))

8 Linear search VBA code and usage (the lookup value (Toivo) is not in the sequance (names))

9 How the binary search works?
The binary search assumes that the target sequence is sorted. This method reduces the search area by half (bisection) repeatedly and try to find target value. More precisely, this method follows the following steps: Repeat until the search area size is 0: Calculate the middle point of the current search area If the target is at the middle, stop. Otherwise, if the target is less that what is at the middle, repeat, changing the end point to be just to the left (if sequence is like a row) of the middle. Otherwise, if the target is greater than what is at the middle , repeat, changing the start point to be just to the right (if sequence is like a row) of the middle.

10 Binary search VBA code and usage (the lookup value (Julia) is in the sequance (names))

11 Binary search VBA code and usage (the lookup value (Sander) is not in the sequance (names))

12 The efficiency indicators of a program
There are three well known efficiency indicators: 1. Number of comparisons – how many comparisons the algorithm does. 2. Number of actions – how many sentences the program has to execute to get the result. 3. Memory capacity – how much memory is used during the work of the program.

13 Comparing linear and binary search
The efficiency of the both methods depends on where the lookup value in the sequence is located. It is typical to evaluate the algorithm according to its worst case. In the case of the linear search the worst is when the lookup value is not in the sequence or it is in the last position. In the case of the binary search the worst case is when the lookup value is not in the sequence.

14 Comparing linear and binary search 2
Comparing based on the worst case of the number of comparisons. If the sequence length is 100 items then in the case of linear search the number of comparison is also 100. But in the case of binary search this number is perceptively 7.

15 Why sorting? Sorting is the easiest way to arrange data.
The idea of all given sorting algorithms is as follows: Compare the items of a sequence with each other and (Inter)change their positions if it is needed.

16 Some well-known sorting algorithms
Simple sorts Efficient Sorts

17 A question to Barack Obama
What is the efficient way to sort a million 32-bit integers? See the answer in the following YouTube video:

18 Comparisons (in seconds)
Nr of items Bubble Insertion Shell Shell Mod Quick 10 000 4 2 0,055 0,031 0,000 50 000 96 46 0,52 0,14 0,070 0,22 0,040 3,14 0,38 4,20

19 Sequential sort Sub SequentialSort(V As Range, nrOfValues&) Dim i&, j&, tmp& For i = 1 To nrOfValues - 1 For j = i + 1 To nrOfValues If V(i) > V(j) Then tmp = V(i): V(i) = V(j): V(j) = tmp Next j Next i End Sub

20 Bubble sort Sub Bubble_Sort(V As Range, nrOfValues&) Dim i&, k&, tmp&, sorted As Boolean k = 0 Do sorted = True k = k + 1 For i = 1 To nrOfValues - k If V(i) > V(i + 1) Then tmp = V(i): V(i) = V(i + 1): V(i + 1) = tmp sorted = False End If DoEvents Next i Loop Until sorted = True End Sub

21 Shell Sort Sub ShellSort(V as range, nrOfValues&) End Sub
Dim gap&, i&, k&, tmp& Dim As Boolean gap = nrOfValues Do While gap > 1 gap = gap \ 2 Do noexchange = True For i = 1 To nrOfValues - gap k = i + gap If V(k) < V(i) Then tmp = V(k): V(k) = V(i): V(i) = tmp: noexchange = False Next i Loop Until noexchange = True Loop End Sub


Download ppt "Data Searching and Sorting algorithms"

Similar presentations


Ads by Google