Linear Data Structure 477
Enter an element to remove: 9
58423470
Explanation In the above program, elements are entered by the user. The user can enter maximum
eight elements. The element which is to be removed is also entered. The while loop calculates the
position of the element in the list. The second for loop shifts all the elements next to the specified
element one position up towards the beginning of the array. The element which is to be deleted is
replaced with successive elements. The last element is replaced with zero. Thus, the empty spaces
are generated at the end of the array.
14.9 SO R TIN G
Sorting is a process in which records are arranged in ascending or descending ofrder. In real life
problems, we come across several examples of such sorted information. For example, the telephone
directory— in it, names of the subscribers and the phone numbers are written according to ascending
alphabets. The records of the list of these telephone holders are to be sorted by the name of the
holder. By using this directory, we can find the telephone number of any subscriber easily. Sort
method has great importance in data structures. Different sort methods are used in sorting
elements/records.
The bubble sort is an example of exchange sort. In this method, repetitively comparison is performed
among elements and essential swapping of elements is done. Bubble sort is commonly used in sorting
algorithms. Though it is easy to understand, it is time consuming. In this type, two successive elements
are compared and swapping is done. Thus, step-by- step entire array elements are checked. It is
different from the selection sort. Instead of searching the minimum element and then applying
swapping, two records are swapped instantly upon noticing that they are not in order.
The following program explains sorting by exchange method.
14.6 Write a program to sort entered numbers by exchange method*
# include <stdio.h>
# include <conio.h>
main ()
{
int num[8]*{0};
int k«0,h,a,n,tmp;
clrscr();
printf (\n Bnter Numbers : ”);
for (0;a<8;++a)
scanf (%da,frnum[a]);
while (fc<7)
{
for (hvk+1;h<8;♦+h)
if (num[k] >num[h] )
{
tmp*num [k] ;

Get Programming and Data Structures now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.