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

Chapter 2 Simple Sorting and Searching Algorithms

Chapter Two discusses simple sorting and searching algorithms, including Linear Search and Binary Search, with their implementations and time complexities. It also covers sorting algorithms such as Insertion Sort, Selection Sort, and Bubble Sort, detailing their mechanisms, implementations, and performance analysis. The chapter concludes with a comparison of the algorithms, highlighting their best, average, and worst-case time complexities.

Uploaded by

nafyjabesa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Chapter 2 Simple Sorting and Searching Algorithms

Chapter Two discusses simple sorting and searching algorithms, including Linear Search and Binary Search, with their implementations and time complexities. It also covers sorting algorithms such as Insertion Sort, Selection Sort, and Bubble Sort, detailing their mechanisms, implementations, and performance analysis. The chapter concludes with a comparison of the algorithms, highlighting their best, average, and worst-case time complexities.

Uploaded by

nafyjabesa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter Two

Simple Sorting and Searching Algorithms


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
Linear Search (Sequential 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).
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;
}
Binary Search
This searching algorithm works only on an ordered list.
The basic idea is:
1. Locate midpoint of array to search
2. Determine if target is in lower half or upper half of an array.
o If in lower half, make this half the array to search
o If in the upper half, make this half the array to search

1
3. 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 log 2 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;
}
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
2.2.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-

2
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 the 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. Now the first three are relatively sorted.
5. Do the same for the remaining items in the list.
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;
}//end of inner loop
}//end of outer loop
}//end of insertion_sort
Analysis
How many comparisons?
1 + 2 + 3 +…+ (n-1) = O(n2)
How many swaps?
1 + 2 + 3 +…+ (n-1) = O(n2)
How much space?
3
In-place algorithm

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.
Implementation:
void selection_sort(int list[])
{
int i, j, smallest;
for(i = 0; i < n; 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)
How much space?
In-place algorithm
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.

4
Implementation:
void bubble_sort(list[])
{
int i, j, temp;
for(i = 0; i < n; i++){
for(j = n-1; j > i; j--){
if(list[j] < list[j-1]){
temp = list[j];
list[j] = list[j-1];
list[j-1] = temp;
}//swap adjacent elements
}//end of inner loop
}//end of outer loop
}//end of bubble_sort
Analysis of Bubble Sort
How many comparisons?
(n-1) + (n-2) +…+ 1 = O(n2)
How many swaps?
(n-1) + (n-2) +…+ 1 = O(n2)
How much Space?
In-place algorithm.
General Comments
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(n logn) 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!.

5
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.
Basic characteristics of sorting algorithms:
1. Insertions sort
 Reduce the number of comparisons in each pass. Because it stops doing so the moment
the exact location is found.
 Thus, it runs in a linear time for an almost sorted input data.
 Involves frequent data movement.
 Appropriate for input whose size is small and almost sorted.
2. Selection sorting
 Least movement data (swap)
 Continuous working even after the list is sorted i.e. many comparisons.
 Appropriate for applications whose comparisons are cheap but swaps are expensive.
3. Bubble Sort
 Easy to develop and implement.
 Involves sequential data movement.
 Continuous working even after the elements have been sorted i.e., many comparisons
 Although it is the least efficient algorithm it may be fast on an input data that is small in
size and almost sorted.
Comparisons of sorting algorithms
There is no optimal sorting algorithm. One may be better for less number of data, another for
average etc… due to this fact it may be necessary to use more than one method for a simple
applications.
Algorithms Best Case Average Case Worst Case
2
Insertion O(n) O(n ) O(n2)
Selection O(n2) O(n2) O(n2)
Bubble O(n2) O(n2) O(n2)

You might also like