3 array operations
Post on 15-Jan-2017
392 Views
Preview:
TRANSCRIPT
using Java
2015
Data Structure Prepared by: Mahmoud Rafeek Al-farra
in Java
3. Array Operations
mfarra.cst.ps www.fb.com/MahmoudRFarra
Contents
Complexity
Insert in array (index, in order)
Delete from array (index, value)
Revision
Introductionmfarra.cst.ps www.fb.com/MahmoudRFarra
Array’s
Operations
Insert
At suitable position
At index
Delete
Specific value
Value of index
Insert value at indexmfarra.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
13
14
To insert value at index 4 (5’th position)
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 14
13
Insert value at indexmfarra.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 ?)
Insert value at suitable positionmfarra.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)
Insert value at suitable positionmfarra.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?
Delete value from indexmfarra.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 delete the value of 5’th cell
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
Delete value from indexmfarra.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. }
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 valuemfarra.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
Delete valuemfarra.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 ?
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 ?
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 ?
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
top related