Vous êtes sur la page 1sur 49

[Algorithm of Linear Search]

1. Set FLAG := -1

2. Print, 'How many elements in the array(<=80)?'

3. Read N

4. if N<0 or N>80

go to Step 2

5. Print, 'Enter elements now...'

6. For I := 0 till I<N, follow steps 7-8

7. Print, 'Element#', I+1,':'

8. Read ARR[I]

9. Print, 'Enter the value to be searched:'

10. Read NUM

[Linear Search begins]

11. For I := 0 till I<N, follow step 12

12. If NUM=ARR[I]

Set FLAG := I,

Break

13. If FLAG=-1

Print, NUM, 'not found.'

Else

Print, NUM, 'found at index ', FLAG, ', location ', FLAG+1

14. Exit
[Algorithm of Binary Search]
1. Set LOC := -1

2. Print, 'How many elements in the array(<=80)?'

3. Read N

4. if N<0 or N>80

go to Step 2

5. Print, 'Enter elements in increasing order now...'

6. For I := 0 till I<N, follow steps 7-8

7. Print, 'Element#', I+1,':'

8. Read ARR[I]

9. Print, 'Enter the value to be searched:'

10. Read NUM

11. Set MIN := 0, MAX := N-1

12. While MIN <= MAX, follow steps 14-15

13. MID := (INT)(MIN+MAX)/2

14. If NUM = ARR[MID]

LOC := MID,

Break

Else if NUM > ARR[MID]

MIN := MID+!

Else

MAX := MID-1
15. If LOC = -1

Print, NUM, ' not found.'

Else

Print, NUM, ' found at index ', LOC, ' , location ', LOC+1

16. Exit
[Program to perform Bubble Sort on Integers]
1. Set N := -1

2. Follow steps 3-4 until N<0 or N>80

3. Print, 'How many elements in the array(<=80)?;

4. Read N

5. Print 'Enter the values now...'

6. For I := 0 till I<N by 1, follow steps 7-8

7. Print, 'Element#', I+1, ':'

8. Read A[I]

9. Print 'Sorting starts...'

10. For I := N-2 to 0 by -1, follow steps 11-12

11. For J := 0 to J<=I by 1, follow step 12

12. If A[J]>A[J+1}

Call SWAP(A,J)

13. Print, 'Sorted array...'

14. For I := 0 till I<N by 1

15. Print, A[I], '\n'

16. Exit
[Algorithm for the function SWAP()]
SWAP(ARR[I], N)

1. Set T := ARR[N]

2. Set ARR[N] := ARR[N+1]

3. Set ARR[N+1] := T

4. Return
[Program to perform Bubble Sort on Strings]
1. Set N := -1

2. Follow steps 3-4 until N<0 or N>80

3. Print, 'How many strings in the string array(<=80)?;

4. Read N

5. Print 'Enter the strings now...'

6. For I := 0 till I<N by 1, follow steps 7-8

7. Print, 'Element#', I+1, ':'

8. Read A[I]

9. Print 'Sorting starts...'

10. For I := N-2 to 0 by -1, follow steps 11-12

11. For J := 0 to J<=I by 1, follow step 12

12. If A[J]>A[J+1}

Call SWAP(A,J)

13. Print, 'Sorted string array...'

14. For I := 0 till I<N by 1

15. Print, A[I], '\n'

16. Exit
[Algorithm for the function SWAP()]
SWAP(ARR[I], N)

1. Set T:=ARR[N]

2. Set ARR[N]:=ARR[N+1]

3. Set ARR[N+1]:=T

4. Return
[Algorithm to perform Insertion Sort]
1. Print, 'How many elements?'

2. Read N

3. Set N := N+1

4. Print, '\nEnter array elements...\n'

5. For I := 1 till I<N by 1, follow step 6

6. Read AR[I]

7. Call INSSORT(AR, N)

8. Print, '\nThe sorted array is...\n'

9. For I := 1 till I<N by 1, follow step 10

10. Print, AR[I], '\t'

11. Exit
[Algorithm for the function INSSORT()]
INSSORT(AR[], S)

1. Set AR[0] := INT_MIN

[INT_MIN is used to denote the smallest number in Mathematical Limits.]

2. For I := 1 to I<S by 1, follow steps 3-11

3. Set T := AR[I]

4. Set J := I-1

5. While T<AR[J], follow steps 6-7

6. Set AR[J+1] := AR[J]

7. J := J-1

