# Data Structures part-4 by vijayreddy.ravipati

VIEWS: 3 PAGES: 169

• pg 1
```									    UNIT – IV

Searching
– Linear search
- binary search

Sorting
–   Bubble sort
-   selection sort
-   Insertion sort
-   Quick sort
-   merge sort.

1
Searching and Sorting
• Searching is the process of finding a particular element in an array
• Sorting is the process of rearranging the elements in an array so
that they are stored in some well-defined order
Searching Algorithms
• Linear search: the search starts at the beginning of the array and
goes straight down the line of elements until it finds a match or
reaches the end of the array
• Binary search: the search starts at the center of a sorted array and
determines which half to continue to search on that basis

Linear Search
•   The most basic
•   Very easy to implement
•   The array DOESN’T have to be sorted
•   All array elements must be visited if the search fails
•   Could be very slow                                          2
Example: Successful Linear Search

3
Example: Failed Linear Search

4
Efficiency of Linear Search

Time complexity of Linear Search algorithm
worst case: O(n)
Best case: O(1)

Time complexity: Time required to execute a program.
Space complexity: The memory (storage) required to store the
program

Big O notation: Is used to measure the time complexity
5
Binary Search
• At each step it splits the remaining array elements into two groups

• Therefore, it is faster than the linear search

• Works only on an already SORTED array

• Thus, there is a performance penalty for sorting the array

• The algorithm starts searching with the middle element.
– If the item is less than the middle element, it starts over
searching the first half of the list.
– If the item is greater than the middle element, the search starts
over starting with the middle element in the second half of the
list.
– It then continues halving the list until the item is found.
• Each iteration eliminates half of the remaining elements
6
• The Time complexity of Binary Search is O(log N)
7
Bubble Sort
Sorting
• Sorting takes an unordered collection and makes it an
ordered one. The idea of Bubble (or exchange) sort is to
scan through the list and swap each pair of adjacent
elements that are in the wrong order.

1       2      3       4        5        6
77     42       35      12      101        5

1       2       3       4        5        6
5      12      35      42       77      101
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1      2     3      4       5       6

77   42      35     12     101      5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1      2     3      4       5       6
42Swap77
42      35     12     101      5
77
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1      2     3      4       5       6

42   77 Swap77
35   35       12     101      5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1      2     3      4       5       6

42   35      77 Swap77
12    12     101      5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1      2     3      4       5       6

42   35      12     77     101      5

No need to swap
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1      2     3      4       5       6

42   35      12     77     101Swap101
5      5
"Bubbling Up" the Largest
Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1       2      3      4       5       6

42     35     12      77      5       101

Largest value correctly placed
• Notice that only the largest value is correctly placed
• All other values are still out of order
• So we need to repeat this process

1       2       3       4         5        6

42      35      12       77        5       101

Largest value correctly placed
Repeat “Bubble Up” How Many Times?
• If we have N elements…

• And if each time we bubble an element, we place it
in its correct location…

• Then we repeat the “bubble up” process N – 1
times.

• This guarantees we’ll correctly
place all N elements.
“Bubbling” All the Elements
1    2    3   4    5    6
42   35   12   77   5    101
1    2    3   4     5   6
35   12   42   5    77   101
1    2   3    4     5   6
N-1

12   35   5    42   77   101
1   2    3    4     5   6
12   5    35   42   77   101
1    2   3    4     5   6
5   12   35   42   77   101
Reducing the Number of Comparisons
1    2    3   4     5    6
77   42   35   12   101   5
1    2    3   4     5     6
42   35   12   77    5    101
1    2    3   4     5    6
35   12   42   5    77    101
1    2   3    4     5    6
12   35   5    42   77    101
1   2    3    4     5    6
12   5    35   42   77    101
Reducing the Number of Comparisons
• On the Ith “bubble up”(pass), we only need to
do MAX-I comparisons.

• For example:
– This is the 4th “bubble up”
– MAX is 6
– Thus we have 2 comparisons to do
1       2      3       4        5       6
12       35     5       42      77       101
#include <stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n;
clrscr();
printf("\n Enter the no.of elements:");
scanf("%d",&n);
printf("\n Enter the elements :");
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
bubblesort(a,n);
printf("\n The elements in sorted
order is :\n");
for(i = 0; i < n; i++)
printf(" %d ",a[i]);
getch();
}
22
Bubble Sort Code
void bubblesort (int a[ ] , int n)
{
int i, j, temp;
for ( i = 0; i < n-1; i++ ) /* controls passes through the list */
{
for ( j = 0; j <n – i-1; j++ ) /* performs adjacent comparisons */
{
if ( a[ j ] > a[ j+1 ] ) /* determines if a swap should occur */
{
temp = a[ j ];       /* swap is performed */
a[ j ] = a[ j + 1 ];
a[ j+1 ] = temp;
}
}
}
}
Selection Sort Algorithm

   Selection sort involved scanning through the list to find (or select)
the smallest element and swap it with the first element.
   The rest of the list is then search for the next smallest and swap it
with the second element.
   This process is repeated until the rest of the list reduces to one
element, by which time the list is sorted.

24
Selection Sort Algorithm – 1
Start by finding
(selecting) the
70
smallest                                     61
60
entry/element.                        52
50     45
40
30
21
20                                    15
9
10

0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
25
Selection Sort Algorithm – 2
• Start by finding
the smallest       70
entry.             60
• Swap the           50
smallest entry     40
with the first     30
entry.             20
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
26
Selection Sort Algorithm – 3
• Start by
finding the   70
smallest      60
entry.        50
• Swap the      40
smallest      30
entry with    20
the first     10
entry.        0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
27
Selection Sort Algorithm – 4
Sorted side       Unsorted side
70
• Part of the    60
array is now   50
sorted.        40
30
20
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
28
Selection Sort Algorithm – 5
Sorted side       Unsorted side
70
• Find the       60
smallest       50
element in     40
the unsorted   30
side.
20
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
29
Selection Sort Algorithm – 6
Sorted side       Unsorted side
70
• Find the       60
smallest       50
element in     40
the unsorted   30
side.
20
• Swap with      10
the front of   0
the unsorted        [1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
30
side.
Selection Sort Algorithm – 7
Sorted side       Unsorted side
70
• We have       60
increased the 50
size of the 40
sorted side 30
by one
20
element.
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
31
Selection Sort Algorithm – 8
Sorted side       Unsorted side
70
• The process    60
continues...                                         Smallest
50                                      from
40                                    unsorted

30
20
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]    [6]
[5]
32
Selection Sort Algorithm – 9
Sorted side       Unsorted side
70
• The process    60
continues...   50
40
30
20
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
33
Selection Sort Algorithm – 10
Sorted side
is bigger            Sorted side      Unsorted side
70
• The process     60
continues...    50
40
30
20
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
34
Selection Sort Algorithm – 11
• The process                      Sorted side       Unsorted side
one more           60
number to the      50
sorted side.       40
• The sorted side    30
has the smallest   20
numbers,           10
arranged from      0
small to large.         [1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
35
Selection Sort Algorithm – 12
• We can stop                            Sorted side      Unsorted sid
when the          70
unsorted side     60
has just one      50
number, since     40
that number       30
must be the       20
largest number.
10
0
[1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
36
Selection Sort Algorithm – 13
• The array is
now sorted.      70
• We repeatedly    60
selected the     50
smallest         40
element, and     30
moved this       20
element to the   10
front of the     0
unsorted side.        [1]
[0]   [2]
[1]   [3]
[2]   [4]
[3]   [5]
[4]   [6]
[5]
37
Selection Sort Algorithm –
Analysis
• Running time
• Find the smallest item (this is O(n))
• Swap (O(n))
– O(n2)
#include "stdio.h"            void main()
#include "conio.h“            {
void ssort(int a[] , int n)   int a[100],i,n;
{ int i , j,t,min,pos;       clrscr();
printf("\n Enter the no.of elements:");
for(i=0;i<n-1;i++)            scanf("%d",&n);
{                             printf("\n Enter the elements :");
min =a[i];                for(i = 0; i < n; i++)
pos=i;                            scanf("%d",&a[i]);
for(j=i+1;j<n;j++)            ssort(a,n);
printf("\n The elements in sorted
{                             order is :\n");
if(a[j]<min)                  for(i = 0; i < n; i++)
{t=a[j];                              printf(" %d ",a[i]);
a[j]=a[pos];                  getch();
a[pos]=t;                     }
}}
}
}
39
Insertion Sort
• Insertion sort works the same way as playing
cards.
– Out of the pile of unsorted cards that were dealt to
you, you pick up a card and place it in your hand in
the correct position relative to the cards you’re
Insertion Sort
• Strategy:
– Insertion of an element in proper order:
– Begin with a sequence E of n elements in arbitrary order
– Initially assume the sorted segment contains first element
– Let x be the next element to be inserted in sorted segment, pull x
“out of the way”, leaving a vacancy
– repeatedly compare x to the element just to the left of the vacancy,
and as long as x is smaller, move that element into the vacancy,
– else put x in the vacancy,
– repeat the next element that has not yet examined.

41
7

5   7
5       7

5   6       7

5   6       7   9

5   6       7   8   9
Insertion Sort
Look at 2nd item - 5.
Compare 5 to 7.
1
7       5                     5 is smaller, so move 5
to temp, leaving
v      an empty slot in
7                      5       position 2.
Move 7 into the empty
slot, leaving position 1
7
>                           open.
2
5       7                 3     Move 5 into the open
<                           position.
Insertion Sort (con’t)
5   7       6   K                   Look at next item - 6.
Compare to 1st - 5.
1            6 is larger, so leave 5.
5   7                          Compare to next - 7.
6 is smaller, so move
v                 6 to temp, leaving an
5   7                  6            empty slot.
Move 7 into the empty
slot, leaving position 2
5           7
>                           open.
2
6       7                         Move 6 to the open
5                          3
<                             2nd position.
46
#include "stdio.h"             void main()
#include "conio.h"             {
void isort(int a[] , int n)    int a[100],i,n;
{ int i , j,temp;             clrscr();
printf("\n Enter the no.of elements:");
for(i=1;i<n;i++)               scanf("%d",&n);
{                              printf("\n Enter the elements :");
temp =a[i];                for(i = 0; i < n; i++)
j=i-1;                             scanf("%d",&a[i]);
while( (j>=0)&& (temp<a[j]))   isort(a,n);
printf("\n The elements in sorted
{                              order is :\n");
a[j+1]=a[j];                   for(i = 0; i < n; i++)
j=j-1; }                               printf(" %d ",a[i]);
getch();
a[j+1]=temp;                   }

}
}
47
Insertion Sort – Analysis
Worst case:
– O(n2)
best case: (sorted list)
O(n)
Mergesort
Divide and Conquer
• Divide and Conquer cuts the
problem in half each time, but uses
the result of both halves:
– cut the problem in half until the
problem is trivial
– solve for both halves
– combine the solutions
Mergesort
• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until the sub-
arrays only contain one element
• Merge the sub-problem solutions together:
– Compare the sub-array’s first elements
– copy the smallest element it into the result array
– Continue the process until all elements have been
put into the result array
Merge Sort: Idea

Divide into
A:     FirstPart           SecondPart
two halves
Recursively
sort
FirstPart                                    SecondPart

Merge

A is sorted!
98 23 45 14   6 67 33 42
98 23 45 14   6 67 33 42

split   98 23 45 14     6 67 33 42
98 23 45 14   6 67 33 42

98 23 45 14      6 67 33 42

98 23   45 14
split
98 23 45 14      6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14

98     23          split
98 23 45 14   6 67 33 42

98 23 45 14     6 67 33 42

98 23       45 14

98     23

Merge
98 23 45 14   6 67 33 42

98 23 45 14     6 67 33 42

98 23       45 14

98     23

23

Merge
98 23 45 14   6 67 33 42

98 23 45 14     6 67 33 42

98 23       45 14

98     23

23 98

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14      split

23 98
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14 45

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14 45

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14 45

14

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14 45

14 23

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14 45

14 23 45

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14

98     23   45   14

23 98       14 45

14 23 45 98

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67   33 42

98     23   45   14                   split

23 98       14 45

14 23 45 98
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67       split

23 98       14 45

14 23 45 98
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67

23 98       14 45

14 23 45 98           Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67

23 98       14 45      6

14 23 45 98           Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67

23 98       14 45      6 67

14 23 45 98           Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67   33   42

23 98       14 45      6 67               split

14 23 45 98
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67   33   42

23 98       14 45      6 67

14 23 45 98                    Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67   33   42

23 98       14 45      6 67     33

14 23 45 98                    Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67   33   42

23 98       14 45      6 67     33 42

14 23 45 98                    Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67      33 42

98     23   45   14    6   67    33   42

23 98       14 45      6 67      33 42

14 23 45 98

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67      33 42

98     23   45   14    6    67   33   42

23 98       14 45      6 67      33 42

14 23 45 98            6

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67      33 42

98     23   45   14    6   67    33   42

23 98       14 45      6 67      33 42

14 23 45 98            6 33

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67      33 42

98     23   45   14    6   67    33   42

23 98       14 45      6 67       33 42

14 23 45 98            6 33 42

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67      33 42

98     23   45   14    6   67    33   42

23 98       14 45      6 67      33 42

14 23 45 98            6 33 42 67

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23         45 14    6 67       33 42

98     23   45   14    6     67   33    42

23 98         14 45        6 67   33 42

14 23 45 98               6 33 42 67

6 14

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6 14 23

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6 14 23 33

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6 14 23 33 42

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6 14 23 33 42 45

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6 14 23 33 42 45 67

Merge
98 23 45 14     6 67 33 42

98 23 45 14           6 67 33 42

98 23       45 14      6 67       33 42

98     23   45   14    6     67   33    42

23 98       14 45          6 67   33 42

14 23 45 98               6 33 42 67

6 14 23 33 42 45 67 98

Merge
98 23 45 14     6 67 33 42

98 23 45 14       6 67 33 42

98 23       45 14      6 67     33 42

98     23   45   14    6   67   33   42

23 98       14 45      6 67     33 42

14 23 45 98            6 33 42 67

6 14 23 33 42 45 67 98
98 23 45 14   6 67 33 42

6 14 23 33 42 45 67 98
Algorithm of mergesort
• Contains two functions
•    One function is used for splitting array into two
halves        (merge sort())
•     Other function is used to merge these arrays into
single array (merge())

Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves. (call merge function)
Merge Sort: (split function)

void mergesort(int a[],int low,int high)
{
int mid;
if(low < high)   {         Recursive Call
mid = (low + high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid); } }
Algorithm for merge function

Merge(Passed two arrays)

Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output
array)
Merge function

void merge(int a[],int l,int h,int m)
{
int c[100],i,j,k;
i = l; j = m + 1; k = l;
while(i <= m && j <= h)           {
if(a[i] < a[j]){
c[k] = a[i]; i++; k++; }
Else { c[k] = a[j]; j++; k++; } }
while(i <= m)        c[k++] = a[i++];
while(j <= h)        c[k++] = a[j++];
for(i = l; i < k; i++) a[i] = c[i]; }
Merge-Sort: Merge
Sorted

A:

merge

Sorted            Sorted

L:                R:
Merge-Sort: Merge Example

A:

L:    1    2   6   8   R: 3   4   5   7
Merge-Sort: Merge Example

A:
1
3     5   15 28    10 14

k=0

L:                           R:
3
1      2 6 8
15 28 30           3
6      4 5 7
10 14 22

i=0                      j=0
Merge-Sort: Merge Example

A:
1      2
5    15 28 30   6   10 14

k=1

L:                               R:
3
1    2
5      15 28
6 8                6
3      4 5 7
10 14 22

i=1                        j=0
Merge-Sort: Merge Example

A:
1   2      3
15 28 30   6   10 14

k=2

L:                             R:
1    2   6      8             6
3      4 5 7
10 14 22

i=2                  j=0
Merge-Sort: Merge Example

A:
1   2     3   4     6   10 14

k=3

L:                            R:
1    2   6     8             6
3    4 5 7
10 14 22

i=2                     j=1
Merge-Sort: Merge Example

A:
1   2     3   4   5     6   10 14

k=4

L:                                R:
1    2    6    8                 6
3    4 5 7
10 14 22

i=2                             j=2
Merge-Sort: Merge Example

A:
1   2     3   4   5   6     10 14

k=5

L:                               R:
1    2   6     8                 6
3    4 5 7
10 14 22

i=2                               j=3
Merge-Sort: Merge Example

A:
1   2   3     4   5   6    7    14

k=6

L:                              R:
1    2   6    8                6
3      4 5 7
10 14 22

i=3                           j=3
Merge-Sort: Merge Example

A:
1   2   3     4   5   5    7    8
14

k=7

L:                              R:
1
3    2
5   15 28
6 8                  6
3     4 5 7
10 14 22

i=3                               j=4
Merge-Sort: Merge Example

A:
1   2   3   4     5   6    7   8

k=8

L:                              R:
1
3    2
5   15 28
6 8                  6
3     4 5 7
10 14 22

i=4                           j=4
Divide
Merge-Sort Execution Example
6     2   8   4   3 3   7 7   5 5   11
Merge-Sort Execution Example
Recursive call , divide
3   7   5   1

6     2    88       4
4
Merge-Sort Execution Example
Recursive call , divide
3   7   5   1

8       4

6     2
2
Merge-Sort Execution Example
Recursive call , base case
3   7   5   1

8     4

2

6
Merge-Sort Execution Example
Recursive call return
3   7   5   1

8      4

6     2
Merge-Sort Execution Example
Recursive call , base case
3   7   5   1

8     4

6

2
Merge-Sort Execution Example
Recursive call return
3   7   5   1

8      4

6     2
Merge-Sort Execution Example
Merge
3   7   5   1

8   4

2   6
Merge-Sort Execution Example
Recursive call return
3   7   5   1

2     6    8      4
Merge-Sort Execution Example
Recursive call , divide
3   7   5   1

2     6

8      4
Merge-Sort Execution Example
Recursive call, base case
3   7   5   1

2    6

4

8
Merge-Sort Execution Example
Recursive call return
3   7   5   1

2     6

8     4
Merge-Sort Execution Example
Recursive call, base case

2    6

8

4
Merge-Sort Execution Example
Recursive call return
3   7   5   1

2     6

8     4
Merge-Sort Execution Example
merge
3   7   5   1

2   6

4   8
Merge-Sort Execution Example
Recursive call return
3   7   5   1

2     6    4      8
Merge-Sort Execution Example
merge
3   7   5   1

2   4   6   8
Merge-Sort Execution Example
Recursive call return
2     4     6     8   3   7   5   1
Merge-Sort Execution Example
Recursive call
2     4    6   8

3   7   5   1
Merge-Sort Execution Example

2   4   6   8

1   3   5   7
Merge-Sort Execution Example
Recursive call return
2     4     6     8   1   3   5   7
Merge-Sort Execution Example
merge
1   2   3   4   5   6   7   8
#include "stdio.h"
#include "conio.h"
void merge(int [],int,int,int);
void mergesort(int a[],int low,int high){
int mid;
if(low < high) {                     void main()
mid = (low + high)/2;                {
mergesort(a,low,mid);                         int i,n,a[100];
mergesort(a,mid+1,high);                      clrscr();
merge(a,low,high,mid); } }                    printf("\n Enter the size of the array :");
void merge(int a[],int l,int h,int m){         scanf("%d",&n);
int c[100],i,j,k;                           printf("\n Enter the elements :\n");
i = l; j = m + 1; k = l;                     for(i = 0; i < n; i++)
while(i <= m && j <= h) {                              scanf("%d",&a[i]);
if(a[i] < a[j]){                             mergesort(a,0,n-1);
c[k] = a[i]; i++; k++; }                     printf("\n Elements in sorted order :\n");
Else { c[k] = a[j]; j++; k++; } }              for(i = 0; i < n; i++)
while(i <= m) c[k++] = a[i++];                          printf("%5d",a[i]);
while(j <= h)       c[k++] = a[j++];          getch();
for(i = l; i < k; i++) a[i] = c[i]; }                                            131
}
Merge-Sort Analysis
• Time, divide
n                                        n

n/2                   n/2                    2 × n/2 = n

log n levels

n/4         n/4       n/4         n/4              4 × n/4 = n

n/2 × 2 = n
Total time for divide: n log n
Merge-Sort Analysis
• Time, merging
n                                       cn

n/2                   n/2                   2 × cn/2 = n

log n levels

n/4         n/4       n/4         n/4              4 × cn/4 = n

n/2 × 2c = n
Total time for merging: cn log n

• Total running time: order of nlogn
• Total Space: order of n
Mergesort
Quicksort

 Developed by C.A.R Hoare.
 Uses divide and conquer method.
 Select a pivot (partitioning element)
 Rearrange the list so that all the elements in the positions before
the pivot are smaller than or equal to the pivot and those after the
pivot are larger than the pivot
 Exchange the pivot with the last element in the first (i.e., ≤)
sublist – the pivot is now in its final position
 Sort the two sublists recursively

p

A[i]≤p                           A[i]>p          135
Recursive implementation with the left most array entry selected as
the pivot element.

161
#include<stdio.h>
void quiksort(int [] , int ,int);
void main()
{
int i,n,a[100];
clrscr();
printf("\n Enter the size of the array :");
scanf("%d",&n);
printf("\n Enter the elements :\n");
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
quiksort(a,0,n-1);
printf("\n The elements in sorted order is :\n");
for(i = 0; i < n; i++)
printf("%5d",a[i]);
getch();
}
void quiksort(int a[] , int lb , int ub)
{
int i , j ,temp, pivot=lb;
i=lb ; j=ub;
do                                        temp=a[j];
{                                          a[j]=a[pivot];
while((a[i]<=a[pivot]) && (i<=ub))        a[pivot]=temp;
i++;                              if(lb<j-1)
while(a[j]>a[pivot])           quiksort(a,lb,j-1);
j--;                              if(j+1<ub)
if(i<j)                        quiksort(a,j+1,ub);
{                                      }
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
while(i<j);
The Time Analysis of quicksort:

The time requirement to sort N elements involves 3 components:

i)   Time taking for partitioning the array , which is roughly
proportional to N
ii) Time required for sorting lower sub array
iii) Time required for sorting upper sub array

Assume there are k elements in the lower sub array:

T(N) =T(k) + T(N-1-k)+cN

where c is proportional constant.
Best case analysis :
The best case be viewed when the partition takes place exactly at
half the original array.
Thus,
T(N)=T(N/2)+T(N/2)+CN
T(N)/N = 2 T(N/2)/ N +C
T(N) /N = T(N/2) /(N/2) +C
Proceeding similarly for recursive calls we have
T(2)/2 =T(1) /1 + mC

Assume that total number of elements N is a power of 2, such that
N=2m
m= logN
T(N) /N = T(1)/1 + c logN
T(N) = N+c NlogN
Therefore time complexity in this case is O(NlogN)
Worst case:
If the array is already sorted , treated as worst case.
Then , while portioning the array , will get any one sub array
either lower sub array or upper but not both.

Thus the time complexity is O(n2)

Note:
Among all sorting techniques , the quick sort is an efficient
algorithm
Time complexities of different sorting algorithms:

Best case      Avg case        Worst case

Bubble sort      O(n2)          O(n2)           O(n2)

Selection sort   O(n2)          O(n2)           O(n2)

Insertion sort   O(n)           O(n2)           O(n2)

Merge sort       O(nlogn)       O(nlogn)        O(nlogn)

Quick sort       O(nlogn)       O(nlogn)        O(n2)
Review of Algorithms
• Selection Sort
– An algorithm which orders items by repeatedly looking through
remaining items to find the least one and moving it to a final
location
• Bubble Sort
– Sort by comparing each adjacent pair of items in a list in turn,
swapping the items if necessary, and repeating the pass
through the list until no swaps are done
• Insertion Sort
– Sort by repeatedly taking the next item and inserting it into the
final data structure in its proper order with respect to items