Sie sind auf Seite 1von 15

SORTING AND SEARCHING

Sorting is basically arranging the elements in some type of order ascending or may be
descending

Searching: Searching is finding the location of the element with a given value or the
record with a given key.
The various searching and sorting algorithms are as follows:
LINEAR SEARCH: It is a sequential search.
COMPLEXITY:C(n)= n/2
ALGO:
linear(DATA,N,ITEM,LOC)
Here, DATA is a linear array with N elements, and ITEM is given item of information.
This algo finds the location LOC of item in data, or sets LOC=0 if search is unsuccessful.
Step 1.(Insert ITEM at the end of DATA) Set DATA [N+1]=ITEM
Step 2.[Initialize counter] Set LOC =1
Step 3.[Search for ITEM]
Repeat while DATA [LOC]=ITEM.
Set LOC=LOC+1
endloop
Step 4.[Successful?] If LOC=N+1, then: Set LOC=0
Step 5. Exit

BINARY SEARCH
COMPLEXITY- log 2(n) [Worst case]
ALGO:
Binary(DATA,LB,UB,ITEM,LOC)
Here DATA is sorted array with lower bound LB and upper bound UB,and ITEM is
agiven item of information .The variable BEG,END and MID denote respectively,the

beginning ,end and middle location of a segment of element of DATA.The algo finds the
location LOC of ITEM in DATA or sets LOC=NULL.
Step 1. [intialise segment variables]
Set BEG=LB,
END=LB and
MID=INT((BEG+END)/2)
Step 2. Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!=ITEM
Step 3. If ITEM<DATA[MID], then
Set END=MID-1
Else:
Set BEG=MID+1
End if
Step 4. Set MID=INT((BEG + END)/2)
End of step 2 loop
Step 5. If DATA[MID]=ITEM, then:
Set LOC=MID
Else:
Set LOC = NULL
End if
Step 6. Exit.

SORT
COMPLEXITY:O(n) comparisons
Speed: O(n^2), extremely slow
Space: The size of initial array
Coding Complexity: Simple
Concept.:- Given an input sequence [an] whose values are assumed to be real numbers,
bubble-sort places each value a(i) in its proper place on the number line by respectively
"bubbling up" or migrating the minimum (maximum) of the sequence or remaining
subsequence to achieve a sort in ascending (descending) order.

In the best case, the input is presorted and bubble-sort requires a minimum number of I/O
operations.
In the worst case, the input is sorted opposite to the desired order of the sorted output,
and a maximum number of I/O operations are required. The number of comparisons is
invariant to input value, since the maximum of each sequence or subsequence is not
known a priori
ALGO:
BUBBLE(DATA,N)
Here DATA is an array with N elements. This algo sorts the elements in DATA.
Step1.Repeat steps 2 and 3 for K=1 to N-1
Step 2. Set PTR=1
Step 3. Repeat while PTR<=N-K

a) If DATA[PTR]>DATA[PTR+1], then interchange DATA[PTR] and


DATA[PTR+1]
End if
b) Set PTR= PTR+1
End of inner loop
End of step 1 outer loop
Step 4.Exit

BINARY SEARCH
COMPLEXITY
Algo_Binary Search_(ARR,N,ITEM,LOC)
Here, ARR be array of size N.
Let ITEM be a variable which we want to search in array ARR and we want put the index
value of the item in variable LOC
LOC = location
Step1. int LB,UB,MID;
Step 2. LB=0 , UB=N+1
Step 3 Repeat while (UB>=LB)
{
MID=(UB+LB)/2
MID=(N-1)/2
Step 4. Compare ARR[MID] with ITEM
If ARR[MID]= = ITEM
{
LOC = MID
And goto Step 6
End if
}
If ARR[MID]>ITEM
{
UB=MID-1
End if
}
Else
{
LB=MID+1
}

Goto Step 3
}end loop
Step 5. if ((UB= = LB) && (ARR[UB]= = ITEM))
{
LOC = UB
Display item found at LOC
}
Else
{
Display Item not found
}
Step 6. End

PATTERN SEARCH
COMPLEXITY:O(n*n)
Time: It is proportional to (n*n)
Worst Case: When every character of the string except the last matches every substring.
ALGO:
Algo_patternsrch(str1,str2,loc)
This algo searches for str2 in str1 and the location of str2 in str1 is noted in variable loc.
Loc will contain index value of str2 in str1.
Step 1. int i ,loc = -1
Step 2.. Repeat while((str1[i]!=str2[0])&&(str1[i]!=NULL))
{
i = i+1
}end loop
Step 3. if (str1[i]==str2[0])
{
j=0
loc =i
Repeat while((str1[i]==str2[j])&&(str2[j]!=NULL))
{
i++;
j++;

}end loop
if(str2[j]!=NULL)
{
Loc=-1
}
Goto Step 1.
Else
Display pattern found at loc
}end if
Step 4. Display pattern not found
Step 5. STOP

