Chapter 3
Chapter 3
Chapter 3
Chapter Two
Elementary Sorting and Searching Algorithms
2.1 Searching
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
Page 1 of 8
DDIT
• 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)
Example Implementation:
int Binary Search(int list[],int k)
{
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;
}
Page 2 of 8
DDIT
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
Page 3 of 8
DDIT
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;
}//end of inner loop
}//end of outer loop
}//end of insertion_sort
Analysis
How many comparisons?
1+2+3+…+(n-1)= n(n-1)/2=O(n2)
How many swaps?
1+2+3+…+(n-1)= O(n2)
How much space? In-place algorithm
Example 2.1 Sort the following list using insertion sort algorithm by showing
the necessary steps and intermediate form of the array bellow.
5 9 3 1 7 6 4 8 2
Solution:
i =1, temp=list[i]=list[1]=9
i=2, temp=list[2]=3
j Swa List[9]
p
j Swa List[9]
1 p- 5 9 3 1 7 6 4 8 2
Page 4 of 8
2 (9,3 5 3 9 1 7 6 4 8 2
)
1 (5,3 3 5 9 1 7 6 4 8 2
)
DDIT
j Swa List[9]
p
5 (9,6 1 3 5 7 6 9 4 8 2
)
4 (7,6 1 3 5 6 7 9 4 8 2
)
3 - 1 3 5 6 7 9 4 8 2
2 - 1 3 5 6 7 9 4 8 2
1 - 1 3 5 6 7 9 4 8 2
i=6 , temp= list[6] = 4
j Swa List[9]
p
6 (9,4 1 3 5 6 7 4 9 8 2
)
5 (7,4 1 3 5 6 4 7 9 8 2
)
4 (6,4 1 3 5 4 6 7 9 8 2
2.2.2. Selection Sort )
3 (5,4 1 3 4 5 6 7 9 8 2
Basic Idea: )
2 - 1 3 4 5 6 7 9 8 2
Loop through the array from i=0 to n-1. 1
j -
Swa 1 3 4 5List[9]
6 7 9 8 2
p
Select the smallest element in the array from 7 (9,8 1 3 4 5 6 7 8 9 2 i+1
)
to n
6 - 1 3 4 5 6 7 8 9 2
Swap this value with value at position i. 5 - 1 3 4 5 6 7 8 9 2
4 - 1 3 4 5 6 7 8 9 2
3 - 1 3 4 5 6 7 8 9 2
Page 5 of 8
2 - 1 3 4 5 6 7 8 9 2
1 - 1 3 4 5 6 7 8 9 2
DDIT
Implementation:
void selection_sort(int list[])
{
int i,j, smallest;
for(i=0;i<n-1;i++){
smallest=i;
for(j=i+1;j<n;j++){
if(list[j]<list[smallest])
smallest=j;
}//end of inner loop
temp=list[smallest];
list[smallest]=list[i];
list[i]=temp;
} //end of outer loop
}//end of selection_sort
Analysis
How many comparisons?
(n-1)+(n-2)+…+1= O(n2)
How many swaps?
n=O(n)
Example 2.1 Sort the following list using selection sort algorithm by showing
the necessary steps and intermediate form of the array bellow.
5 9 3 1 7 6 4 8 2
Solution
j( i+1 to n -1) Initial list i(from 0 to n-2)
J 0 1 2 3 4 5 6 7 8
1 5 1 1 1 1 1 1 1 1 1
2 9 9 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3
4 1 5 5 5 4 4 4 4 4 4
5 7 7 7 7 7 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6
7 4 4 4 4 5 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8 8
9 2 2 9 9 9 9 9 9 9 9
Page 6 of 8
DDIT
Page 7 of 8
DDIT
Each of these algorithms requires n-1 passes: each pass places one item in its correct place. The ith
pass makes either i or n - i comparisons and moves. So: or O(n2). Thus these algorithms are only
suitable for small problems where their simple code makes them faster than the more complex code
of the O(nlogn) algorithm. As a rule of thumb, expect to find an O(nlogn) algorithm faster for n>10 -
but the exact value depends very much on individual machines!.
Empirically it’s known that Insertion sort is over twice as fast as the bubble sort and is just as easy to
implement as the selection sort. In short, there really isn't any reason to use the selection sort - use
the insertion sort instead.
If you really want to use the selection sort for some reason, try to avoid sorting lists of more than a
1000 items with it or repetitively sorting lists of more than a couple hundred items.
Question: What is the deference between selection sort and bubble sort?
Page 8 of 8