8. Set AR[J+1] := T

9. Print, 'Array after pass ', I, ' is \t'

10. For K := 1 till K<S by 1, follow step 11

11. Print AR[K], '\t'

12. Return
[Algorithm to demonstrate Selection Sort]
1. Print, 'How many elements?\t'

2. Read N

3. Print, '\nEnter array elements...\n'

4. For I := 0 till I<N by 1, follow step 5

5. Read AR[I]

6. Call SELSORT(AR, N)

7. Print, '\nSorted array is...\n'

8. For I := 0 till I<N by 1, follow step 9

9. Print, AR[I],'\n'

10. Exit
[Algorithm for function SELSORT()]
SELSORT(AR[], SIZE)

1. For I := 0 till I<SIZE by 1, follow steps 2-15

2. Set FLAG := 0

3. Set SMALL := ARR[I]

4. For J := I+1 till J<SIZE by 1, follow steps 5-8

5. If AR[j]<SMALL, follow steps 6-8

6. Set SMALL := AR[J]

7. Set POS := J

8. Set FLAG := 1

9. If FLAG != 0, follow steps 10-12

10. Set TEMP := AR[I]

11. Set AR[I] := AR[POS]

12. Set AR[POS] := TEMP

13. Print, '\nArray after pass ', I+1,' is\n'

14. For J := 0 till J<SIZE by 1, follow step 15

15. Print AR[J], '\t'

16. Return
[Algorithm to demonstrate queues using arrays]
1. Set FRONT := -1

2. Set REAR := -1

3. Follow steps 4-12 until CH =1 or CH =2

4. Print, '\n---Queue Menu---\n1.Insertion\n2.Deletion\nEnter your choice...'

5. Read CH

6. If CH =1, follow steps 7-8

7. If REAR = MAX-1, then

Print, 'Overflow'

Exit

Else if REAR = -1, then

Set FRONT := 0

Set REAR := 0

Print, 'Enter the item to be inserted:'

Read ARR[REAR]

Call DISPLAY(ARR, FRONT, REAR)

Else

REAR := REAR+1

Print, 'Enter the item to be inserted:'

Read ARR[REAR]

Call DISPLAY(ARR, FRONT, REAR)

8. Break
9. If CH =2, follow steps 10-11

10. If FRONT > REAR or FRONT = -1, then

Print, 'Underflow'

Exit

Else

Print, ARR[FRONT], ' deleted'

FRONT := FRONT+1

Call DISPLAY(ARR, FRONT, REAR)

11. Break

12. If CH != 1 and CH != 2, then

Print, 'Wrong choice entered!'

13. Exit
[Algorithm for the function DISPLAY()]
DISPLAY( A[], F, R)

1. Print,'\n'

2. For I := F till T<R by 1, follow step 3

3. Print, A[I], '\t'

4. Return
[Algorithm to demonstrate circular queues using arrays]
1. Set FRONT := -1

2. Set REAR := -1

3. Follow steps 4-12 until CH =1 or CH =2

4. Print, '\n---Queue Menu---\n1.Insertion\n2.Deletion\nEnter your choice...'

5. Read CH

6. If CH =1, follow steps 7-8

7. If REAR = FRONT -1 or (FRONT =0 and REAR =MAX-1), then

Print, 'Overflow'

Exit

Else if REAR = -1, then

Set FRONT := 0

Set REAR := 0

Print, 'Enter the item to be inserted:'

Read ARR[REAR]

Call DISPLAY(ARR, FRONT, REAR)

Else if REAR = MAX-1, then

Set REAR := 0

Print, 'Enter the item to be inserted:'

Read ARR[REAR]

Call DISPLAY(ARR, FRONT, REAR)

Else

REAR := REAR+1
Print, 'Enter the item to be inserted:'

Read ARR[REAR]

Call DISPLAY(ARR, FRONT, REAR)

8. Break

9. If CH =2, follow steps 10-11

10. If FRONT = -1, then

Print, 'Underflow'

Exit

Else if FRONT = REAR, then

Print, '\n', ARR[FRONT], ' deleted.'

Set FRONT := -1

Set REAR := -1

Else if FRONT = MAX-1, then

Print, '\n', ARR[FRONT], ' deleted'

Set FRONT := 0

Call DISPLAY(ARR, FRONT, REAR)

Else

Print, '\n', ARR[FRONT], ' deleted'

FRONT := 0

Call DISPLAY(ARR, FRONT, REAR)

