Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
By Kapil Kumar R.
DS Notes 3rd Semester BCA (Section - A)
Array Algorithms: • Formula for Length of Array:- Length= UB- LB +1 where UB= Upper Bound of the array(largest index) LB= Lower Bound of the array(smallest index)
Address Calculation formula
The array elements are always stored in continuous memory locations. Formula for calculating address of data elements of an array:- LOC(LA[K])= Base(LA)+ w(K-LB) where LOC(LA[K])= Address of the element LA[K] of the array LA Base(LA)= Base address of the array LA w= Number of words per memory cell (1 for char,2 for int, 4 for float) and LB=Lower Bound of the array(smallest index)
Creating A Linear Array
Arrays can always be created and displayed(printed) with the help of a loop. For Example:- int arr[10]; int i; for(i=0;i<10;i++) { scanf(“%d”, &arr[i]); printf(“%d”, arr[i]); }
Traversing A Linear Array
Traversal of a linear array is the simplest. Algorithm for traversal=> TRAVERSE(LA,LB,UB) Here LA is a linear array with lower bound LB and upper bound UB. This algorithm traverses LA applying an operation PROCESS to each element of LA. 1. Set K=LB 2. Repeat Steps 3 and 4 while K<=UB 3. Apply PROCESS to LA[K] 4. Set K = K+ 1 [End of Step 2 loop] 5. Exit
Algorithm of Linear Search
1. Set K=0 and LOC=-1 2. Repeat Steps 3 and 4 while LOC=-1 and K<N 3. If DATA[K] =ITEM, then Set LOC=K 4. Set K = K + 1 [End of Step 2 loop] 5. If LOC= -1 then Write “ITEM is not in the array DATA” Else Write “LOC is the location of ITEM” [End of If structure] 6. Exit
Algorithm of Binary Search
1. Set BEG=LB, END=UB and MID=INT(BEG+ END)/2 2. Repeat steps 3 and 4 while BEG<=END and DATA[MID] ITEM 3. If ITEM < DATA[MID] then Set END = MID – 1 Else Set BEG= MID +1 [End of If structure] 4. Set MID=INT(BEG+ END)/2 [End of Step 2 loop] 5. If DATA[MID]= ITEM then Set LOC=MID Else Set LOC= NULL [End of If structure] 6. Exit
Algorithm for Bubble Sort
1. Repeat Steps 2 and 3 for K=1 to N-1 2.Set PTR = 1 3.Repeat while PTR<= N-K (a) If DATA[PTR]> DATA[ PTR +1] then Interchange DATA[PTR] and DATA[PTR +1] [End of If structure] (b) Set PTR= PTR +1 [End of inner loop] [End of Step 1 outer loop] 4. Exit
Algorithm for Insertion in Array
1. Set J=N 2. Repeat Steps 3 and 4 while J>=K 3. Set LA[J+1] = LA[J] 4. Set J = J – 1 [End of Step 2 loop] 5. Set LA[K]= ITEM 6. Set N= N + 1 7. Exit
Algorithm for Deletion of Array
1. Set ITEM = LA[K] 2. Set J=K 3. Repeat Steps 4 and 5 while J<=N-1 4. Set LA[J] = LA[J +1] 5. Set J= J+1 [End of Step 3 loop] 6. Set N = N – 1 7. Exit