You are on page 1of 47

Chapter Three

Searching and Sorting algorithm

1
Cont…
Searching Algorithm
Leaner search
Binary search
Sorting Algorithm
Bubble Sort
Insertion Sort
Selection Sort
2
Sorting
• Sorting is a process through which the data is
arranged in ascending or descending order.
• Sorting can be classified in two types
– Internal Sorts:- This method uses only the primary
memory during sorting process. All data items are held
in main memory and no secondary memory is required
this sorting process.
– External Sorts:- Sorting large amount of data requires
external or secondary memory. This process uses
external memory such as HDD, to store the data which
is not fit into the main memory.
3
Cont…
There are 3 types of internal sorts.
(i) SELECTION SORT :- Ex:- Selection sort
algorithm, Heap Sort algorithm
(ii) INSERTION SORT :- Ex:- Insertion sort algorithm,
Shell Sort algorithm
(iii) EXCHANGE SORT :- Ex:- Bubble Sort Algorithm, Quick sort algorithm

4
Advanced Sorting algorithm
Shell Sort
Merge sort
Quick sort
Heap sort

5
Advanced Sorting algorithm
Shell Sort
• Shell sort is an improvement of insertion sort. It is
developed by Donald Shell in 1959.
• Insertion sort works best when the array elements are
sorted in a reasonable order.
• In insertion sort, we move elements only one position
ahead. When an element has to be moved far ahead, many
movements are involved.
6
Cont….
• The shell sort algorithm works by comparing elements
that are distant rather than adjacent in array or list
where adjacent elements are compared.
• Shell sort improves on the efficiency of inserting sort
by quick shifting value to their destination.
• Shell sort also called diminishing increment sort.
• The distance between comparisons decreases as the
sorting algorithm runs until the last phase in which
adjacent elements are compared.
• Shell sort is only efficient for medium size lists. for
bigger lists, shell sort is not best choice.
• Fastest of all O(n2) sorting algorithms 7
Cont…
• 5 times fastest than bubble sorting algorithm
and a little over twice as fast as the insertion
sort , its closest competitor.
• But, shell sorting algorithm is complex
algorithm and it is not nearly as efficient as
merging, heap and quick sort algorithms.
• This sorting algorithm is significantly slower
than merging, heap and quick sorting algorithm,
but this is good choice for sorting elements less
500 items.
8
Cont…
• In this algorithm we compare elements that are
distance apart rather than adjacent elements.
• We start by comparing elements at certain
distance apart, so if there are N elements we
start with value of Gap<N.
• In each pass we keep reducing the value of gap
till we reach the last pass when gap is 1.
• In the last pass, shell sorting is like inserting
sort
9
Cont’d
• Algorithm:

– Choose gap gk between elements to be partly ordered.

– Generate a sequence (called increment sequence) gk, gk-

1 ,…., g2, g1 where for each sequence gi, A[j]<=A[j+gi]

for 0<=j<=n-1-gi and k>=i>=1


Cont’d

11
Cont’d

12
Cont’d

13
Cont’d

14
Cont’d

15
Cont’d

16
17
18
Cont’d

19
Cont’d

20
Cont…

21
Cont’d

22
Cont’d

23
Cont’d

24
Cont’d

25
Cont’d

26
Cont’d

27
Cont’d

Continue comparison until to get full sorted array!!!


28
Example 2: Sort the following list using shell
sort algorithm.

29
Cont’d

30
Cont…
int sort(int arr[])
    {
        int n = arr.length;
        for (int gap = n/2; gap > 0; gap /= 2)
        {
            for (int i = gap; i < n; i += 1)
            {
                
                int temp = arr[i];
                int j;
                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
                 arr[j] = arr[j - gap];
                arr[j] = temp;
            }
        }
        return 0;
    } 31