11. Break

12. If CH != 1 and CH != 2, then

Print, 'Wrong choice entered!'


13. Exit
[Algorithm for the function DISPLAY()]
DISPLAY( A[], F, R)

1. Print,'\n'

2. If F <= R, then

For I := F till I<=R by 1, then

Print A[I], '\t'

Else

For I := F till I<MAX by 1, then

Print A[I], '\t'

For I := 0 till I<= R by 1, then

Print A[I], '\t'

3. Return
[Algorithm to demonstrate Stacks in Linked List]
[Create a NODE data type with elements (INT)INFO, (POINTER OF NODE)DATA]

1. Set CH := 'y'

2. Set TOP := NULL

3. While CH='y' or CH='Y', follow steps 4-11

4. Print, 'Enter information for new node:'

5. Read INF

6. Set NEWPTR := CREATENEWNODE(INF)

7. If NEWPTR=NULL, then

Print, 'Node could not be created!'

Exit

8. Call PUSH(NEWPTR)

9. Call DISPLAY(TOP)

10. Print, 'Wanna enter more (y/n)?'

11. Read CH

12. Print, '\nDo you wanna pop the elements(y/n)?'

13. Read CH

14. While CH='y' or CH='Y', follow steps 15-19

15. Call POP()

16. Print, '\nThe stack after performing the deletion is...\n'

17. Call DISLAY(TOP)

18. Print,'\nDo you wanna pop more elements(y/n)?'

19. Read CH
20. Exit
[Algorithm for the function CREATENEWNODE()]
NODE *CREATENEWNODE(N)

1. Set PTR := new NODE

2. Set PTR->INFO := N

3. Set PTR->NEXT := NULL

4. Return PTR
[Algorithm for the function PUSH()]
PUSH(*NP)

1. If TOP=NULL, then

Set TOP := NP

Else

Set SAVE := TOP

Set TOP := NP

Set NP->NEXT := SAVE

2. Return
[Algorithm for the function DISPLAY()]
DISPLAY(*NP)

1. While NP != NULL, follow steps 2-3

2. Print, NP->INFO,'-->'

3. Set NP:=NP->NEXT

4. Print,'!!!\n'

5. Return
[Algorithm for the function POP()]
POP()

1. If TOP=NULL, then

Print, 'Underflow!!'

Exit

Else

Set PTR:=TOP

Set TOP:=TOP->NEXT

Delete PTR

2. Return
[Algorithm to demonstrate Linked List Traversal]
[Create a NODE data type with elements (INT)INFO, (POINTER OF NODE)DATA]

1. Print,'Node creation...\n'

2. Set CH:='y'

3. Set TOP:=NULL

4. While CH='y' or CH='Y', follow steps 5-11

5. Print,'\nEnter info for node:'

6. Read I

7. Set PTR:=CREATENEWNODE(I)

8. Set PTR->NEXT:=TOP

9. Set TOP:=PTR

10. Print,'\nDo you want to enter more (y/n)?'

11. Read CH

12. Print,'\nPress any key to continue...'

13. Call DISPLAY(TOP)

14. Exit
[Algorithm for the function CREATENEWNODE()]
NODE *CREATENEWNODE(N)

1. Set PTR := new NODE

2. Set PTR->INFO := N

3. Set PTR->NEXT := NULL

4. Return PTR
[Algorithm for the function DISPLAY()]
DISPLAY(*NP)

1. While NP != NULL, follow steps 2-3

2. Print, NP->INFO,'-->'

3. Set NP:=NP->NEXT

4. Print,'!!!\n'

5. Return
[Algorithm to demonstrate Linked List Search]
[Create a NODE data type with elements (INT)INFO, (POINTER OF NODE)DATA]

1. Print, 'Node creation...\n'

2. Set CH:='y'

3. Set TOP:=NULL

4. While CH='y' or CH='Y', follow steps 5-11

5. Print,'\nEnter info for node:'

6. Read I

7. Set PTR:=CREATENEWNODE(I)

8. Set PTR->NEXT:=TOP

9. Set TOP:=PTR

10. Print,'\nDo you want to enter more (y/n)?'

11. Read CH

12. Print, '\nPress any key to continue...'

13. Print, 'Enter the element to be searched:'

14. Read I

15. Set POS:=SEARCH(TOP,I)

16. If POS=-1, then

Print, '\n', I, ' not found in Linked List.'

Else

Print, '\n', I, ' found at position ', POS, '.'


