Sei sulla pagina 1di 5

Shuttle Sort - Simple Insertion Sort

#include< stdio.h>
#include< conio.h>

void shutsort(int a[],int n)


{
int j,i=1,mid;

while(i< n)
{
j=i-1;
while(j>=0)
{
if(a[j]>a[j+1])
{
mid = a[j];
a[j] = a[j+1];
a[j+1]=mid;
j--;
}
else
break;
}
i++;
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");

for(i=0;i< n;i++)
printf("%5d",a[i]);
shutsort(a,n);

printf("\nArray After Sorting : ");


for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT
Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 98

Element 5 : 1

Array Befor Sorting : 21 36 54 98 1


Array After Sorting : 1 21 36 54 98
*/

Theory:
A simple, but not very efficient, algorithm for arranging a set of n numbers in order of
magnitude. The method starts with the left-hand pair of numbers, swapping them if necessary.
The second and third numbers are now considered. If they are swapped then the first pair are
reconsidered. Next the third and fourth numbers are considered. If swapped then previous pairs
are again reconsidered, working from right to left. As with the bubble sort, ½n(n-1) comparisons
may be required.

Shuttle sort. A sorting method based on swaps of pairs of numbers. In the example, × indicates a swap and Ο that
no swap is required. The sort works from left to right, reconsidering earlier pairs when a swap is made.

Applications
Many algorithms are useful in a broad spectrum of computer applications. These elementary
algorithms are widely studied and considered an essential component of computer science. They
include algorithms for sorting, searching, text processing, solving graph problems, solving basic
geometric problems, displaying graphics, and performing common mathematical calculations.
Sorting arranges data objects in a specific order, for example, in numerically ascending or
descending orders. Internal sorting arranges data stored internally in the memory of a computer.
Simple algorithms for sorting by selection, by exchange, or by insertion are easy to understand
and straightforward to code. However, when the number of objects to be sorted is large, the
simple algorithms are usually too slow, and a more sophisticated algorithm, such as heap sort or
quick sort, can be used to attain acceptable performance. External sorting arranges stored data
records.
Searching looks for a desired data object in a collection of data objects. Elementary searching
algorithms include linear search and binary search. Linear search examines a sequence of data
objects one by one. Binary search adopts a more sophisticated strategy and is faster than linear
search when searching a large array. A collection of data objects that are to be frequently
searched can also be stored as a tree. If such a tree is appropriately structured, searching the tree
will be quite efficient.
A text string is a sequence of characters. Efficient algorithms for manipulating text strings, such
as algorithms to organize text data into lines and paragraphs and to search for occurrences of a
given pattern in a document, are essential in a word processing system. A source program in a
high-level programming language is a text string, and text processing is a necessary task of a
compiler. A compiler needs to use efficient algorithms for lexical analysis (grouping individual
characters into meaningful words or symbols) and parsing (recognizing the syntactical structure
of a source program). See also Software engineering; Word processing.
A graph is useful for modeling a group of interconnected objects, such as a set of locations
connected by routes for transportation. Graph algorithms are useful for solving those problems
that deal with objects and their connections—for example, determining whether all of the
locations are connected, visiting all of the locations that can be reached from a given location, or
finding the shortest path from one location to another.
Mathematical algorithms are of wide application in science and engineering. Basic algorithms
for mathematical computation include those for generating random numbers, performing
operations on matrices, solving simultaneous equations, and numerical integration. Modern
programming languages usually provide predefined functions for many common computations,
such as random number generation, logarithm, exponentiation, and trigonometric functions.
In many applications, a computer program needs to adapt to changes in its environment and
continue to perform well. An approach to make a computer program adaptive is to use a self-
organizing data structure, such as one that is reorganized regularly so that those components
most likely to be accessed are placed where they can be most efficiently accessed. A self-
modifying algorithm that adapts itself is also conceivable. For developing adaptive computer
programs, biological evolution has been a source of ideas and has inspired evolutionary
computation methods such as genetic algorithms. See also Genetic algorithms.
Certain applications require a tremendous amount of computation to be performed in a timely
fashion. An approach to save time is to develop a parallel algorithm that solves a given problem
by using a number of processors simultaneously. The basic idea is to divide the given problem
into subproblems and use each processor to solve a subproblem. The processors usually need to
communicate among themselves so that they may cooperate. The processors may share memory,
through which they can communicate, or they may be connected by communication links into
some type of network such as a hypercube. See also Concurrent processing; Multiprocessing;
Supercomputer.

Operation
An algorithm generally takes some input, carries out a number of effective steps in a finite
amount of time, and produces some output. An effective step is an operation so basic that it is
possible, at least in principle, to carry it out using pen and paper. In computer science theory, a
step is considered effective if it is feasible on a Turing machine or any of its equivalents. A
Turing machine is a mathematical model of a computer used in an area of study known as
computability, which deals with such questions as what tasks can be algorithmically carried out
and what cannot. See also Automata theory; Recursive function.
Many computer programs deal with a substantial amount of data. In such applications, it is
important to organize data in appropriate structures to make it easier or faster to process the data.
In computer programming, the development of an algorithm and the choice of appropriate data
structures are closely intertwined, and a decision regarding one often depends on knowledge of
the other. Thus, the study of data structures in computer science usually goes hand in hand with
the study of related algorithms. Commonly used elementary data structures include records,
arrays, linked lists, stacks, queues, trees, and graphs.

Potrebbero piacerti anche