You are on page 1of 58

UNIT V

SORTING
Sorting

Sorting refers to arranging data in a particular format.

Sorting algorithm specifies the way to arrange data in a

particular order.

Most common orders are in numerical or lexicographical

order.
Sorting

What is Sorting?
Sorting is a process of ordering or placing a list of elements

from a collection in some kind of order.

Sorting can be done in ascending and descending order.

It arranges the data in a sequence which makes searching

easier.
Sorting Techniques

Sorting technique depends on two parameters.

1. Execution time of program that means time taken for

execution of program(Time Complexity).

2. Space that means space taken by the program(Space

Complexity).
Sorting techniques are differentiated by their efficiency
and space requirements.
The complexity of sorting algorithm

1. Time Complexity : Execution time of program that means

time taken for execution of program.

2. Space Complexity : Memory Space taken by the program.


Types of Sorting
The techniques of sorting can be divided into two categories.
These are: 1. Internal Sorting 2. External Sorting
Internal Sorting: If all the data that is to be sorted can be
adjusted at a time in the main memory, the internal sorting
method is being performed.
External Sorting: When the data that is to be sorted cannot be
accommodated in the memory at the same time and some has
to be kept in auxiliary memory such as hard disk, floppy disk,
magnetic tapes etc, then external sorting methods are
performed.
Efficiency of Sorting Technique
To get the amount of time required to sort an array of 'n' elements by a
particular method, the normal approach is to analyze the method to find the
number of comparisons) required by it.
Various sorting techniques are analyzed in various cases and named these
cases as follows:
Best case
Worst case
Average case
Hence, the result of these cases is often a formula giving the average time
required for a particular sort of size 'n.' Most of the sort methods have time

requirements that range from O(nlog n) to O(n2).


Types of Sorting Technique

Internal Sorting : External Sorting :


Insertion Sort merge sort

Selection sort Quick Sort

Bubble sort Heap Sort


 Insertion Sort Algorithm

Insertion Sort

Insertion sort is a simple sorting algorithm that works similar

to the way you sort playing cards in your hands.

 The array is virtually split into a sorted and an unsorted

part.

Values from the unsorted part are picked and placed at the

correct position in the sorted part.


 Insertion Sort Algorithm

Characteristics of Insertion Sort:

This algorithm is one of the simplest algorithm with simple

implementation

Basically, Insertion sort is efficient for small data values

Insertion sort is adaptive in nature, i.e. it is appropriate for

data sets which are already partially sorted.


 Insertion Sort Algorithm

Working of Insertion Sort algorithm:

Consider an example: arr[]: {12, 11, 13, 5, 6}

  12       11       13       5       6   


 Insertion Sort Algorithm
First Pass:
Initially, the first two elements of the array are compared
in insertion sort.
   12      11      13      5      6   
Here, 12 is greater than 11 hence they are not in the
ascending order and 12 is not at its correct position. Thus,
swap 11 and 12.
So, for now 11 is stored in a sorted sub-array.
   11      12      13      5      6   
 Insertion Sort Algorithm
Second Pass:
Now, move to the next two elements and compare
them
   11      12      13      5      6   
Here, 13 is greater than 12, thus both elements
seems to be in ascending order, hence, no swapping
will occur. 12 also stored in a sorted sub-array along
with 11
Third Pass:
Now, two elements are present in the sorted sub-
array which are 11 and 12
Moving forward to the next two elements which are
13 and 5
   11      12      13      5      6   Both 5 and 13 are not
present at their correct place so swap them
   11      12      5      13      6   After swapping, elements
12 and 5 are not sorted, thus swap again
   11      5      12      13      6   Here, again 11 and 5 are
not sorted, hence swap again
   5      11      12      13      6   here, it is at its correct
position
Fourth Pass:
Now, the elements which are present in the sorted sub-array are 5,
11 and 12
Moving to the next two elements 13 and 6
   5      11      12      13      6   
Clearly, they are not sorted, thus perform swap between both
   5      11      12      6      13   Now, 6 is smaller than 12, hence, swap
again
   5      11      6      12      13   Here, also swapping makes 11 and 6
unsorted hence, swap again
   5      6      11      12      13  
 Finally, the array is completely sorted.
