Array Operations
Array Operations
Operations
• Searching
• Sorting
• Insertion
• Deletion
• Merging
Searching
• Linear Search
• Binary Search
Linear Search
•Step 1 - Read the search element from the user.
•Step 2 - Compare the search element with the first element in the
list.
•Step 3 - If both are matched, then display "Given element is
found!!!" and terminate the function
•Step 4 - If both are not matched, then compare search element
with the next element in the list.
•Step 5 - Repeat steps 3 and 4 until search element is compared
with last element in the list.
•Step 6 - If last element in the list also doesn't match, then display
"Element is not found!!!" and terminate the function.
Linear Search
0 1 2 3 4 5 6 7
LIST 65 20 10 55 32 12 50 99
Search Element 12
Step 1:
Search element 12 is compared with first element 65
0 1 2 3 4 5 6 7
LIST 65 20 10 55 32 12 50 99
12
LIST 65 20 10 55 32 12 50 99
12
No Match found. So move to next element
Linear Search
Step 3:
Search element 12 is compared with Next element 10
0 1 2 3 4 5 6 7
LIST 65 20 10 55 32 12 50 99
12
No Match found. So move to next element
Linear Search
Step 4:
Search element 12 is compared with Next element 55
0 1 2 3 4 5 6 7
LIST 65 20 10 55 32 12 50 99
12
No Match found. So move to next element
Linear Search
Step 5:
Search element 12 is compared with Next element 32
0 1 2 3 4 5 6 7
LIST 65 20 10 55 32 12 50 99
12
LIST 65 20 10 55 32 12 50 99
12
Match found. Searching end. Display element found at position 6.
Binary Search
•Step 1 - Read the search element from the user.
•Step 2 - Find the middle element in the sorted list.
•Step 3 - Compare the search element with the middle element in the sorted list.
•Step 4 - If both are matched, then display "Given element is found!!!" and terminate the
function.
•Step 5 - If both are not matched, then check whether the search element is smaller or larger
than the middle element.
•Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for
the left sublist of the middle element.
•Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the
right sublist of the middle element.
•Step 8 - Repeat the same process until we find the search element in the list or until sublist
contains only one element.
•Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.
Binary Search
0 1 2 3 4 5 6 7
LIST 10 12 20 32 50 55 65 80
Search Element 12
Low = 0
High = 7
Mid = (Low + High)/2
Mid = (0+7)/2 = 3
a[mid] = a[3]=32
Mid Element 32
Binary Search
Step 1:
Search element 12 is compared with middle element 32
0 1 2 3 4 5 6 7
LIST 10 12 20 32 50 55 65 80
12
No Match found. As 12 is smaller than 32 search will be
performed only on the left side
Binary Search
0 1 2
LIST 10 12 20
Search Element 80
Low = 0
High = mid-1 (3-1)
Mid = (Low + High)/2
Mid = (0+2)/2 = 1
a[mid] = a[1]=12
Mid Element 12
Binary Search
Step 2:
Search element 12 is compared with middle element 12
0 1 2
LIST 10 12 20
12
Match found. So the result is “Element found at index 1
Binary Search
0 1 2 3 4 5 6 7
LIST 10 12 20 32 50 55 65 80
Search Element 80
Low = 0
High = 7
Mid = (Low + High)/2
Mid = (0+7)/2 = 3
a[mid] = a[3]=32
Mid Element 32
Binary Search
Step 1:
Search element 80 is compared with middle element 32
0 1 2 3 4 5 6 7
LIST 10 12 20 32 50 55 65 80
80
No Match found. As 80 is greater than 32 search will be
performed only on the right side
Binary Search
4 5 6 7
LIST
50 55 65 80
Search Element 80
Low = mid+1(4)
High = 7
Mid = (Low + High)/2
Mid = (4+7)/2 = 5
a[mid] = a[5]=55
Mid Element 55
Binary Search
Step 2:
Search element 80 is compared with middle element 55
4 5 6 7
LIST
50 55 65 80
80
No Match found. As 80 is greater than 55 search will be
performed only on the right side
Binary Search
6 7
LIST
65 80
Search Element 80
Low = mid+1(6)
High = 7
Mid = (Low+ High)/2
Mid = (6+7)/2 = 6
a[mid] = a[6]=65
Mid Element 65
Binary Search
Step 3:
Search element 80 is compared with middle element 65
6 7
LIST 65 80
80
No Match found. As 80 is greater than 65 search will be
performed only on the right side
Binary Search
7
LIST
80
Search Element 80
Low = 7
High = 7
Mid = (Low+ High)/2
Mid = (7+7)/2 = 7
a[mid] = a[7]=80
Mid Element 80
Binary Search
Step 4:
Search element 80 is compared with middle element 80
LIST 80
80
Match found. So the result is “Element found at index 7
Sorting
• Selection Sort
• Bubble Sort
Selection Sort
• Sorting Algorithm
• In-place Comparison based algorithm
• List is considered as two parts.
• Sorted part
• Unsorted Part
• Initially sorted part is empty and Unsorted part is the complete list
• Smallest element is selected from the unsorted array
• Swap the elements if necessary
• Steps
• Step 1 − Set MIN to location 0
• Step 2 − Search the minimum element in the list
• Step 3 − Swap with value at location MIN
• Step 4 − Increment MIN to point to next element
• Step 5 − Repeat until list is sorted
Selection Sort
14 33 27 10 35 19 42 44
14 33 27 10 35 19 42 44
As the minimum value is not in the first position, so swap the elements
Iteration 1: -
10 33 27 14 35 19 42 44
Selection Sort
10 33 27 14 35 19 42 44
Iteration 2: -
Rest of the array is compared. First position is perfect. Take Second
Position element 33 and find which one is smallest.
10 14 27 33 35 19 42 44
Iteration 3: -
10 14 19 33 35 27 42 44
Selection Sort
Iteration 4: -
10 14 19 27 35 33 42 44
Iteration 5: -
10 14 19 27 33 35 42 44
Iteration 6: -
10 14 19 27 33 35 42 44
Selection Sort
Iteration 7: -
10 14 19 27 33 35 42 44
Iteration 8: -
10 14 19 27 33 35 42 44
Bubble Sort
• Sorting Algorithm
• Adjacent element Comparison based algorithm
• Compare with the adjacent element and swapped if required.
• Steps
Bubble Sort
14 33 27 35 10
14 33 27 35 10
14 27 33 35 10
14 27 33 35 10
14 27 33 35 10
14 27 33 10 35
Bubble Sort
Iteration 2: -
14 27 33 10 35
14 27 33 10 35
14 27 33 10 35
14 27 33 1
10 35
14 27 33 10 35
14 27 10 33 35
14 27 10 33 35
14 27 10 33 35
Bubble Sort
Iteration 3: -
14 27 10 33 35
14 27 10 33 35
14 27 10 33 35
14 10 27 1
33 35
14 10 27 33 35
14 10 27 33 35
14 10 27 33 35
14 10 27 33 35
Bubble Sort
Iteration 4: -
14 10 27 33 35
10 14 27 33 35
10 14 27 33 35
10 14 27 1
33 35
10 14 27 33 35
10 14 27 33 35
10 14 27 33 35
10 14 27 33 35