You are on page 1of 23

DS & Algorithms:2013

DATA STRUCTURES &


ALGORITHMS

Iftikhar Muhammad
1

Iftikhar 1
DS & Algorithms:2013

Arrays Operations
• Traversal
• Inserting
• Deletion
• Search
• Sorting

Iftikhar 2
DS & Algorithms:2013

Traversal
• Traversing:
• In traversing operation, each element of an array is accessed
exactly once for processing. This is called visiting of the array.
• To compute the sum of values of each element of an array.
• Display values of each element of an array.
• Algorithm:
• 1. Set k=LB
• 2. Repeat Step 3 and Step 4 while k<=UB
• 3. Apply PROCESS to LA[K]
• 4. Set K=K+1
• 5. [End of Step 2 loop]
• 6. Exit

Iftikhar 3
DS & Algorithms:2013

Traversal Algo
• Find the Number NUM of years during which
more than 300 automobiles were sold.
• 1. Set NUM=0
• 2. Repeat for K=1932 to 1984
• 3. If AUTO[K]>300, then: Set NUM=NUM +1.
• 4. [End of Loop]
• 5. Return

Iftikhar 4
DS & Algorithms:2013

Inserting
• In inserting operation , new data items are added or inserted into
any location of an empty or filled array.
• In case of empty array, new values are stored in empty locations.
• In case of filled array , old values are either replaced by new values
or pushed backward to make room for new values.
• It is very easy to insert data at the end of array(Why?).
• Without disturbing the data of other elements of the array.

Iftikhar 5
DS & Algorithms:2013

Inserting
• Algorithm- Inserting value at specified location

1. Set J=N
2. Repeat Step 3 and Step 4 while J>=K
3. Set LA[J+1]=LA[J]
4. Set J=J-1
5. [End of Step 2 loop]
6. Set LA[K]=ITEM
7. Set N=N+1
8. Exit

Iftikhar 6
DS & Algorithms:2013

Deletion
• Deleting item from a specified Location
• When the data item is removed from a specified location within an
array, the items from that location up to the end of array are moved
one location towards the beginning of the array.
• Algorithm:
1. Set ITEM=LA[K]
2. Repeat for J=K to N-1
3. Set LA[J]=LA[J+1]
4. [End of loop]
5. Set N=N-1
6. Exit

Iftikhar 7
DS & Algorithms:2013

Search
• The Process of finding a specific data item and its location in an
array is called searching.
• Successful- If found
• Terminated- If data item found
• Unsuccessful
• Searching Operation in data structures is used for data modification
together with inserting, deleting and updating.
• For example , when a data item is to be deleted , it is first searched
and then deleted, if found.
• The most commonly used search algorithms are as follows.
• i. Sequential Search
• Ii. Binary Search

Iftikhar 8
DS & Algorithms:2013

Search
• The sequential search is a slow process and is used for only small lists of data.
• The Method is not recommended for large amount of data because some more efficient
methods are available for large and complex searches.
• Algorithm: Linear Search
1. SET LOC=-1
2. INPUT N values into array XYZ
3. INPUT VAL
4. Repeat Step 5 For I= 1 TO N
5. IF VAL = XYZ[I] THEN
6. LOC=1
7. Print “value found at location”, LOC
8. Exit
9. END IF
10. If LOC=-1 THEN
11. PRINT ”Value is not Found”
12. END IF
13. EXIT

Iftikhar 9
DS & Algorithms:2013

Search(Binary Search)
1. Set BEG=LB, END=UB and MID=INT((BEG+ENG)/2
2. Repeat Steps 3 and 9 while BEB<=ENG and DATA[MID]!=ITEM
3. If ITEM<DATA[MID],then
4. Set END=MID-1
5. Else
6. Set BEG=MID+1
7. END IF
8. Set MID=INT((BEG+END))/2
9. END OF LOOP
10. If DATA[MID]=ITEM, then
11. Set LOC=MID
12. Else
13. Set LOC=NULL
14. END IF
15. EXIT

Iftikhar 10
DS & Algorithms:2013

Time complexity of linear search?


• Best case: 1
• step Average case: n/2 steps
• Worse case: n steps

Iftikhar 11
DS & Algorithms:2013

Analysis of Binary Search algorithm


• Best case: 1
• Worse case: O(log n)
• Average case: O(Log n)
• The average cost of a successful search is
about the same as the worst case.

Iftikhar 12
DS & Algorithms:2013

Worst case analysis of binary search

Iftikhar 13
DS & Algorithms:2013

Average case analysis of binary search

Iftikhar 14
DS & Algorithms:2013

Average case analysis of binary search

Iftikhar 15
DS & Algorithms:2013

Average case analysis of binary search

Iftikhar 16
DS & Algorithms:2013

SelectionSort

Iftikhar 17
DS & Algorithms:2013

Analysis of Selection Sort

Iftikhar 18
DS & Algorithms:2013

Iftikhar 19
DS & Algorithms:2013

Bubble Sort Algorithm


1. for(int i=0; i<n; x++)
2. for(int j=0; j<n-1; y++)
3. if(array[j]>array[j+1])
4. int temp = array[j]
5. array[j] = array[j+1]
6. array[j+1] = temp

Iftikhar 20
DS & Algorithms:2013

Bubble Sort Time Complexity

Called Linear Time


• Best-Case Time Complexity O(N)
Order-of-N
– Array is already sorted
– Need 1 iteration with (N-1) comparisons

Called Quadratic Time


O(N2)
• Worst-Case Time Complexity Order-of-N-square
– Need N-1 iterations
– (N-1) + (N-2) + (N-3) + …. + (1) = (N-1)* N / 2

Iftikhar 21
DS & Algorithms:2013

• Complexity is measured based on time consume in
operations to compare and swap elements.
• Number of comparisons
• – a for loop embedded inside a while loop
• – Worst Case (n‐1)+(n‐2)+(n‐3) …+1 , or O(n^2)
• – Best Case –(n‐1) or O(n)
• Number of Swaps
• – inside a conditional ‐> #swaps data dependent !!
• – Best Case 0, or O(1)
• – Worst Case (n‐1)+(n‐2)+(n‐3) …+1 , or O(n2)

Iftikhar 22
DS & Algorithms:2013

Iftikhar 23

You might also like