0% found this document useful (0 votes)
18 views24 pages

ch#2

The document discusses simple searching and sorting algorithms, highlighting their importance in computer systems. It covers sequential and binary search methods, explaining their algorithms and time complexities, as well as sorting algorithms like insertion, selection, and bubble sort. The document emphasizes that sorting improves the efficiency of searching operations.

Uploaded by

Getaneh Awoke
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)
18 views24 pages

ch#2

The document discusses simple searching and sorting algorithms, highlighting their importance in computer systems. It covers sequential and binary search methods, explaining their algorithms and time complexities, as well as sorting algorithms like insertion, selection, and bubble sort. The document emphasizes that sorting improves the efficiency of searching operations.

Uploaded by

Getaneh Awoke
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/ 24

C H A P T E R TWO

SIMPLE SEARCHING
AND
SORTING ALGORITHMS
32
Why do we study sorting and searching algorithms?
 These algorithms are the most common and
useful tasks operated by computer system.
 Computers spend a lot of time for searching and
sorting.
1. Simple Searching algorithms
 Searching:- is a process of finding an element
in a list of items or determining that the item is
not in the list.
 A search method looks for a key, arrives by
parameter. By convention, the method will
return the index of the element corresponding to
the key or, if unsuccessful, the value -1.
33
 There are two simple searching algorithms:
⚫ Sequential, and Binary Search
2.1.1. Sequential Searching (Linear)
 Sequential search looks at elements, one at a time, from the first in
the list until a match for the target is found.
 The most natural way of searching an item. Easy to understand and
implement.
Algorithm:
 In a linear search, we start with top (beginning) of the list, and
compare the element at top with the key.
 If we have a match, the search terminates and the index number is
returned. If not, we go on the next element in the list.
 If we reach the end of the list without finding a match, we return -1.

Pseudo code
 Loop through the array starting at the first element until the value
of target matches one of the array elements.
 If a match is not found, return –1.

 Time is proportional to the size of input (n) and


34
 We call this time complexity O(n)
4
5
6
2.1.2. Binary Searching
 This searching algorithm works only for an ordered list of
elements and also uses divide and conquer strategy
(approach).
Algorithm:
I. In a binary search, we look for the key in the middle of the
list. If we get a match, the search is over.
II. If the key is greater than the element in the middle of the
list, we make the top (upper) half the list to search.
III. If the key is smaller, we make the bottom (lower) half the
list to search.
IV. Repeat the above steps (I,II and III) until one element
remains.
 If this element matches return the index of the element,

else return -1 index. (-1 shows that the key is not in the list).
 The computational time for this algorithm is proportional to38
log2 n Therefore the time complexity is O(log n)
39
S P A C E C OMPLEXITY O(1) 40
2.2. Sorting Algorithms
 Sorting is one of the most important operations
performed by computers.
 Sorting is a process of reordering a list of items in either
increasing or decreasing order.
 Sorting data (i.e., placing the data into some particular
order, such as ascending or descending) is one of the
most important computing applications.
 It is important due mainly to the fact that searching a
list is much faster if the list is sorted.
 The following are simple sorting algorithms used to sort
small-sized lists.
⚫ Insertion Sort
⚫ Selection Sort
⚫ Bubble Sort 41
1. Insertion Sort
 The insertion sort works just like its name suggests - it
inserts each item into its proper place in the final list.
 The simplest implementation of this requires two list
structures - the source list and the list into which sorted
items are inserted.
 To save memory, most implementations use an in-place
sort that works by moving the current item past the
already sorted items and repeatedly swapping it with
the preceding item until it is in place.
 It's the most instinctive type of sorting algorithm.

 The approach is the same approach that you use for


sorting a set of cards in your hand.
 While playing cards, you pick up a card, start at the
beginning of your hand and find the place to insert th42e
new card, insert it and move all the others up one place.
Basic Idea:
 Find the location for an element and move all others up, and
insert the element. The process involved in insertion sort is
as follows:
1. The left most value can be said to be sorted relative to
itself. Thus, we don’t need to do anything.
2. Check to see if the second value is smaller than the first
one. If it is, swap these two values. The first two values
are now relatively sorted.
3. Next, we need to insert the third value in to the
relatively sorted portion so that after insertion, the
portion will still be relatively sorted.
4. Remove the third value first. Slide the second value to
make room for insertion. Insert the value in the
appropriate position.
5. Now the first three are relatively sorted.
43
6. Do the same for the remaining items in the list.
13
14
15
16
2.2.2. Selection Sort
Basic Idea:
Loop through the array from i=0 to n-1.
Select the smallest element in the array from i to n
Swap this value with value at position i.

17
18
19
2.2.3. Bubble Sort
Bubble sort is the simplest algorithm to implement and the slowest
algorithm on very large inputs.
Basic Idea: Loop through array from i=0 to n and swap adjacent
elements if they are out of order.

20
21
22
23

You might also like