0% found this document useful (0 votes)
13 views

ch2 simple searching & sorting

Uploaded by

Adesa zawude
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)
13 views

ch2 simple searching & sorting

Uploaded by

Adesa zawude
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/ 29

Chapter Two

Simple Searching and


Sorting Algorithms

1
Find your self
 How many of you need to be a programmer?
 Do you remember general syntax of C++?
 What is a difference between program & algorithm?

2
Data Structures and Algorithms
 A program is written in order to solve a problem.
 A solution to a problem actually consists of two things:
 A way to organize the data (Data Structure )

 Sequence of steps to solve the problem (algorithm) or

 An algorithm is a finite set of instructions which

accomplish a particular task


 Therefore, a program is nothing but data structures +
algorithms.
 Abstraction is a process of classifying characteristics as
relevant and irrelevant for the particular purpose at hand and3
ignoring the irrelevant ones.
Algorithms
 An algorithm is a well-defined computational procedure
that takes some value or a set of values as input and
produces some value or a set of values as output.
 Data structures model the static part of the world. They are
unchanging while the world is changing.
 Algorithms are the dynamic part of a program’s world
model.
 An algorithm transforms data structures from one state to
another state in two ways:
1. An algorithm may change the value held by a data structure
2. An algorithm may change the data structure itself
4
Algorithms

 The quality of a data structure is related to its ability to successfully


model the characteristics of the world.
 Similarly, the quality of an algorithm is related to its ability to
successfully simulate the changes in the world.
 However, the quality of data structure and algorithms is determined by
their ability to work together well.

5
Algorithm Analysis Concepts

 There are two approaches to measure the efficiency of


algorithms:
1. Empirical: Programming competing algorithms and
trying them on different instances.
2. Theoretical: Determining the quantity of resources
required mathematically (Execution time, memory
space, etc.) needed by each algorithm.

6
Algorithm Analysis Concepts

 However, it is difficult to use actual clock-time as a


consistent measure of an algorithm’s efficiency,
because clock-time can vary based on many things.
For example,
1. Specific processor speed
2. Current processor load
3. Specific data for a particular run of the program
• Input Size
• Input Properties
4. Operating Environment

7
Complexity Analysis
 Complexity Analysis is the systematic study of the cost of
computation (measured either in time units or in operations
performed), or in the amount of storage space required.
 The goal is to have a meaningful measure that
permits comparison of algorithms independent of
operating platform.
 There are two things to consider:
 Time Complexity: Determine the approximate number of
operations required to solve a problem of size n.
 Space Complexity: Determine the approximate memory
required to solve a problem of size n. Complexity analysis
involves two distinct phases:
8
Complexity Analysis

 Factors that should not affect time complexity analysis:


• The programming language chosen to implement the
algorithm
• The quality of the compiler
• The speed of the computer on which the algorithm is to
be executed
 To determine the feasibility of an algorithm by estimating
an upper bound on the amount of work performed

9
The challenges of the course

 I’m not intimidating you.


Don't terrify when you read and be logical as well as
reasonable.

 The course need your effort


Try to read more, realize the concept, don’t consume
your time by memorizing line by line (term by term)

 The course need your mathematical background


Try calculation

10
Searching and Sorting Algorithm
 Searching is a process of looking for a specific element in a
list of items or determining that the item is not in the list.
There are two simple searching algorithms:
• Sequential Search, and
• Binary Search
1. Linear Search
 Pseudocode- 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 we call this
time complexity O(n).
May 17, 2022 11
Linear Search (Sequential Search)
 Example Implementation:
int Linear_Search(int list[], int key) {
int index=0;
int found=0;
do{
if(key==list[index])
found=1;
else
index++;
}while(found==0&&index<n);
if(found==0)
index=-1;
return index; } 12
Binary Search
 Prerequisite -This searching algorithms works only on an ordered
list.
The basic idea is:
 Step1: Locate midpoint of array to search

 Step2: Determine if target is in lower half or upper half of an

array.
 If in lower half, make this half the array to search

 If in the upper half, make this half the array to search

 Step3: Loop back to step 1 until the size of the array to search is

one, and this element does not match, in which case return –1.
 The computational time for this algorithm is proportional to log2 n.
Therefore the time complexity is O(log n)
13
Example Implementation:
int Binary_Search(int list[], int key) {
int left=0; int right=n-1; int found=0;
do{
mid=(left+right)/2;
if(key==list[mid])
found=1;
else{ if(key<list[mid])
right=mid-1;
else
left=mid+1;
}
}while(found==0&&left<right);
if(found==0)
index= -1;
else
index=mid;
return index;} 14
Binary search

15
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.
 The following are simple sorting algorithms used to sort
small-sized lists.
• Insertion Sort
• Selection Sort
• Bubble Sort

16
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 inplace 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 natural type of sorting
algorithm. The approach is the same approach that you use
for sorting a set of cards in your hand.
17
Insertion Sort: 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. 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.
 Now the first three are relatively sorted.
5. Do the same for the remaining items in the list. 18
Insertion Sort Implementation
void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++){
temp=list[i];
for(int j=i; j>0 && temp<list[j-1];j--)
{ // work backwards through the array
finding where temp should go
list[j]=list[j-1];
list[j-1]=temp;
}
}
}

19
Insertion Sort Implementation
void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++){
temp=list[i];
for(int j=i; j>0 && temp<list[j-1];j--)
{ // work backwards through the array
finding where temp should go
list[j]=list[j-1];
list[j-1]=temp;
}
}
}

20
Insertion Sort Implementation

21
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.

 Find smallest element in unsorted portion of container

May 17, 2022 singular linked list 22


23
Selection Sort

May 17, 2022 24


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

May 17, 2022 25


void bubble_sort(list[]){
Bubble Sort
int i,j,temp;
for(i=0;i<n; i++){
Bubble sort starts with very first two
for(j=0;j<n; j++){
elements, comparing them to check
if(list[j]>list[j+1]){ which one is greater.

temp=list[j];
list[j]=list[j+1]; In this case, value 33 is greater
than 14, so it is already in sorted
list[j+1]=temp; locations. Next, we compare 33
with 27.
}
}
We find that 27 is smaller than 33 and
} these two values must be swapped.

}
void bubble_sort(list[]){
Bubble Sort
int i,j,temp; The new array should look like this -

for(i=0;i<n; i++){
for(j=0;j<n; j++){ Next we compare 33 and 35. We find
that both are in already sorted
if(list[j]>list[j+1]){
positions.
temp=list[j];
list[j]=list[j+1];
Then we move to the next two
list[j+1]=temp; values, 35 and 10.

}
}
We know then that 10 is smaller
} 35. Hence they are not sorted.
} 27
void bubble_sort(list[]){
int i,j,temp; We swap these values. We find that
we have reached the end of the array.
for(i=0;i<n; i++){ After one iteration, the array should
look like this -
for(j=0;j<n; j++){
if(list[j]>list[j+1]){
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
} We take an unsorted array for our
example. Bubble sort takes Ο(n2 ) time
} so we're keeping it short and precise 28
End of
Simple search
& sort

May 17, 2022


29

You might also like