[Algorithm for the function SEARCH()]
INT SEARCH(*NP, N)

1. Set I:=1

2. While NP!=NULL, follow steps 3-6

3. If NP->INFO=N, then

4. Return I

5. Set NP:=NP->NEXT

6. Set I:=I+1

7. Return -1
[Algorithm for the function CREATENEWNODE()]
NODE *CREATENEWNODE(N)

1. Set PTR := new NODE

2. Set PTR->INFO := N

3. Set PTR->NEXT := NULL

4. Return PTR
[Algorithm to demonstrate Linked List Insertion]
[Create a NODE data type with elements (INT)INFO, (POINTER OF NODE)DATA]

1. Print, 'Node creation...\n'

2. Set CH:='y'

3. Set TOP:=NULL

4. While CH='y' or CH='Y', follow steps 5-11

5. Print,'\nEnter info for node:'

6. Read I

7. Set PTR:=CREATENEWNODE(I)

8. Set PTR->NEXT:=TOP

9. Set TOP:=PTR

10. Print,'\nDo you want to enter more (y/n)?'

11. Read CH

12. Print, '\nPress any key to continue...'

13. Print, '\nEnter the element to be inserted:'

14. Read I

15. Set NEWPTR:=CREATENEWNODE(I)

16. Print, '\nEnter the location for insertion:'

17. Read I

18. Call INSERT(NEWPTR, I)

19. Print, '\n\nAfter performing the insertion, the Linked List is...\n'

20. Call DISPLAY()

21. Exit
[Algorithm for the function CREATENEWNODE()]
NODE *CREATENEWNODE(N)

1. Set PTR := new NODE

2. Set PTR->INFO := N

3. Set PTR->NEXT := NULL

4. Return PTR
[Algorithm for the function INSERT()]
INSERT(*PTR, LOC)

1. Set *NP:=NULL

2. If LOC=1, then

Set PTR->NEXT:=TOP

Set TOP:=PTR

Return

Else

For I:=2, NP:=TOP till NP!=NULL by 1, follow steps a-c

a. Set SAVE:=NP

b. Set NP:=NP->NEXT

c. If I=LOC, then

Set SAVE->NEXT:=PTR

Set PTR->NEXT:=NP

Return

3. Set SAVE->NEXT:=PTR

4. Return
[Algorithm for the function DISPLAY()]
DISPLAY(*N)

1. While N != NULL, follow steps 2-3

2. Print, N->INFO,'-->'

3. Set N:=N->NEXT

4. Print,'!!!\n'

5. Return
[Algorithm to demonstrate Linked List Deletion]
[Create a NODE data type with elements (INT)INFO, (POINTER OF NODE)DATA]

1. Print, 'Node creation...\n'

2. Set CH:='y'

3. Set TOP:=NULL

4. While CH='y' or CH='Y', follow steps 5-11

5. Print,'\nEnter info for node:'

6. Read I

7. Set PTR:=CREATENEWNODE(I)

8. Set PTR->NEXT:=TOP

9. Set TOP:=PTR

10. Print,'\nDo you want to enter more (y/n)?'

11. Read CH

12. Print, '\nPress any key to continue...'

13. Print, 'Enter the element to be deleted:'

14. Read I

15. Set POS:=SEARCHDEL(I)

16. If POS=-1, then

Print, '\n', I, ' not found in Linked List.\nThus, it can't be deleted.'

Else

Print, '\n', I, ' found at position ', POS, ' and deletion accomplished successfully.'

17. Print, '\nThe Linked List after the whole procedure is...\n'

18. Call DISPLAY()


19. Exit
[Algorithm for the function CREATENEWNODE()]
NODE *CREATENEWNODE(N)

1. Set PTR := new NODE

2. Set PTR->INFO := N

3. Set PTR->NEXT := NULL

4. Return PTR
[Algorithm for the function DISPLAY()]
DISPLAY(*N)

1. While N != NULL, follow steps 2-3

2. Print, N->INFO,'-->'

3. Set N:=N->NEXT

4. Print,'!!!\n'

5. Return
[Algorithm for the function SEARCHDEL()]
INT SEARCHDEL(N)

1. Set *NP:=TOP

2. If NP->INFO=N, then

Set TOP:=TOP->NEXT

Return 1

3. Set I:=2

4. Set SAVE:=TOP

5. Set NP:=TOP->NEXT

6. While NP!=NULL, follow steps 7-10