Insertion Sort Algorithm
for i = 1 to size-1 Pass 1
{ 12      11      13      5      6  

temp = a[i]; i=1 j = 0 Skip the while loop


a[1] =12
j = i -1;
12      11      13      5      6  
while ((temp < a[j]) && (j > 0))
{
a[j + 1 ] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
Insertion Sort Algorithm
for i = 1 to size-1
Pass 2
{
temp = a[i]; 12      11      13      5      6  
j = i -1;
i =2 j = 1 temp = a[2] =
while ((temp < a[j]) && (j > 0))
{ 11 < a[ j ] =12 and j>0
a[j + 1 ] = a[j];
a[2] = 12
j = j - 1;
j =0 Skip the while loop
}
a[j + 1] = temp; a[1] = 11
}
11      12      13      5      6
Insertion Sort Algorithm Pass 3
for i = 1 to size-1
{ 11      12      13      5      6
temp = a[i]; i =3 j = 2 temp = a[3] = 13 >
j = i -1;
a[ j ] =12 Skip the while loop
while ((temp < a[j]) && (j > 0))
{ a[3] = 13
a[j + 1 ] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
Insertion Sort Algorithm Pass 4

for i = 1 to size-1 11      12      13      5      6


{
i =4 j = 3 temp = a[4] = 5 < a[ j ]
temp = a[i];
=13
j = i -1;
a[4] = 13
while ((temp < a[j]) && (j > 0))
{ j =2 Repeat the while loop a[j] = 12
a[j + 1 ] = a[j]; a[3] = 12
j = j - 1;
j=1 Repeat the while loop a[j] = 11
}
a[2] = 11
a[j + 1] = temp;
j=0
}
Insertion Sort Algorithm
Pass 5
for i = 1 to size-1 5 11      12      13 6
{
i =5 j = 4 temp = a[5] = 6 <
temp = a[i]; a[ j ] =13
j = i -1; a[5] = 13
while ((temp < a[j]) && (j > 0)) j =3 Repeat the while loop a[j] =
{ 12
a[j + 1 ] = a[j]; a[4] = 12
j =2 Repeat the while loop a[j] =
j = j - 1;
11
}
a[3] = 11
a[j + 1] = temp; j=1 Skip the while loop
} a[2] = 6
void insertionSort(int arr[], int n)
{
    int i, temp, j;
    for (i = 1; i < n; i++)
    {
         temp = arr[i];
        j = i - 1;
 
         while (j >= 0 && arr[j] > temp )
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = temp ;
    }
}
Selection Sort Algorithm
 Selection Sort algorithm is used to arrange a list of elements in a particular

order (Ascending or Descending).

 In selection sort, the first element in the list is selected and it is compared

repeatedly with all the remaining elements in the list.

 If any element is smaller than the selected element (for Ascending order),

then both are swapped so that first position is filled with the smallest element

in the sorted order.

 Next, we select the element at a second position in the list and it is compared

with all the remaining elements in the list.

 If any element is smaller than the selected element, then both are swapped.
The selection sort algorithm is performed using the
following steps...
Step 1 - Select the first element of the list (i.e., Element at
first position in the list).
Step 2: Compare the selected element with all the other
elements in the list.
Step 3: In every comparision, if any element is found smaller
than the selected element (for Ascending order), then both
are swapped.
Step 4: Repeat the same procedure with element in the next
position in the list till the entire list is sorted.
Selection Sort Algorithm

for(i=0; i<size; i++)


{
for(j=i+1; j<size; j++)
{
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
Bubble sort

 Bubble sort is a simple sorting algorithm.

 This sorting algorithm is comparison-based algorithm in

which each pair of adjacent elements is compared and the

elements are swappedif they are not in order.


Bubble 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 i = 1 to size
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
MERGE SORT
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.
Merge Sort Algorithm
Algorithm

• In the following algorithm, arr is the given array, beg is

the starting element, and end is the last element of the

array.

• Beg = 1

• End = n = 8
MERGE_SORT(arr, beg, end)  

  if beg < end  

set mid = (beg + end)/2  

MERGE_SORT(arr, beg, mid)  

MERGE_SORT(arr, mid + 1, end)  

MERGE (arr, beg, mid, end)  

end of if  

  END MERGE_SORT  
The important part of the merge sort is the MERGE function.

This function performs the merging of two sorted sub-arrays

that are A[beg…mid] and A[mid+1…end], to build one

sorted array A[beg…end].

So, the inputs of the MERGE function are A[], beg,

mid, and end.
The implementation of the MERGE function is given as
follows -

/* Function to merge the subarrays of a[] */  

void merge(int a[], int beg, int mid, int end)    

{    

    int i, j, k;  

    int n1 = mid - beg + 1;    

    int n2 = end - mid;    

      

    int LeftArray[n1], RightArray[n2]; //temporary arrays  
/* copy data to temp arrays */  

    for (int i = 0; i < n1; i++)    

    LeftArray[i] = a[beg + i];    

    for (int j = 0; j < n2; j++)    

    RightArray[j] = a[mid + 1 + j];   
 i = 0, /* initial index of first sub-array */  

    j = 0; /* initial index of second sub-array */   

    k = beg;  /* initial index of merged sub-array */  

          while (i < n1 && j < n2)    
    {    

        if(LeftArray[i] <= RightArray[j])    

        {    

            a[k] = LeftArray[i];    

            i++;    

        }    
  else    
        {    
            a[k] = RightArray[j];    
            j++;    
        }    
        k++;    
    }    
    while (i<n1)    
    {    
        a[k] = LeftArray[i];    
        i++;    
        k++;    
    }    
  while (j<n2)    
    {    
        a[k] = RightArray[j];    
        j++;    
        k++;    
    }    
}    
According to the merge sort, first divide the given array into two
equal halves. Merge sort keeps dividing the list into equal parts until
it cannot be further divided.
As there are eight elements in the given array, so it is divided into two
arrays of size 4.
Now, again divide these two arrays into halves.
As they are of size 4, so divide them into new
arrays of size 2.

Now, again divide these arrays to get the


atomic value that cannot be further divided.

Now, combine them in the same manner they were broken.


In combining, first compare the element of each array and then combine them
into another array in sorted order.
So, first compare 12 and 31, both are in sorted positions. Then compare 25 and 8,
and in the list of two values, put 8 first followed by 25. Then compare 32 and 17,
sort them and put 17 first followed by 32. After that, compare 40 and 42, and
place them sequentially.
In the next iteration of combining, now compare the arrays with
two data values and merge them into an array of found values in
sorted order.

Now, there is a final merging of the arrays. After the final merging of above
arrays, the array will look like -
What is Searching?

 Searching is the process of finding a given value position in a

list of values.

 It decides whether a search key is present in the data or not.

 It is the algorithmic process of finding a particular item in a

collection of items.
Searching Techniques
To search an element in a given array, it can be done in following
ways:

1. Sequential Search/ Linear Search

2. Binary Search
Linear Search
• Linear search is a very basic and simple search algorithm. In
Linear search, we search an element or value in a given array
by traversing the array from the starting, till the desired
element or value is found.
• It compares the element to be searched with all the elements
present in the array and when the element is matched
successfully, it returns the index of the element in the array,
else it return -1.
• Linear Search is applied on unsorted or unordered lists, when
there are fewer elements in a list.
Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is checked and if
a match is found then that particular item is returned, otherwise the search
continues till the end of the data collection.
Linear Search Algorithm

Step 1 − Read the number to search ,total element in the list ie


Key and n
Step 2 − For i = 1 to n
Step 3 - If Key = a[i] then Print the Number is present at index
i and Exit .
else Continue for loop.
Step 4 - Print the Number is not present
Step 3 - Exit
Features of Linear Search Algorithm
 It is used for unsorted small list of elements.

 It has a very simple implementation.


Binary Search
Binary Search is used with sorted array or list.
In binary search, we follow the following steps:
1. We start by comparing the element to be searched with the
element in the middle of the list/array.
2. If we get a match, we return the index of the middle element.
3. If we do not get a match, we check whether the element to be
searched is less or greater than in value than the middle
element.
4. If the element/number to be searched is greater in value than
the middle number, then we pick the elements on the right side
of the middle element(as the list/array is sorted, hence on the
right, we will have all the numbers greater than the middle
number), and start again from the step 1.
5. If the element/number to be searched is lesser in value than the
middle number, then we pick the elements on the left side of the
Features of Binary Search Algorithm
 Binary Search is useful when there are large number of
elements in an array and they are sorted.
 So a necessary condition for Binary search to work is that the
list/array should be sorted.
 It has a simple implementation.
The element to be searched is 25

we compare the key element with the mid element. So (25 == 25), hence we have found the key at location
[mid] = 6.
Thus we repeatedly divide the array and by comparing the key element with the mid, we decide in which half
to search for the key. Binary search is more efficient in terms of time and correctness and is a lot faster too.

You might also like