Searching and Sorting1 231121 100048
Searching and Sorting1 231121 100048
By
H R Choudhary (Asstt. Professor)
Department of CSE
Engineering College Ajmer
Linear Search or Sequential Search
Binary Search
end procedure
The element being searched may be found at the first position.
In this case, the search terminates in success with just one
comparison.
Thus in best case, linear search algorithm takes O(1)
operations.
Worst Case-
In the worst possible case,
The element being searched may be present at the last position or not
present in the array at all.
In the former case, the search terminates in success with n comparisons.
In the later case, the search terminates in failure with n comparisons.
Thus in worst case, linear search algorithm takes O(n) operations.
Thus, we have-
Time Complexity of Linear Search Algorithm is O(n).
We change our low to mid + 1 and find the new mid value
again.
low = mid + 1
mid = low + (high - low) / 2
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Sorting Terms:
Some terms are generally coined while discussing sorting techniques,
here is a brief introduction to them −
Increasing Order:
A sequence of values is said to be in increasing order, if the successive
element is greater than the previous one. For example, 1, 3, 4, 6, 8, 9
are in increasing order, as every next element is greater than the
previous element.
Decreasing Order:
A sequence of values is said to be in decreasing order, if the successive
element is less than the current one. For example, 9, 8, 6, 4, 3, 1 are
in decreasing order, as every next element is less than the previous
element.
Sorting...
Non-Increasing Order:
A sequence of values is said to be in non-increasing order, if the
successive element is less than or equal to its previous element in
the sequence. This order occurs when the sequence contains
duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing
order, as every next element is less than or equal to (in case of 3)
but not greater than any previous element.
Non-Decreasing Order:
A sequence of values is said to be in non-decreasing order, if the
successive element is greater than or equal to its previous element
in the sequence. This order occurs when the sequence contains
duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-
decreasing order, as every next element is greater than or equal to
(in case of 3) but not less than the previous one.
Thank You
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of
the array. After one iteration, the array should look like this −
To be precise, we are now showing how an array should look
like after each iteration. After the second iteration, it should
look like this −
Notice that after each iteration, at least one value moves at the
end.
And when there's no swap required, bubble sorts learns that an
array is completely sorted.
Algorithm:
We assume list is an array of n elements. We further assume that
swap function swaps the values of the given array elements.
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
We observe in algorithm that Bubble Sort compares each pair
of array element unless the whole array is completely sorted
in an ascending order. This may cause a few complexity
issues like what if the array needs no more swapping as all
the elements are already ascending.
loop = list.count;
for i = 0 to loop-1 do:
swapped = false
for j = 0 to loop-1 do:
end for
So we swap them.
This process goes on until all the unsorted values are covered in a sorted
sub-list.
end for
end procedure
Thank You
The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the
sorted array. This process continues moving unsorted array
boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and
worst case complexities are of Ο(n2), where n is the number of
items. H R Choudhary Asstt Professor Dept. Of CSE, Engineering College Ajmer
Selection Sort...
For the first position in the sorted list, the whole list is scanned
sequentially. The first position where 14 is stored presently,
we search the whole list and find that 10 is the lowest value.
The same process is applied to the rest of the items in the array.
Algorithm:
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
end procedure
Merge sort first divides the array into equal halves and
then combines them in a sorted manner.
How Merge Sort Works?
To understand merge sort, we take an unsorted array as the
following −
We know that merge sort first divides the whole array
iteratively into equal halves unless the atomic values are
achieved. We see here that an array of 8 items is divided into
two arrays of size 4.
This does not change the sequence of appearance of items in
the original. Now we divide these two arrays into halves.
We further divide these arrays and we achieve atomic
value which can no more be divided.
Now, we combine them in exactly the same manner as they
were broken down. Please note the color codes given to these
lists.
We first compare the element for each list and then combine
them into another list in a sorted manner. We see that 14 and
33 are in sorted positions. We compare 27 and 10 and in the
target list of 2 values we put 10 first, followed by 27. We
change the order of 19 and 35 whereas 42 and 44 are placed
sequentially.
In the next iteration of the combining phase, we
compare lists of two data values, and merge them
into a list of found data values placing all in a sorted
order.
After the final merging, the list should look like this −
Algorithm:
Merge sort keeps on dividing the list into equal halves
until it can no more be divided. By definition, if it is
only one element in the list, it is sorted. Then, merge
sort combines the smaller sorted lists keeping the
new list sorted too.
Step 1 − if it is only one element in the list it is already sorted,
return.
Step 2 − divide the list recursively into two halves until it can
no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
procedure mergesort( var a as array )
if ( n == 1 ) return a
l1 = mergesort( l1 )
l2 = mergesort( l2 )
var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while
while ( a has elements )
add a[0] to the end of c
remove a[0] from a
end while
return c
end procedure
Quick Sort Algorithm
Quicksort is the widely used sorting algorithm that makes n log n
comparisons in average case for sorting an array of n elements. It is a
faster and highly efficient sorting algorithm. This algorithm follows
the divide and conquer approach. Divide and conquer is a technique of
breaking down the algorithms into subproblems, then solving the
subproblems, and combining the results back together to solve the
original problem.
After that, left and right sub-arrays are also partitioned using the
same approach. It will continue until the single element
remains in the sub-array.
H R Choudhary Asstt Professor Dept. Of CSE, Engineering College Ajmer
H R Choudhary Asstt Professor Dept. Of CSE, Engineering College Ajmer
Choosing the pivot
Picking a good pivot is necessary for the fast implementation of
quicksort. However, it is typical to determine a good pivot. Some of
the ways of choosing a pivot are as follows -
Pivot can be random, i.e. select the random pivot from the given array.
Pivot can either be the rightmost element of the leftmost element of
the given array.
Select median as the pivot element.