Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lesson 1
(Revision)
Objectives
Introduction to Arrays
Algorithms dealing with Arrays
Introduction to Arrays
The arrays are defined just like variables in C++, however, in addition to the type, we
also mention the size of the array. The items in an array are called elements, it is worth
noting here that the elements of an array are of the same type. The syntax is:
1. Loc = 0
2. FOR i = 0 to n - 1
1. IF ai = x THEN
1. Loc = i
2. End algorithm
AJ/Handout 1 -2- Object Oriented Programming
return 0;
AJ/Handout 1 -3- Object Oriented Programming
}
Algorithm to sort a sequence of elements called Bubble Sort or Maximum Last
Find the largest element in sequence and place at the end,
Then find largest element from 1 to n -1, and place at n -1 location and so on
1. FOR j = n - 1 down to 1
1. M = a0
2. Loc = 0 ; for initialization
3. FOR i = 1 to j
1. IF ai > M THEN ; new candidate for the maximum
1. Loc = i
2. M = ai
4. aLoc = aj ; moving aj to the place where M was found
5. aj = M ; finishing the swap
0 1 2 3 4 5 6 7 8 9 Array index
Iteration 1 12 2 1 5 23 29 16 75 20 15 Values
of outer loop i ← j For loop pointers
0 1 2 3 4 5 6 7 8 9 Array index
Iteration 2 12 2 1 5 23 29 16 15 20 75 Values
of outer loop i ← j For loop pointers
0 1 2 3 4 5 6 7 8 9 Array index
Iteration 3 12 2 1 5 23 20 16 15 29 75 Values
of outer loop i ← j For loop pointers
Outer loop continues till j becomes 1, and then values of a[0] and a[1] are sorted in same fashion as above.
}
Algorithm to sort a sequence of elements called Selection Sort
Start from location 1, find smallest item in the remaining sequence and swap with location 1
Then move to location 2, find smallest item in remaining sequence and swap with location 2
And so on …
1. FOR i = 0 to n-1
1. Loc = i ; assuming ith element is minimum
2. FOR j = i+1 to n-1
1. IF aj < aLoc THEN ; finding the index Loc of minimum element
1. Loc = j
3. temp = ai
4. ai = aLoc ; swapping the minimum element to ith loc
5. aLoc = temp ; finishing the swap
0 1 2 3 4 5 6 7 8 9 Array index
Iteration 1 23 1 4 2 6 9 11 5 16 7 Values
of outer loop i j → For loop pointers
0 1 2 3 4 5 6 7 8 9 Array index
Iteration 2 1 23 4 2 6 9 11 5 16 7 Values
of outer loop i j → For loop pointers
0 1 2 3 4 5 6 7 8 9 Array index
Iteration 3 1 2 4 23 6 9 11 5 16 7 Values
of outer loop i j → For loop pointers
Outer loop continues till i becomes 9, and then values are sorted in same fashion as above.
int main ( )
{
const int s=10;
int j,i,temp,min;
int a[s]= {23, 1, 4, 2, 6, 9, 11, 5, 16, 7};
Algorithm to search for an element from a sorted sequence, called Binary Search
1. L = 0 ;Low is first element
2. H = n-1 ;High is last element
3. Loc = 0 ;when element is found location points to it
4. WHILE H – L > 0 ;while Not down to one element
1. K = (L + H)/2 ;Midpoint of High and Low
2. IF x = ak THEN ;success
1. Loc = k
2. End Algorithm
OTHERWISE
3. IF x < ak THEN ;K is too high
1. H = K - 1
2. IF H < L THEN H = L ;happens when previous H was previous L+1
4. IF x > ak THEN ;K is too low
1. L =K+1
5. IF aH = x THEN Loc = H ;success or Fail at end;
0 1 2 3 4 5 6 7 8 9 Array index
1 2 4 5 6 7 9 11 16 23 Values
Iteration 3 L,K H For loop pointers
int main ( )
{
const int n=10;
int H, L, K, m, i, j, x, s;
int a[n]= {75, 20, 8, 5, 3, 4, 9, 1, 2, 15}; //Unsorted sequence
cout << "\nThe unsorted sequence is: ";
for (i=0; i<n ; i++) cout << a[i] << " ";
while ( H - L > 0 )
{
K = (H + L) / 2; //find mid point
if (x < a[K]) //K is too high
{
H = K - 1;
if (H < L) H = L; //happens if previous H L+1
}
if (x > a[K]) //K is too low
L = K + 1;
}
if ( x < a[L]) m = L; //H=L here, success
else m = L + 1;
return 0;
}