The document discusses simple sorting and searching algorithms. It describes selection sort, bubble sort, and insertion sort, which are all O(n^2) elementary sorting algorithms best for small lists. It also covers linear/sequential search, which has O(n) complexity, and binary search, which has optimal O(log n) complexity but requires a sorted list. Pseudocode and examples are provided for each algorithm.
2. CHAPTER OVERVIEW
Simple Sorting algorithms
o Selection sort
o Bubble sort
o Insertion sort
Simple searching algorithms
o Linear/sequential searching
o Binary searching
2
20 October 2023
3. SORTING ALGORITHMS
Sorting
o process of reordering a list of items in either increasing or
decreasing order.
o efficiency of sorting algorithm is measured using
o the number of comparisons and
o the number of data movements made by the algorithms.
o Sorting algorithms are categorized as:
o simple/elementary and
o advanced.
o Simple sorting algorithms, like Selection sort, Bubble sort and
Insertion sort, are only used to sort small-sized list of items.
3
20 October 2023
4. 1. SELECTION SORT
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
4
20 October 2023
5. SELECTION SORT …CONT’D
i.e. the basic idea is
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.
we repeatedly find the next largest (or smallest)
element in the array and move it to its final position
in the sorted array.
Note: the list/array is divided into two parts:
the sub-list of items already sorted and
the sub-list of items remaining to be sorted
5
20 October 2023
6. SELECTION SORT EXAMPLE …CONT’D
Analysis:
The outer loop executes n-1 times
The inner loop executes about n(n-1)/2
times on average (from n to 2 times)
Work done in the inner loop is constant
(swap two array elements)
Time required is roughly (n-1)*[n(n-1)/2]
You should recognize this as O(n2)
i.e.
How many comparisons?
(n-1)+(n-2)+…+1 = O(n2)
How many swaps?
n = O(n)
6
20 October 2023
7 2 8 5 4
2 7 8 5 4
2 4 8 5 7
2 4 5 8 7
2 4 5 7 8
7. CODE FOR SELECTION SORT …CONT’D
void selectionSort(int[] a)
{
int outer, inner, min;
for (outer = 0; outer < a.length - 1; outer++)
{
min = outer;
for (inner = outer + 1; inner < a.length; inner++)
{
if (a[inner] < a[min])
{
min = inner;
}
}
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
}
}//end of function
7
20 October 2023
8. 2. BUBBLE SORT
Also called Exchange sort
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.
repeatedly compares adjacent elements of an array.
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 8
20 October 2023
10. CODE FOR BUBBLE SORT ...CONT’D
void bubbleSort(int[] a)
{
int outer, inner;
for (outer = a.length - 1; outer > 0; outer--) //counts down
{
for (inner = 0; inner < outer; inner++)
{
if (a[inner] > a[inner + 1])
{
int temp = a[inner];
a[inner] = a[inner + 1];
a[inner + 1] = temp;
}
}
}
}
10
20 October 2023
11. ANALYSIS FOR BUBBLE SORT ...CONT’D
Let n = a.length = size of the array
The outer loop is executed n-1 times
Each time the outer loop is executed, the inner loop is executed
Inner loop executes n-1 times at first, linearly dropping to just
once
On average, inner loop executes about n(n-1)/2 times for
each execution of the outer loop
In the inner loop, the comparison is always done (constant
time), the swap might be done (also constant time)
Result is (n-1) * [ n(n-1)/2 ] + k, that is, O(n2)
i.e.
How many comparisons?
(n-1)+(n-2)+…+1= O(n2)
How many swaps?
(n-1)+(n-2)+…+1= O(n2)
11
20 October 2023
12. 3. INSERTION SORT
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.
Basic Idea:
Find the location for an element and move all others up, and
insert the element.
The approach is the same approach that we use for
sorting a set of cards in our hand.
While playing cards, we pick up a card, start at the
beginning of our hand and find the place to insert the
new card, insert it and move all the others up one place.
12
20 October 2023
13. INSERTION SORT ...CONT’D
The algorithm 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.
6. Do the same for the remaining items in the list.
13
20 October 2023
14. INSERTION SORT EXAMPLE ...CONT’D
14
20 October 2023
Sort: 34 8 64 51 32 21
34 8 64 51 32 21
The algorithm sees that 8 is smaller than 34 so it swaps.
8 34 64 51 32 21
51 is smaller than 64, so they swap.
8 34 51 64 32 21
The algorithm sees 32 as another smaller number and moves
it to its appropriate location between 8 and 34.
8 32 34 51 64 21
The algorithm sees 21 as another smaller number and moves
into between 8 and 32.
Final sorted numbers:
8 21 32 34 51 64
15. CODE FOR INSERTION SORT ...CONT’D
void insertionSort(int[] array)
{
int inner, outer;
for (outer = 1; outer < array.length; outer++)
{
int temp = array[outer];
inner = outer;
while (inner > 0 && array[inner - 1] >= temp)
{
array[inner] = array[inner - 1];
inner--;
}
array[inner] = temp;
}
} 15
20 October 2023
16. ANALYSIS OF INSERTION SORT ...CONT’D
We run once through the outer loop, inserting each of n
elements; this is a factor of n
On average, there are n/2 elements already sorted
The inner loop looks at (and moves) half of these
This gives a second factor of n/4
Hence, the time required for an insertion sort of an array of
n elements is proportional to n2/4
Discarding constants, we find that insertion sort is O(n2)
i.e.
How many comparisons?
1+2+3+…+(n-1)= O(n2)
How many swaps?
1+2+3+…+(n-1)= O(n2)
16
20 October 2023
17. SUMMARY OF SORTING ALGORITHMS
Bubble Sort, Selection Sort, and Insertion Sort are
all O(n2)
Within O(n2),
Bubble Sort is very slow, and should probably never be
used for anything
Selection Sort is intermediate in speed
Insertion Sort is usually the fastest of the three--in fact, for
small arrays (like 10 or 15 elements), insertion sort is faster
than more complicated sorting algorithms
Selection Sort and Insertion Sort are “good enough”
for small arrays
17
20 October 2023
18. SEARCHING ALGORITHMS
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.
Two simple searching algorithms:
Sequential / Linear Search, and
Binary Search
18
20 October 2023
19. 1. LINEAR SEARCHING
Also called sequential searching
Simplest type of searching process
Easy to implement
Can be used on very small data sets
Not practical for searching large collections
The idea is:
Loop through the array starting at the first/last element until
the value of target matches one of the array elements or until
all elements are visited.
If a match is not found, return –1
Analysis:
Time is proportional to the size of input n
time complexity O(n)
19
20 October 2023
20. LINEAR SEARCHING …CONT’D
Algorithm for Sequential/Linear Search
1. Initialize searcharray, key/number to be searched,
length
2. Initialize index=0,
3. Repeat step 4 till index<=length.
4. if searcharray[index]=key
return index
else
increment index by 1.
20
20 October 2023
21. IMPLEMENTATION OF LINEAR SEARCHING …CONT’D
int linearSearch(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;
}
21
20 October 2023
22. 2. BINARY SEARCHING
This searching algorithms works only on an ordered list.
It uses principle of divide and conquer
Though additional cost has to do with keeping list in order, it is
more efficient than linear search
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.
If in lower half, make this half the array to search
If in the upper half, make this half the array to search
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.
22
20 October 2023
23. BINARY SEARCHING …CONT’D
Analysis:
computational time for this algorithm is proportional to log2n
Therefore the time complexity is O(log n)
Algorithm for Binary Search
1. Initialize an ordered array, searcharray, key, length.
2. Initialize left=0 and right=length
3. Repeat step 4 till left<=right
4. Middle =(left + right) / 2
5. if searcharray[middle]=key
Search is successful
return middle.
else
if key<searcharray[middle]
right=middle - 1
else
left=middle + 1.
23
20 October 2023
24. IMPLEMENTATION OF BINARY SEARCHING …CONT’D
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;
} 24
20 October 2023