Sei sulla pagina 1di 7

AJ/Handout 1 -1- Object Oriented Programming

Lesson 1
(Revision)

Objectives

 Introduction to Arrays
 Algorithms dealing with Arrays

Introduction to Arrays

Generally we group items together to process them in a sequential manner. For


example, list of student names, list of items in an inventory, list of student numbers in
all classes of BIIT, list of scores in a quiz, etc. Examples of sequential data are
numerous, and such data can be stored in C++ using arrays (one dimensional). I
explicitly mention one dimensional, since, multidimensional arrays will be dealt
somewhat later in the course. It is worth mentioning here that arrays are not the only
means by which we represent sequential data in lists, there are other data structures
which are also available for this purpose, but arrays are the simplest.

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:

Type_of_elements_in_array Array_name [size] ;

int student_ages [12]; // array student_ages with 12 elements of type int

#include <iostream> // Array example 1


using namespace std;
int main ( )
{
int age[12]; // array “age” of 12 integers
for (int j = 0 ; j < 12 ; j++ ) // get ages
{
cout << “Enter an age: “;
cin >> age[j]; // assign age to array elements
}
for (j = 0 ; j < 12 ; j++ )
cout << “You entered “ << age[j] << endl;
return 0;
}

Algorithms dealing with Arrays

Algorithm for Linear Search in a sequence “a”

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

#include <iostream> //C++ implementation of Linear Search algorithm


using namespace std;
int main()
{
int x, loc;
int a[10]; //array “a” of 10 integers
for (int i=0 ; i < 10 ; i++) //get numbers for the sequence in array
{
cout << endl << i << “ enter number: “;
cin >> a[i];
}
cout << endl << “Enter number to be searched in sequence: “ ;
cin >> x;
loc = 0; //algorithm for linear search
for ( i=0 ; i<10 ; i++)
if (a[i] == x) loc = i; //has a logical bug! try {loc = i; break;}
cout << endl << “The number is at: " << loc << endl;
return 0;
}

Algorithm to find the largest number in a sequence


1. M = a0
2. Loc = 0
3. FOR i = 1 to n - 1
1. IF ai > M THEN
1. M = ai
2. Loc = i

#include <iostream> //C++ implementation of Largest number algorithm


