Searching and Sorting 573
Enter 10 names:
ashish bhaskar deepak dinesh gajanan ganesh goval govind raj ramesh
Enter the name to search:amit
At starting beg is 1, mid is 5 & end is 10
mid is 2 & end is 4
mid is 1 & end is 1
mid is 0 & end is 0
The number amit is not traced
16.5 SORTING
As discussed in the introduction, sorting is a process in which records are arranged in ascending
or descending order. In real life we come a cross several examples of such sorted information. For
example, in a telephone directory the names of the subscribers and their phone numbers are written
in ascending alphabets. The records of the list of these telephone holders are to be sorted by their
names. By using this directory, we can find the telephone number and address of the subscriber
very easily. The sort method has great impact on data structures in our daily life.
For example, consider the five numbers 5, 9, 7, 4,1.
The above numbers can be sorted in ascending or descending order.
The representation of these numbers in
Ascending order (0 to n): 1 4 5 7 9
Descending order (n to 0): 9 7 5 4 1
Similarly, alphabets can be sorted as given below.
Consider the alphabets B, A, D, C, E. These are sorted in
Ascending order (A to Z): A B C D E
Descending order (Z to A): E D C B A
16.6 INSERTION SORT
In insertion sort an element is inserted at the appropriate place. For example, consider an array of
n elements. In this type, swapping of elements is done without taking any temporary variable. The
greater numbers are shifted towards the end of the array and smaller are shifted at beginning.
16.4 Write a program to sort array elements using insertion sort.
#include <stdio.h>
#include cconio.h>
main()
{
int nums[2 0 ],p=0, q = l, r , num;
c l r s c r ( ) ;
p r in t f ( "Enter number of elements: " ) ;
sc a n f( "%d", &num);
p rin t f ("n Enter %d elements: n,num);
while (pcnum)
scanf("% d", inum s[p++]) ;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232