0% found this document useful (0 votes)
6 views

sorting and searching algorithm

The document outlines various searching and sorting algorithms, including Linear Search, Binary Search, Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, and Quick Sort. Each algorithm is described step-by-step, detailing the initialization, conditions, and operations involved. The document serves as a guide for implementing these algorithms in programming.

Uploaded by

roger88838
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

sorting and searching algorithm

The document outlines various searching and sorting algorithms, including Linear Search, Binary Search, Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, and Quick Sort. Each algorithm is described step-by-step, detailing the initialization, conditions, and operations involved. The document serves as a guide for implementing these algorithms in programming.

Uploaded by

roger88838
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1) LINEAR_SEARCH(A, N, VAL)

Step 1: [INITIALIZE] SET POS=-1


Step 2: [INITIALIZE] SETI=1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS=I
PRINT POS
Go to Step 6
[END OF IF]
[END OF LOOP]
Step 6: EXIT
SETI=I+1
Step 5: IF POS = –1
PRINT VALUE IS NOT PRESENT
IN THE ARRAY
[END OF IF]
2) BINARY_SEARCH Algorithm
BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS=-1
Step 2: Repeat Steps 3 and 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID-1
ELSE
SET BEG = MID+1
[END OF IF]
[END OF LOOP]
Step 5: IF POS=-1
PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT
3) Bubble sort
BUBBLE_SORT(A, N)
Step 1: Repeat Step 2 For1= to N-1
Step 2: Repeat ForJ= toN-I
Step 3: IF A[J] > A[J+1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT

4) INSERTION-SORT
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 forK=1toN–1
Step 2: SET TEMP = ARR[K]
Step 3: SETJ=K-1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SETJ=J-1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
5) SELECTION SORT
SMALLEST (ARR, K, N, POS)
Step 1: [INITIALIZE] SET SMALL = ARR[K]
Step 2: [INITIALIZE] SET POS=K
Step 3: Repeat forJ= K+1 to N
IF SMALL > ARR[J]
SET SMALL = ARR[J]
SET POS=J
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS

SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K=1
to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT

6) Merge sort
MERGE (ARR, BEG, MID, END)
Step 1: [INITIALIZE] SETI= BEG,J= MID+1, INDEX =
Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J]
SET TEMP[INDEX] = ARR[I]
SETI=I+1
ELSE
SET TEMP[INDEX] = ARR[J]
SETJ=J+1
[END OF IF]
SET INDEX = INDEX+1
[END OF LOOP]
Step 3: [Copy the remaining elements of right sub-array, if any]
IFI> MID
Repeat whileJ<= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX+1, SETJ=J+1
[END OF LOOP]
[Copy the remaining elements of left sub-array, if any]
ELSE
Repeat whileI<= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX+1, SETI=I+1
[END OF LOOP]
[END OF IF]
Step 4: [Copy the contents of TEMP back to ARR] SET K=
Step 5: Repeat whileK< INDEX
SET ARR[K] = TEMP[K]
SETK=K+1
[END OF LOOP]
Step 6: END

7) Quick sort Algorithm


PARTITION (ARR, BEG, END, LOC)
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =0
Step 2: Repeat Steps 3 to 6 while FLAG =0
Step 3: Repeat while ARR[LOC] <= ARR[RIGHT] AND LOC != RIGHT
SET RIGHT = RIGHT-1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG=1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG =
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT+1
[END OF LOOP]
Step 6: IF LOC = LEFT
SET FLAG=1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
QUICK_SORT (ARR, BEG, END)
Step 1: IF (BEG < END)
CALL PARTITION (ARR, BEG, END, LOC)
CALL QUICKSORT(ARR, BEG, LOC-1)
CALL QUICKSORT(ARR, LOC+1, END)
[END OF IF]
Step 2: END

You might also like