0% found this document useful (0 votes)
26 views5 pages

Algos

The document describes 13 algorithms: 1. Traversing an array using a while loop and for loop. 2. Inserting and deleting elements from an array. 3. Performing linear and binary searches on an array. 4. Sorting arrays using bubble, selection, insertion, and quick sort algorithms. 5. Merging two sorted arrays into a single sorted array. 6. Implementing stack, queue, and priority queue data structures using arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Algos

The document describes 13 algorithms: 1. Traversing an array using a while loop and for loop. 2. Inserting and deleting elements from an array. 3. Performing linear and binary searches on an array. 4. Sorting arrays using bubble, selection, insertion, and quick sort algorithms. 5. Merging two sorted arrays into a single sorted array. 6. Implementing stack, queue, and priority queue data structures using arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algos

Algo 1 (Traversing of Array using while Loop)


1. Set K = LB
2. Repeat step 3 and 4 while k <= UB
3. Apply Process to LA[k]
4. Set k=k+1
5. Exit

Algo 2 (Traversing of Array using For Loop)


1. Repeat for K = LB to UB (Apply Process top LA[k])
2. Exit

Algo 3 (Insertion in an Array)


1. Set J = n
2. Repeat step 3 and 4 while j>= k
3. Set LA[j+1] = LA[j]
4. Set j = j+1
5. Set LA[k] = item
6. Set n = n+1
7. Exit

Alog 4 (Deletion from an Array)


1. Set Item = LA[k]
2. Repeat for j = k to n-1 , Set LA[j] = LA [j+1] (End of Loop)
3. Set n = n-1
4. Exit
Algo 5 (Linear Search)
1. Set Data[n+1] = item
2. Set loc = 1
3. Repeat while Data[loc] != item, Set loc = loc + 1(End of loop)
4. If loc = n+1 , then set loc =0
5. Exit

Algo 6 (Binary Search)


1. Set Beg = lb, End = ub, Mid = (Beg + End)/2
2. Repeat step 3 and step 4 while Beg <= End and Data[Mid] != Item
3. If(Item < Data[Mid] then, End = Mid -1, Else Set Beg = Mid +1
4. Set Mid = int((Beg +End)/2)
5. If Data[Mid] = Item , then set Loc = Mid, Else Set Loc = 0
6. Exit

Algo 7 (Bubble Sort )


1. Repeat Step 2 for k = 1 to n-1
2. Repeat for ptr =1 to n-k , if Data[ptr] >Data[ptr +1], then Swap Data[ptr] and
Data[ptr+1](End of loop)
3. Exit

Alog 8 (Selection Sort)


1. Repeat step 2 and 3 for k = 1 to n-1
2. Call Min(A, k,n,loc)
3. Interchange/Swap A[k] with A[loc]
4. Exit
Min(A,k,n,loc)
1. Set min = A[k] and loc = k
2. Repeat for j = k+1 , k+2 …….n, if min > A[j] then, min = A[j] and loc = j
3. Return

Algo 9 (Insertion Sort)

1. Set A[0] = -999999999999


2. Repeat step 3 to 5 for k= 2,3,…n
3. Set temp = A[k] and ptr = k-1
4. Repeat while temp < A[ptr], a) Set A[ptr+1] = A[ptr] b) set ptr = ptr -1
5. Set A[ptr + 1] = temp
6. Return
Algo 10 ( Merging)
Let A and B are Sorted Arrays with R and S element
respectively.This Also merge A and B into Array C with n =
R+S element
1. Set NA= 1, NB = 1, ptr = 1
2. Repeat while NA < = R and NB <= S
If A[NA] < B[NB] then
a) C[ptr] = A[NA]
b) Set ptr = ptr +1 and NA = NA + 1
Else
a) C[ptr] = B[NB]
b) Set ptr = ptr + 1, NB = NB+1
(End of if)
3. If NA > R then
Repeat for k=0,1,2,…S-NB
C[ptr+k] = B[NB + k]
Else
Repeat for k= 0,1,2,…R-NA
C[ptr+k] = A[NA+k]
End of if
4. Exit

Algo 11 (Stack)
Push(stack , top, Maxstk, item)
1. If top = Maxstk , then print “Overflow” and return
2. Else Set top = top + 1
3. Set stack[top] item
4. Return
Pop(stack, top ,item)
1. If top =0 then print “underflow” and return
2. Set item = stack [top]
3. Set top = top -1
4. Return
Algo 12 (Quick)
Quick(A,n,beg,end,loc)
1. Set left = beg, right = end, and loc= beg
2. Repeat while A[loc] <= A[right] and loc !=right
a) right = right -1
End of loop
b) if loc = right then return
c) if A[loc] > A[right]
interchange A[loc] and A[right]
set loc = right
go to step 3
end of if
3. a) Repeat while A[left]<= A[loc] and left != loc then , left = left + 1(end of loop)
b. if loc = left ,then return
c) if A[left] > A[loc] , then
interchange A[left] and A[loc]
set loc = left
Go to step 2
End of if, End of Quick

1. set top = Null


2. if n>1, then top = top +1 , lower[1] = 1, upper[1] = n
3. Repeat step 4 to 7 while top != null
4. Beg = lower[top] , End = upper[top] , Top = Top – 1
5. Call Quick(A,n,Beg,End,loc)
6. If Beg<loc-1 then
top = top + 1
Lower[top] = Beg, Upper[top] = loc-1
7. If loc+1<End
Top = Top +1, lower[top] = loc +1
Upper[top] = End
8. Exit
Algo 13 (Queue)
1. If front = 1 and rear = n or front = rear + 1 then , print “Overflow” and return(end of if)
2. If front = Null then front = 1 and rear = 1
Else if rear= n then set rear = 1
Else rear = rear + 1
3. Queue[rear] = item
4. return

You might also like