Professional Documents
Culture Documents
Linear Data Structure
Linear Data Structure
Non
Linear
Linear
Implementation Approaches
In nonlinear data structures, a data element can be attached to several other data elements to
represent specific relationships that exist among them.
C/C++ Java
N>0 N>0
Low High
Mid
Created by Saba Anwar, edited by Asmara safdar Computer 10/02/2017
Science Department- CIIT Lahore
Binary Search
Algorithm:BINARY_SEARCH(A, K, V)
Input: Sorted Array in ascending order , lower and upper index of list, value to be searched
Output: index of value if found
Steps:
Start
1. Set low=0, high=K-1 The binary search gets its name
2. While (low <= high) because the algorithm
3. mid = (low + high) / 2; continually divides the list into
4. If (V < A[mid]) //If value in the left half two parts.
5. high = mid - 1; //Update high index
6. Else If (V > A[mid]) //If value in the right half Binary search algorithm is good
7. low = mid + 1; //Update the low index for larger arrays.
8. Else If array size is very small, there
9. return mid; // value == Array[mid] is not huge difference between
10. End If linear search and binary search
11. End While algorithm time
12. return -1; //index was not found
End
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 56 78 41
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 56 78 41
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 56 78 78 41
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 56 56 78 41
Insert 10 at index 2
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 10 56 78 41
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 56 78 41
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 78 78 41
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 78 41 41
After Deletion
Index 0 1 2 3 4 5 6 7 8 9
Value 25 12 78 41
0 1 2 3 4 5
8 20 15 -5 7 -55
Min_index
0 1 2 3 4 5 0 1 2 3 4 5
8 20 15 -5 7 -55 8 20 15 -5 7 -55
Is this number Is this number
< number at < number at
Min_index? Min_index Min_index? Min_index
0 1 2 3 4 5 0 1 2 3 4 5
8 20 15 -5 7 -55 8 20 15 -5 7 -55
0 1 2 3 4 5
Is this number
8 20 15 -5 7 -55 < number at
Min_index?
0 1 2 3 4 5
8 20 15 -5 7 -55
0 1 2 3 4 5 0 1 2 3 4 5
8 20 15 -5 7 -55 -55 20 15 -5 7 8
0 1 2 3 4 5 0 1 2 3 4 5
8 20 15 -5 7 -55 -55 20 15 -5 7 8
Swap
0 1 2 3 4 5 0 1 2 3 4 5
-55 -5 15 20 7 8 -55 -5 7 20 15 8
0 1 2 3 4 5 0 1 2 3 4 5
-55 -5 7 8 15 20 -55 -5 7 8 15 20
0 1 2 3 4 5
-55 -5 7 8 15 20