You are on page 1of 19

Chapter 2 Searching and Sorting

Sorting: ordering a list of values

Types of sorting

1. Insertion sort.
2. Merge Sort. 
3. Quick Sort.
4. Radix Sort.
5. Selection sort
6. Bubble sort

Searching: finding the position of a value within a list

Types of searching

1. Linear search
2. Binary search

Binary search

Binary search is implemented using following steps...

Step 1 - Read the search element from the user.

Step 2 - Find the middle element in the sorted list.

Step 3 - Compare the search element with the middle element in the sorted list.

Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.

Step 5 - If both are not matched, then check whether the search element is smaller or larger
than the middle element.

Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for
the left sublist of the middle element.

Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for
the right sublist of the middle element.

Step 8 - Repeat the same process until we find the search element in the list or until sublist
contains only one element.

Marathwada Mitra Mandal’s Polytechnic Page 1


Chapter 2 Searching and Sorting

Step 9 - If that element also doesn't match with the search element, then display "Element
is not found in the list!!!" and terminate the function.

Example:-

Marathwada Mitra Mandal’s Polytechnic Page 2


Chapter 2 Searching and Sorting

Marathwada Mitra Mandal’s Polytechnic Page 3


Chapter 2 Searching and Sorting

#include<stdio.h>
#include<conio.h>
void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d integer values in Assending order\n", size);
for (i = 0; i < size; i++)
scanf("%d",&list[i]);
printf("Enter value to be search: ");
scanf("%d", &sElement);
first = 0;
last = size - 1;
middle = (first+last)/2;
while (first <= last) {
if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement) {
printf("Element found at index %d.\n",middle);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Element Not found in the list.");
getch();

Marathwada Mitra Mandal’s Polytechnic Page 4


Chapter 2 Searching and Sorting

Describe working of linear search with example.

In linear search, search element is compared with each element from the list in a sequence.
Comparison starts with first element from the list and continues till number is found or
comparison reaches to the last element of the list. As each element is checked with search
element, the process of searching requires more time. Time complexity of linear search is O
(n) where n indicates number of elements in list.

Linear search on sorted array:-On sorted array search takes place till element is found or
comparison reaches to an element greater than search element.

Example:- Using array representation

Input list 10, 20, 30, 40, 50 and Search element 30, Index =0

Marathwada Mitra Mandal’s Polytechnic Page 5


Chapter 2 Searching and Sorting

Linear search Program

#include <stdio.h>
 
int main()
{
  int array[100], search, c, n;
 
  printf("Enter number of elements in array\n");
  scanf("%d", &n);
 
  printf("Enter %d integer(s)\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  printf("Enter a number to search\n");
  scanf("%d", &search);
 

Marathwada Mitra Mandal’s Polytechnic Page 6


Chapter 2 Searching and Sorting

  for (c = 0; c < n; c++)
  {
    if (array[c] == search)    /* If required element is found */
    {
      printf("%d is present at location %d.\n", search, c+1);
      break;
    }
  }
  if (c == n)
    printf("%d isn't present in the array.\n", search);
 
  return 0;
}
Output of program:

Marathwada Mitra Mandal’s Polytechnic Page 7


Chapter 2 Searching and Sorting

Bubble Sort

1. Starting from the first index, compare the first and the second elements.If the first
element is greater than the second element, they are swapped.

Now, compare the second and the third elements. Swap them if they are not in order.
The above process goes on until the last element.

2. The same process goes on for the remaining iterations. After each iteration, the
largest element among the unsorted elements is placed at the end.

In each iteration, the comparison takes place up to the last unsorted element.

The array is sorted when all the unsorted elements are placed at their correct
positions.

Marathwada Mitra Mandal’s Polytechnic Page 8


Chapter 2 Searching and Sorting

   

Marathwada Mitra Mandal’s Polytechnic Page 9


Chapter 2 Searching and Sorting

 
#include <stdio.h>
 
int main()
{
  int array[100], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0 ; c < n - 1; c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 

Marathwada Mitra Mandal’s Polytechnic Page 10


Chapter 2 Searching and Sorting

  for (c = 0; c < n; c++)
     printf("%d\n", array[c]);
 
  return 0;
}
Output of program:

Selection Sort

#include <stdio.h>
 
int main()
{
  int array[100], n, c, d, position, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0; c < (n - 1); c++)
  {
    position = c;
  

Marathwada Mitra Mandal’s Polytechnic Page 11


Chapter 2 Searching and Sorting

    for (d = c + 1; d < n; d++)
    {
      if (array[position] > array[d])
        position = d;
    }
    if (position != c)
    {
      swap = array[c];
      array[c] = array[position];
      array[position] = swap;
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for (c = 0; c < n; c++)
    printf("%d\n", array[c]);
 
  return 0;
}
Output of program:

Marathwada Mitra Mandal’s Polytechnic Page 12


Chapter 2 Searching and Sorting

Example:

Insertion Sort

1. The first element in the array is assumed to be sorted. Take the second element
and store it separately in key.

Compare key with the first element. If the first element is greater than key, then key is

Marathwada Mitra Mandal’s Polytechnic Page 13


Chapter 2 Searching and Sorting

placed in front of the first elemet.

2. Now, the first two elements are sorted.

Take the third element and compare it with the elements on the left of it. Placed it just
behind the element smaller than it. If there is no element smaller than it, then place it at
the begining of the array.

Marathwada Mitra Mandal’s Polytechnic Page 14


Chapter 2 Searching and Sorting

3. In a similar way, place every unsorted element at its correct position.

Marathwada Mitra Mandal’s Polytechnic Page 15


Chapter 2 Searching and Sorting

#include <stdio.h>
 
int main()
{
  int n, array[1000], c, d, t;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 1 ; c <= n - 1; c++) {
    d = c;
 
    while ( d > 0 && array[d-1] > array[d]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;
 
      d--;
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for (c = 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }
 
  return 0;
}
Output of program:

Marathwada Mitra Mandal’s Polytechnic Page 16


Chapter 2 Searching and Sorting

Quick Sort

Implementation :

Select the first element of array as the pivot element First, we will see how the partition of
the array takes place around the pivot. 

 
In the implementation below, the following components have been used: Here, A[] = array
whose elements are to be sorted

start: Leftmost position of the array

end: Rightmost position of the array

Marathwada Mitra Mandal’s Polytechnic Page 17


Chapter 2 Searching and Sorting

Merge Sort

To understand merge sort, we take an unsorted array as the following −

Marathwada Mitra Mandal’s Polytechnic Page 18


Chapter 2 Searching and Sorting

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 −

Now we should learn some programming aspects of merge sorting.

Marathwada Mitra Mandal’s Polytechnic Page 19

You might also like