Beruflich Dokumente
Kultur Dokumente
SERCHINGS &SORTINGS
SERCHING : Consider searching for a given value v in an array of size N. There are 2 basic
approaches: Linear Serch or sequential search and binary search.
1) LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={33,44,55,66,77},i,key;
clrscr();
printf("enter element to be searched");
scanf("%d",&key);
for(i=0;i<5;i++)
{
if(key==a[i])
printf("element is found at %d position",i);
else if(key!=a[i])
printf("\nno such element");
}
getch();
}
UNIT-4
SERCHINGS &SORTINGS
BINARY SEARCH
When the values are in sorted order, a better approach than the one given above is to use binary
search. The algorithm for binary search starts by looking at the middle item x. If x is equal to v,
it quits and returns true. Otherwise, it uses the relative ordering of x and v to eliminate half of the
array (if v is less than x, then it can't be stored to the right of x in the array; similarly, if it is
greater than x, it can't be stored to the left of x). Once half of the array has been eliminated, the
algorithm starts again by looking at the middle item in the remaining half. It quits when it finds v
or when the entire array has been eliminated.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={33,44,55,22,11},i,j,m,l=0,h=4,key;
clrscr();
printf("enet a key");
scanf("%d",&key);
for(i=0;i<5;i++)
{
m=(l+h)/2;
if(key>a[m])
h=m-1;
else if(key<a[m])
l=m+1;
}
printf("position is %d",m);
getch();
UNIT-4
SERCHINGS &SORTINGS
}
Sorting
Consider sorting the values in an array A of size N. Most sorting algorithms involve what are
called comparison sorts; i.e., they work by comparing values. Comparison sorts can never have
a worst-case running time less than O(N log N). Simple comparison sorts are usually O(N 2 ); the
more clever ones are O(N log N).
Three interesting issues to consider when thinking about different sorting algorithms are:
Does an algorithm always take its worst-case time?
What happens on an already-sorted array?
How much space (other than the space for the array itself) is required?
We will discuss four comparison-sort algorithms:
1.
2.
3.
4.
selection sort
insertion sort
merge sort
quick sort
1) selection sort
The idea behind selection sort is:
1. Find the smallest value in A; put it in A[0].
2. Find the second smallest value in A; put it in A[1].
3. etc.
The approach is as follows:
Use an outer loop from 0 to N-1 (the loop index, k, tells which position in A to fill next).
Each time around, use a nested loop (from k+1 to N-1) to find the smallest value (and its
index) in the unsorted part of the array.
Swap that value with A[k].
Note that after i iterations, A[0] through A[i-1] contain their final values (so after N iterations,
A[0] through A[N-1] contain their final values and we're done!)
#include<stdio.h>
UNIT-4
SERCHINGS &SORTINGS
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter size of arrary");
scanf("%d",&n);
printf("enter elements");
for(i=0;i<n;i++)
{
printf("enter element at a[%d]",i);
scanf("%d",&a[i]);
}
printf("\nbefore sorting");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nafter sort");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
UNIT-4
SERCHINGS &SORTINGS
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
getch();
}
2) INSERTION SORT
As for selection sort, a nested loop is used; however, a different invariant holds: after the ith time
around the outer loop, the items in A[0] through A[i-1] are in order relative to each other (but are
not necessarily in their final places). Also, note that in order to insert an item into its place in the
(relatively) sorted part of the array, it is necessary to move some values to the right to make
room.
UNIT-4
SERCHINGS &SORTINGS
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int temp,a[5]={44,55,22,11,66},i,j;
clrscr();
printf("\n before sort");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
printf("\n after sort");
for(i=0;i<5;i++)
{
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
for(i=0;i<5;i++)
printf("%d ",a[i]);
getch(); }
UNIT-4
SERCHINGS &SORTINGS
3) QUICK SORT
#include<stdio.h>
#include<conio.h>
#define maxsize 6
int A[maxsize];
++k;
if(A[k]<A[a])
{
leftarr[ltidx]=A[k];
ltidx++;
}
else
{
rtarr[rtidx]=A[k];
rtidx++;
UNIT-4
SERCHINGS &SORTINGS
}
}
k=a;
for(l=0;l<ltidx;++l)A[k++]=leftarr[l];
A[k++]=pivot;
for(l=0;l<rtidx;++l)A[k++]=rtarr[l];
if(ltidx>0)quicksort(a,a+ltidx-1);
if(rtidx>0)quicksort(b-rtidx+1,b);
}
void printarr(int a)
{
int i;
for(i=0;i<a;i++)
{
printf("%d",A[i]);
printf("\n");
}
}
main()
{
int i,s;
clrscr();
UNIT-4
SERCHINGS &SORTINGS
printf("enter the number of numbers to be entered \n");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("enter the number \n" );
scanf("%d",&A[i]);
}
printf("array before sorting ");
printarr(s);
quicksort(0,s-1);
printf("array after sorting");
printarr(s);
}
4) Heapsort:
Given an array of 6 elements: 15, 19, 10, 7, 17, 16, sort it in ascending order using heap sort.
Steps:
1.
2.
Consider the values of the elements as priorit ies and build the heap tree.
Start deleteMin operations, storing each deleted element at the end of the heap array.
After performing step 2, the order of the elements will be opposite to the order in the heap tree.
Hence, if we want the elements to be sorted in ascending order, we need to build the heap tree
in descending order - the greatest element will have the highest priority.
Note that we use only one array , treating its parts differently:
a.
b.
when building the heap tree, part of the array will be considered as the heap,
and the rest part - the orig inal array.
when sorting, part of the array will be the heap, and the rest part - the sorted array.
UNIT-4
SERCHINGS &SORTINGS
This will be indicated by colors: wh ite for the original array, blue for the heap and red for the so rted array
Here is the array: 15, 19, 10, 7, 17, 6
A. Building the heap tree
The array represented as a tree, complete but not ordered:
Start with the right most node at height 1 - the node at position 3 = Size/ 2.
It has one greater child and has to be percolated down:
UNIT-4
SERCHINGS &SORTINGS
Next co mes array [2]. Its children are smaller, so no percolation is needed.
The last node to be processed is array[1]. Its left ch ild is the greater of the children.
The item at array[1] has to be percolated down to the left, swapped with array[2].
UNIT-4
SERCHINGS &SORTINGS
The children of array[2] are g reater, and item 15 has to be moved down further, swapped with array[5].
UNIT-4
SERCHINGS &SORTINGS
1.4. Percolate once more (10 is less that 15, so it cannot be inserted in the previous hole)
UNIT-4
SERCHINGS &SORTINGS
UNIT-4
SERCHINGS &SORTINGS
Instead it becomes a cell fro m the sorted array
2.3. The element 10 is less than the children of the hole, and we perco late the hole down:
3. DeleteMax 16
3.1. Store 16 in a temporary place. A hole is created at the top
UNIT-4
SERCHINGS &SORTINGS
3.3. Percolate the hole down (7 cannot be inserted there - it is less than the children of the hole)
UNIT-4
SERCHINGS &SORTINGS
4.3. Store 10 in the hole (10 is greater than the children of the hole)
UNIT-4
SERCHINGS &SORTINGS
5. DeleteMax the top element 10.
5.1. Remove 10 fro m the heap and store it into a temporary location.
5.3. Store 7 in the hole (as the only remain ing element in the heap
7 is the last element fro m the heap, so now the array is sorted
Back to HeapSort
UNIT-4
SERCHINGS &SORTINGS