5. Insert value at index
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void insertValue(int[] arr, int value, int index)
2. {
3. for (int i = arr.Length - 1; i > index; i--)
4. arr[i] = arr[i - 1];
5. arr[index] = value;
6. }
In the previous code:
if the loop start from the wanted index up to the length of array,
a logical error will be appeared, (What is it ?)
6. Insert value at suitable position
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0
15
14
To insert the value at suitable position (in order)
2 7 80 88 90 999 12 15 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0 14
1 2 3 4 5 6 7 8 9 9
array
0
2 7 80 88 90 999 12 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
99
14
Null
1. Search for the index of value 15.
2. Shift the value to the left (to free the wanted position)
3. Insert the new value (15)
7. Insert value at suitable position
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void insertValueInOrder(int[] arr, int value)
2. {
3. // to find the position of value
4. int i;
5. for ( i = 0; i < arr.Length-1; i++)
6. {
7. if (id[i] > value)
8. {
9. break;
10. }
11. }
12. // shift value to free wanted position
13. for (int j = arr.Length - 1; j > i; j--)
14. arr[j] = arr[j - 1];
15. arr[i] = value;
16. }
Self Study:
What about, if
all the values in
array is smaller
than the new
value?
9. Delete value from index
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void deleteValue(int[] arr, int index)
2. {
3. // overwrite on position of value
4. for (int k =index ; k < id.Length-1; k++)
5. id[k] = id[k + 1];
6. }
10. To delete the value
1. Search for the index of value 15.
2. Overwrite on the index of value from it the last position.
Delete value
mfarra.cst.ps www.fb.com/MahmoudRFarra
2 7 80 88 90 999 12 15 23 30 45 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0
15
14
2 7 80 88 90 999 12 15 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
array
0 14
1 2 3 4 5 6 7 8 9 9
array
0
2 7 80 88 90 999 12 23 30 45 15 77
1 2 3 4 5 6 7 8 9 10 11 12 13
99
14
Null
11. Delete value
mfarra.cst.ps www.fb.com/MahmoudRFarra
1. public void deleteValue(int[] arr, int value)
2. {
3. // to find the position of value
4. int i;
5. for ( i = 0; i < arr.Length-1; i++)
6. {
7. if (id[i] == 12)
8. {
9. break;
10. }
11. }
12. // overwrite on position of value
13. for (int k =i ; k< id.Length-1; k++)
14. id[k] = id[k + 1];
15. }
Self Study:
What is the
result, if the
wanted value
does not found
in array ?
12. Time Complexity
mfarra.cst.ps www.fb.com/MahmoudRFarra
To insert on value in specific index in array with size
(n), we need to shift all values of array (worst state),
this mean the time complexity is O(n).
What about delete ?
13. Time Complexity
mfarra.cst.ps www.fb.com/MahmoudRFarra
To insert on value in suitable index in array with size
(n), we need to:
1. Search for the suitable position by linear approach
(O(n)).
2. Shift all values of array (worst state), this mean the
time complexity is O(n).
The total complexity O(2n).
What about delete ?
14. using Java
2015
FB: M a h m o u d R F a r r a
YouTube: M a h m o u d R F a r
SlidesShare: mralfarra
Thank you