Merge Sort
•Merge sort is a sorting algorithm that uses the
divide, conquer, and combine algorithmic
paradigm.
•Divide means partitioning the n-element array to
be sorted into two sub-arrays of n/2 elements.
• If A is an array containing zero or one element, then
it is already sorted.
•However, if there are more elements in the array,
divide A into two sub-arrays, A1 and A2, each
containing about half of the elements of A.
32
Cont…
• Conquer: means sorting the two sub-arrays recursively
using merge sort.
• Combine: means merging the two sorted sub-arrays of
size n/2 to produce the sorted array of n elements.
– The basic steps of a merge sort algorithm are as
follows:
• If the array is of length 0 or 1, then it is already
sorted.
• Otherwise, divide the unsorted array into two
sub-arrays of about half the size.
• Use merge sort algorithm recursively to sort each
sub-array.
• Merge the two sub-arrays to form a single sorted list33
Cont….
• Example: Sort the array given below using
merge sort.

34
Sort the following list using merge sort algorithm.

35
Cont…
• The running time of merge sort in the average
case and the worst case can be given as O(n
log n)

36
Quick Sort
• Quick sort is a widely used sorting algorithm
developed by C. A. R. Hoare that makes O(n log
n) comparisons in the average case to sort an array
of n elements.
• Quick sort is the fastest known algorithm. It uses
divide and conquer strategy and in the worst case
its complexity is O (n2). But its expected
complexity is O(nlogn).
• Like merge sort, this algorithm works by using a
divide-and-conquer strategy to divide a single
unsorted array into two smaller sub-arrays. 37
Cont…
• Techniques of quick sort
1) Set the index of the first element in the array to pivot
and left variables. Also, set the index of the last
element of the array to the right variable. That is,
pivot= 0, left = 0, and right = n–1.
2)Start from the element pointed by right and
scan the array from right to left, comparing
each element on the way with the element
pointed by the variable pivot.

38
Cont…
That is, a[pivot] should be less than a[right].
A.If that is the case, then simply continue comparing
until right becomes equal to pivot. Once right =
pivot, it means the pivot has been placed in its
correct position
B. However, if at any point, we have a[pivot] >
a[right], then interchange the two values and jump
to Step 3.
C.Set pivot= right
39
Cont…
3. Start from the element pointed by left and scan the
array from left to right, comparing each element on
the way with the element pointed by pivot. That is,
a[pivot] should be greater than a[left].
(a) If that is the case, then simply continue comparing
until left becomes equal to pivot. Once left = pivot,
it means the pivot has been placed in its correct
position.
(b) However, if at any point, we have a[pivot] < a[left],
then interchange the two values and jump to Step 2.
(c) Set pivot= left.
40
Cont…
In general
– the left part has items less than or equal to the
pivot value
– the right part has items greater than or equal to
the pivot value
3. Recursively sort the left part
4. Recursively sort the right part

41
Cont’d

42
Cont’d

43
The following algorithm can be used to position a pivot value and create partition.

Left=0;
Right=n-1; // n is the total number of elements in the list
PivotPos=Left;
while(Left<Right)
{
if(PivotPos==Left)
{
if(Data[Left]>Data[Right])
{
swap(data[Left], Data[Right]);
PivotPos=Right;
Left++;
}
else
Right--;
}
else
{
if(Data[Left]>Data[Right])
{
swap(data[Left], Data[Right]);
PivotPos=Left;
Right--;
}
else
Left++;
44
}
}
Heap Sort
• Heap sort operates by first converting the list in to a heap
tree. Heap tree is a binary tree in which each node has a
value greater than both its children (if any). It uses a
process called "adjust to accomplish its task (building a
heap tree) whenever a value is larger than its parent. The
time complexity of heap sort is O(nlogn).

45
Cont’d
• Algorithm:
1. Construct a binary tree
• The root node corresponds to Data[0].
• If we consider the index associated with a particular node to be i,
then the left child of this node corresponds to the element with index
2*i+1 and the right child corresponds to the element with index
2*i+2. If any or both of these elements do not exist in the array, then
the corresponding child node does not exist either.
2. Construct the heap tree from initial binary tree using "adjust"
process.
3. Sort by swapping the root value with the lowest, right most value and
deleting the lowest, right most value and inserting the deleted value
in the array in it proper position.

46
47

You might also like