Professional Documents
Culture Documents
Chapter Three 2
Chapter Three 2
2
1. Simple Searching algorithms
4
Algorithm:
• In a linear search, we start with top (beginning) of the
list, and compare the element at top with the key.
5
Implementation:
• Assume the size of the list is n.
7
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 and
returns its index
III. If the key is smaller, we make the left (lower) half the
list to search.
8
IV. Repeat the above steps (I,II and III) until one
element remains.
9
Implementation: Return index;
int BinarySearch(int list[ ], int key)
Complexity Analysis:
{
int found=0,index=-1;
int top=n-1,bottom=0,middle; Example: Find Big-Oh of
do{ Binary search algorithm
middle=(top + bottom)/2; in the worst case analysis.
if(key==list[middle]){
found=1;
Index=middle;
}
else{
if(key<list[middle])
top=middle-1;
else
bottom=middle+1;
}
} 10
}while(found==0 && top>=bottom);
Simple Sorting Algorithm
• Even small children are taught to put their things "in order", and
they are confronted with some sort of sorting long before they
learn anything about arithmetic.
11
Simple Sorting Algorithm
• One of the most important purpose of sorting is to facilitate later
search from the sorted set.
• Hence, sorting is a relevant and essential activity, particularly in
data processing.
12
Simple Sorting Algorithms
13
I. Simple sorting
Algorithm:
• In simple sort algorithm the first element is
compared with the second, third and all
subsequent elements.
• If any one of the other elements is less than
the current first element then the first
element is swapped with that element.
• Eventually, after the last element of the list is
considered and swapped, then the first
element has the smallest element in the list.
• The above steps are repeated with the
second, third and all subsequent elements.
14
`
0 1 2
Example: n-1
--------
i
j
4 2 3 1
i=0 j=1 2 4 3 1
j=2 2 4 3 1
j=3 1 4 3 2
i=1 j=2 1 3 4 2
j=3 1 2 4 3
i=2 j=3 1 2 3 4
15
`
Analysis: O(?)
1st pass----- (n-1) comparisons
2nd pass---- (n-2) comparisons
|
|
|
(n-1)th pass--- 1 comparison
T(n)=1+2+3+4+-------+(n-2)+(n-1)
= (n*(n-1))/2
=n2/2-n/2
=O(n2)
16
Complexity Analysis:
17
II. Selection sort
Selection sort Algorithm
• It is the most natural and easiest sorting algorithm
• Given an array of length n,
– Search elements 0 through n-1 and select the smallest
• Swap it with the element in location 0
– Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
– Search elements 2 through n-1 and select the smallest
• Swap it with the element in location 2
– Search elements 3 through n-1 and select the smallest
• Swap it with the element in location 3
– Continue in this fashion until there’s nothing left to search
18
Elementary Sorting Algorithm
Selection sort algorithm
SelectionSort (A, n)
for j 0 to n-1
min A[j]
index j
//find the index of the jth min value
for i j+1 to n
if(A[i] < min)
index i
min A[i]
//swap A[j]and A[index]
temp min
A[index] A[j]
A[j] temp
19
Elementary Sorting Algorithm
Selection sort example
7 2 8 5 4
• Selection Sort might swap an array
element with itself--this is harmless,
2 7 8 5 4
and not worth checking for
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
20
Elementary Sorting Algorithm
SelectionSort (A, n)
for j 0 to n-1
min A[j]
index j
//find the index of the jth min value
for i j+1 to n-1
if(A[i] < min)
index i
min A[i]
//swap A[j]and A[index]
temp min
A[index] A[j]
A[j] temp
Note:
Both worst and best case has the same growth rate
21
III. Insertion sort
• Given an array of length n,
– Take the element at index j and call is key
– Let the element at index i is greater than key and the element
at index i-1 is less than the key
– Push all the element from index j-1 to i one step to the right
– Insert the key into index I
22
Elementary Sorting Algorithm
Insertion sort algorithm
InsertionSort (A, n)
for j 1 to n-1
key A[j]
//Insert key into the list A[0…j-1] so that A[0..j] becomes sorted
i j-1
while (i >= 0 and A[i] > key)// i is valid index and A[i] > the key
A[i+1] A[i]
i i-1
A[i+1] key
23
Elementary Sorting Algorithm
One step of insertion sort
sorted next to be inserted
3 4 7 12 14 14 20 21 33 38 10 55 9 23 28 16
temp
less than 10 10
3 4 7 10 12 14 20
12 14 14 21 21 38
20 33 33 10
38 55 9 23 28 16
sorted
24
Elementary Sorting Algorithm
Insertion sort algorithm analysis (worst case)
• In this case the inner loop will iterate j times for every j.
• This is the case where the data is sorted in descending order
InsertionSort (A, n)
for j 1 to n-1
key A[j]
i j-1
while (i >= 0 and A[i] > key)
A[i+1] A[i]
i i-1
A[i+1] key
25
Elementary Sorting Algorithm
Insertion sort algorithm analysis (best case)
• In this case the inner loop will iterate one times for every j.
• This is the case where the data is sorted in ascending order
InsertionSort (A, n)
for j 1 to n-1
key A[j]
i j-1
while (i >= 0 and A[i] > key)
A[i+1] A[i]
i i-1
A[i+1] key
26
IV. Bubble sort
• Compare each element (except the last one) with its neighbor to
the right
– If they are out of order, swap them
– This puts the largest element at the very end
– The last element is now in the correct and final place
• Compare each element (except the last two) with its neighbor to
the right
– If they are out of order, swap them
– This puts the second largest element next to last
– The last two elements are now in their correct and final places
• Compare each element (except the last three) with its neighbor
to the right
– Continue as above until you have no unsorted elements on the left
27
Elementary Sorting Algorithm
3. Bubble sort algorithm
BubbleSort (A, n)
for j 0 to n-2
for i 1 to n -1 -j
if( A[i-1] > A[i])
//Swap A[i] and A[i-1]
temp A[i-1]
A[i-1]A[i]
A[i] temp
28
Elementary Sorting Algorithm
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
29
Elementary Sorting Algorithm
Bubble sort algorithm analysis
BubbleSort (A, n)
for j 0 to n-2
for i 1 to n -1 -j
if( A[i-1] > A[i])
temp A[i-1]
A[i-1]A[i]
A[i] temp