using namespace std;
int main()
{
int m, loc;
int a[10]; //array “a” of 10 integers
for (int i=0 ; i < 10 ; i++) //get numbers for the sequence in array
{
cout << endl << i << “ enter number: “;
cin >> a[i];
}

m = a[0]; //algorithm for largest number in a sequence


loc = 0;
for ( i=1 ; i<10 ; i++)
if (a[i] > m)
{
m = a[i];
loc = i;
}
cout << endl << “Largest number is: " << m << “ at location: “ << loc << endl;

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.

#include <iostream> //C++ implementation of Maximum Last algorithm


using namespace std;
int main( )
{
int m, loc, i, j;
int a[10]; //array “a” of 10 integers
for ( i = 0 ; i < 10 ; i++) //get numbers for the sequence in array
{
cout << endl << i << “ enter number: “;
cin >> a[i];
}
for ( j = 9 ; j >= 1 ; j--) //main algorithm for sorting the sequence
{
m = a[0];
loc = 0;
for ( i = 1 ; i <= j ; i++)
if (a[i] > m)
{
loc = i;
m = a[i];
}
a[loc] = a[j];
a[j] = m; //swap the largest number to a[j]
}
for (i = 0 ; i < 10 ; i++) cout << endl << i << “ Sorted number: “ << a[i]; //print sorted list
return 0;
AJ/Handout 1 -4- Object Oriented Programming

}
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.

#include <iostream> //Selection Sort


using namespace std;

int main ( )
{
const int s=10;
int j,i,temp,min;
int a[s]= {23, 1, 4, 2, 6, 9, 11, 5, 16, 7};

for (i=0 ; i < s ; i++)


{
min = i; //Assuming that ith element is minimum
for (j = i+1 ; j < s ; j++)
if ( a[j] < a[min] ) min = j; //find index of minimum element
temp = a[i];
a[i] = a[min]; //swap ith element with minimum el
a[min] = temp;
}

for (i=0 ; i<s ; i++) /display sorted sequence


cout << a[i] << " ";
return 0;
}
AJ/Handout 1 -5- Object Oriented Programming

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;

Searching for 2, and then 5 in the following sorted sequence of elements


0 1 2 3 4 5 6 7 8 9 Array index
1 2 4 5 6 7 9 11 16 23 Values
Iteration 1 L K H For loop pointers

If searching for 0 1 2 3 4 5 6 7 8 9 Array index


2 then stop here 1 2 4 5 6 7 9 11 16 23 Values
Iteration 2 L K H For loop pointers

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

If searching for 0 1 2 3 4 5 6 7 8 9 Array index


5 then stop here 1 2 4 5 6 7 9 11 16 23 Values
Iteration 4 L,H For loop pointers

#include <iostream> //Binary Search


using namespace std;
int main ( )
{
const int n=10;
int H, L, K, Loc, i, x, a[n]= {1, 2, 4, 5, 6, 7, 9, 11, 16, 23};
cout << "\nThe sequence is: "; for (i=0; i<n ; i++) cout << a[i] << " ";
cout << "\nEnter number to search:"; cin >> x;
L = 0;
H = n - 1;
Loc = 100; //to show out of range
while ( H - L > 0 )
{ K = (H + L) / 2; //find mid point
if ( x == a[K])
{ Loc = K; //Success
break;
}
else if (x < a[K]) //K is too high
{ H = K - 1;
if (H < L) H = L; //happens if previous H is previous L+1
}
else if (x > a[K]) //K is too low
L = K + 1;
}
if ( x == a[H]) Loc = H; //H=L here, we are out of loop, success
cout << "\nThe Loc is: " << Loc << endl;
return 0; }
AJ/Handout 1 -6- Object Oriented Programming

Algorithm to sort a sequence of elements called Insertion Sort

1. FOR j = 1 to n-1 ; First Locate m then Insert aj at m


1. x = aj ; Locate based on Binary Search
2. L = 0 ; Low is first element
3. H = j-1 ; High is last element
4. m = 0 ; insertion point
5. WHILE H – L > 0 ; while Not down to one element
1. K = (L + H) / 2
2. IF x < ak THEN ;K is too high
1. H = K - 1
2. IF H < L THEN H = L
3. IF x > ak THEN ;K is too low
1. L =K+1
6. IF x < aL THEN
1. m=L
OTHERWISE
2. m =L+1

7. s = aj ; Insert aj at location m by pushing am to aj-1 up


8. FOR i = j down to m + 1 ; Push up by
1. ai = ai-1 ; replacing each term with one preceding it
9. am = s ; Insert saved value of aj

Insert 5 at m by 0 1 2 3 4 5 6 7 8 9 Array index


Pushing am to 1 2 8 5 3 4 9 75 20 15 Values
aj-1 up by 1 m j For loop pointers

Insert 3at m by 0 1 2 3 4 5 6 7 8 9 Array index


Pushing am to 1 2 5 8 3 4 9 15 20 75 Values
aj-1 up by 1 m j For loop pointers

Insert 4 at m by 0 1 2 3 4 5 6 7 8 9 Array index


Pushing am to 1 2 3 5 8 4 9 15 29 75 Values
aj-1 up by 1 m j For loop pointers

#include <iostream> //Insertion Sort


using namespace std;

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] << " ";

for (j=1 ; j < n ; j++)


{
x = a[j]; //Locate insertion point m
L = 0;
H = j - 1;
m = 0;
AJ/Handout 1 -7- Object Oriented Programming

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;

s = a[j]; //Insertion of a[j] at m in sequence


for (i = j ; i > m ; i--)
a[i] = a[i-1];
a[m] = s;
}

cout << "\nThe sorted sequence is: ";


for (i=0; i<n ; i++) cout << a[i] << " ";

return 0;
}

Potrebbero piacerti anche