7. If NP->INFO=N, then

Set SAVE->NEXT=NP->NEXT

Return I

8. Set SAVE:=NP

9. Set NP:=NP->NEXT

10. Set I:=I+1

11. Return -1
[Algorithm demonstrating Quick Sort]
1. Set I:=0

2. Set N:=0

3. Print, '\nEnter the size of the array:'

4. Read N

5. Print, '\nEnter the array now...\n'

6. For I:=0 till I<N by 1, follow atep 7

7. Read ARR[I]

8. Call SORT(ARR,0,N)

9. Print, '\nSorted array is:\n'

10. For I:=0 till I<N by 1, follow step 11

11. Print, ARR[I], '\t'

12. Exit
[Algorithm of the function SWAP()]
SWAP(*A, *B)

1. Set T:=*A

2. Set *A:=*B

3. Set *B:=T
[Algorithm of SORT()]
SORT(ARR[], BEG, END)

1. Set K:=0

2. If END>BEG+1, then

Set PIV:=ARR[BEG]

Set L:=BEG+1

Set R:=END

While L<R, follow step a

a. If ARR[I]<=PIV, then

Set L:=L+1

Else

Call SWAP(&ARR[I], &ARR[--R])

Call SWAP(&ARR[--L], &ARR[BEG])

Call SORT(ARR, BEG, L)

Call SORT(ARR, R, END)


[Algorithm demonstrating Stacks in an Array]
1. Set TOP:=-1

2. Set CH:='y'

3. While CH='y' or CH='Y', follow steps 4-11

4. Print, 'Enter item for insertion:'

5. Read ITEM

6. RES:=PUSH(STACK, TOP, ITEM)

7. If RES=-1. then

Print, 'Overflow! Aborting!'

Exit

8. Print, '\nThe stack now is:\n'

9. Call DISPLAY(STACK, TOP)

10. Print, 'Wanna enter more elements(y/n)?'

11. Read CH

12. Print, 'Do you want to pop elements now(y/n)?'

13. Read CH

14. While CH='y' or CH='Y', follow steps 15-20

15. Set RES:=POP(STACK, TOP)

16. If TOP=-1, then

Print, '\nUnderflow! Aborting!'

Exit

17. Print, '\nThe stack now is:\n'

18. Call DISPLAY(STACK, TOP)


19. Print, 'Wanna pop more elements(y/n)?'

20. Read CH

21. Exit
[Algorithm for the function PUSH()]
INT PUSH(STACK[], &TOP, ELE)

1. If TOP=SIZE-1, then

return -1

Else

TOP:=TOP+1

STACK[TOP]:=ELE

2. Return 0
[Algorithm for the function DISPLAY()]
DISPLAY(STACK[], TOP)

1. Print, STACK[TOP], '<--', '\n'

2. For I:=TOP-1 till I>=0 by -1, follow step 3

3. Print, STACK[I], '\n'

4. Return
[Algorithm for the function POP()]
POP(STACK[], &TOP]

1. If TOP=-1, then

Return -1

Else

RET:=STACK[TOP]

TOP:=TOP-1

2. Return RET
INDEX

S No Title Date Remarks


1 Program demonstrating Linear Search 14.01.2010
2 Program demonstrating Binary Search 14.01.2010
3 Program demonstrating Bubble Sort on Integers 21.01.2010
4 Program demonstrating Bubble Sort on Strings 21.01.2010
5 Program demonstrating Insertion Sort 28.01.2010
6 Program demonstrating Selection Sort 28.01.2010
7 Program demonstrating Quick Sort 04.02.2010
8 Program demonstrating Linked List Operations (Traversal) 11.02.2010
9 Program demonstrating Linked List Operations (Search) 11.02.2010
10 Program demonstrating Linked List Operations (Insertion) 11.02.2010
11 Program demonstrating Linked List Operations (Deletion) 11.02.2010
12 Program demonstrating Stacks in an Array 25.02.2010
13 Program demonstrating Stacks in a Linked List 25.02.2010
14 Program demonstrating Queue in an Array 04.03.2010
15 Program demonstrating Circular Queue in an Array 04.03.2010

NOTE: Each program unit contains Algorithm, Flowchart, Program Code and Output in the
order specified.
DATA STRUCTURES PRACTICAL
FILE

Submitted to:
Mr. Rahul Aggarwal

Submitted by:
Anamjyot Kaur
08104006
Elec & Comm Engg

Vous aimerez peut-être aussi