LINKED LIST - is a linear collection of data elements called NODES ,where the linear
order is given by pointers. Each node is divided into 2 parts : 1st part contains
information of elements, and second part contains the address of the next node.
ALGO:
Searching in linked list.
Search_algo(start,node,ecode,ename,next)
Let ptr be a pointer of data type node. Let sitem be the searched item.
Step 1. ptr = =start
Step 2.found =n , ct = 0;
Step 3. Repeat while((found !=y)&&(ptr!=NULL))
{
ct++
if(ptr->ecode = =sitem)
{
Cout<<ptr->ename
Found=y
Cout<<ct
}end if
Ptr=ptr->next
End loop
Step 4. if (found =n)
{
Display item not found ;
}end if
Step 5. Exit

QUICK SORT:
COMPEXITY:O(n*n)
And also f(n) 1.4[n log n] is expected number of comparisons.
Speed: O(n^2), extremely slow
Space: The size of initial array
Coding Complexity: Simple
Quick Sort - basic idea
Partition the array in O(n)
Recursively sort left array in O(log2 n) best/average case
Recursively sort right array in O(log2 n) best/average case

Quick Sort facts:


Worst case time: O(n^2).
Average case (if all orderings equally likely): O(n log n).
Average case (if random pivot, worst-case input): O(n log n).
Fast in practice due to in-site sorting unlike merge sort.
There are many pivot selection algorithm for Quick Sort (different execution time).

Quicksort_calling(A,N,beg,end,loc)
Let A be the array parameter beg & end contains index value to 1st element and last
element of array A. We will uses 2 stacks i.e. stacks array i.e. lower & upper.
LOWER & UPPER
Step 1. Top= -1
Step 2. if N>1
Then Top=Top+1
Lower[Top]=0
Upper [Top]=N-1
Step 3. Repeat Step 4 to7 till Top= -1 or while Top>-1
Step 4. beg=lower[Top]
End=upper[Top]
Top=Top-1
Step 5. Call quickcalled (A,N,beg,end,loc)
Step 6. if beg<loc-1 , then
{
Top=Top+1

lower[Top]=beg
upper[Top]=loc-1
}end if
Step 7. if loc+1<end , then
{
Top =Top+1
Lower[Top]=loc+1
Upper[Top]=end
}end if
End loop
Step 8. Exit
Algo_quickcalled(A,N,beg,end,loc)
This algo is treated on a sublist A[beg] to A[end] and position A[beg] at its correct
position i.e. A[loc]. Thus positioning A[beg] at its correct position in a sorted array. We
will use 2 local variable contain the boundary of the elements which have not been
scaned by the algorithm
Step 1. left = beg ,
right = end ,
loc = beg
Step 2.(a) Repeat while [loc]<=A[right] && loc!=right
right=right-1
end loop
(b)if (loc= =right)->return
(c)if A[loc]>A[right]
{
i.
temp=A[loc]
A[loc]=A[right]
ii
loc=right
iii
goto step 3.
Step 3. (a) Repeat while A[loc]>=A[left] && loc!=left
{
left = left + 1
}end loop
(b) if (loc = = left) then return
(c) if A[left]>A[loc]
{
i temp=A[left]
A[loc]=temp
ii loc=left
iii goto Step2.
}
Step 4. Return

Heap Sort
Complexity: O(n log 2 n)
Speed: O(N*log N)
Space: The size of initial array
Coding Complexity: Complex, using advanced data structure, a Heap.

Heap Sort-Basic Idea


Build the Heap from array A
We know the root of the Heap is the highest value, swap with the last element
Now we are sure that the last element has the highest value
Then we decrease the heap size by 1
And do Heapify in O(log N) without considering the last element
Repeat this process until Heap size become 1.
INSHEAP(TREE,N,ITEM)
Let H be a heap with N elements . let the sequential representation be represented as an
array TREE.
This algorithm is to insert the ITEM in heap H. We assume that there are total of N nodes
in the heap H.
In this algorithm we will first insert the item at position N+1 in the array TREE then
make it rise to make a max heap.
Step 1. N=N+1;
TREE[N]=ITEM
Step 2. Ptr=N/2;
Repeat while ((Ptr > 0)&&(TREE[ptr]<TREE[N]))
{
If (TREE[ptr]<TREE[N]
{
Temp=TREE[Ptr];
TREE[Ptr]=TREE[N];
TREE[N]=Temp;
}
N=ptr;
Ptr=N/2;
endif
}
Endloop
Step 3. Return;

Algorithm for deleting root from heap


DELHEAP(TREE,N,ITEM)
Let H be a heap with N elements . let the sequential representation be represented as an
array TREE.
Step 1. Var Item=TREE[1];
Step 2. Var Last=TREE[N];
N=N-1;
Step4. TREE[1]=last;
Step 5. /********************The following code will sink the element to its correct
position ****/
ptr=1;
left=2*ptr;
right=2*ptr+1;
repeat while ((ptr<=N) &&(TREE[ptr]<TREE[left])&&TREE[ptr]<TREE[right]))
{
If (TREE[ptr]<TREE[left]
Then
Temp=TREE[ptr];
TREE[ptr]=TREE[left];
TREE[left]=temp;
Ptr=left;
Else
if (TREE[ptr]>TREE[right])
then
temp=TREE[right];
TREE[right]=TREE[ptr];
TREE[ptr]=temp;
Ptr=right;
Endif
Left=2 * ptr;
Right=left+1;
Endloop
Step 6.
Return
Algorithm HeapSort(Arr A,N)
Let A be an array of size N. We want to sort the array A. First we will build the heap H
using sequential representation TREE.
Step 1. Ctr=1;
Step 2. Repeat while (ctr <=N)
{
Call INSHEAP(TREE,N=ctr-1,ITEM=A[ctr]);
Ctr=ctr+1;

}
Endloop;
Step 3. Ctr=1
Repeat while (ctr<=N)
{
DELHEAP(TREE,N,ITEM);
A[ctr]=item;
Ctr=ctr+1;
}
endloop
Step 4: exit

Radix Sort/Bucket Sort


For Radix Sort, we assume that the input are n d-digits number, where d is reasonably
limited.
Radix Sort will then sorts these number digit by digit, starting with the least significant
digit to the most significant digit. It usually use a stable sort algorithm to sort the digits,
such as Counting Sort above.
Example:
input:
321
257
113
622
sort by third (last) digit:
321
622
113
257
after this phase, the third (last) digit is sorted.
sort by second digit:
113
321
622
257
after this phase, the second and third (last) digit are sorted.
sort by second digit:
113
257

321
622
after this phase, all digits are sorted.

Merge Sort
Complexity:O(n) [f(n)<=n=O(n)]
Speed: O(n Log n), similar speed with Quick Sort, pure n log n algorithm
Space: 2*the size of initial array
Coding Complexity: Complex, using Divide & Conquer approach
Merge Sort - basic idea
Sort first half of the array.
Sort second half of the array.
Merge the result.
The bad side of this sorting algorithm is this algorithm need 2*the size of initial array to
perform it's task. This can be very inefficient from memory-Point of View.
ALGO:
MERGEPASS(A,N,L,B)
The N-element array A is composed of sorted subarrays where each subarray has L
elements except possibly the last subarray, which may have fewer than L elements.
The procedure merges the pairs of subarrays of A and assigns them to the array B.
Step1. Q=INT(N/(2*L)),
S=2*L*Q
R=N-S
Step2. Repeat for J=1,2,..Q:
a)Set LB=1+(2*J-2)L.
b) Call MERGE(A,L,LB,A,L,LB+L,B,LB).
end of loop
Step3. If R<=L,then
Repeat for J=1,2,....R
Set B(S+J) =A(S+J)
Endloop
Else:
Call MERGE(A,L,S+1,A,R,L+S+1,B,S+1).
End of if structure.
Step4. Return

MERGESORT(A,N)
This algo sorts the N-element array A using an auxiliary array B.
Step1. Set L=1
Step2.Repeat steps 3 to 6 while L<N.
Step3. Call MERGEPASS(A,N,L,B)
Step4. Call MERGEPASS(B,N,2*L,A)
Step5. Set L=4*L.
End of step 2 loop
Step6.Exit.

INSERTION SORT:
COMPLEXITY:
Worst Case:- n(n-1)/2 =O(n*n)
Average Case:- n(n-1)/4 =O(n*n)
Algo:
INSERTION (A,N)
This algorithm sorts the array A with N elements.
Step1. A[0]= -
Step2. Repeat steps 3 to 5 for K=2,3,.N
Step3. Set TEMP =A[K] and PTR =K-1
Step4. Repeat while TEMP<A[PTR]:
a) Set A[PTR+1] = A[PTR]
b) Set PTR= PTR-1.
End of loop
Step5.Set A[PTR+1]=TEMP
End of step 2 loop.
Step6. Return.

SELECTION SORT
COMPLEXITY:
Worst Case: n(n-1)/2 =O(n*n)
Average Case: n(n-1)/2=O(n*n)
This algorithm sorts the array A with N elements.
Step1. Repeat steps 2 and 3 for K=1,,.N-1:
Step2. Call MIN(A,K,N,LOC)
Step3. Set TEMP= A[K],

A[K]= A[LOC],
A[LOC]= TEMP
End of step1 loop
Step4. Exit.

Das könnte Ihnen auch gefallen