1 two-dimensional arrays. 2 can be visualized as consisting m rows, each of n columns syntax:...

Post on 22-Dec-2015

215 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Two-Dimensional Arrays

2

Two-Dimensional Arrays

• Can be visualized as consisting m rows, each of n columns

Syntax:

datatype arrayname [row] [ column] ;

Example:

int val[3] [4] = {8, 16, 9, 52,

3, 15, 27, 6,

14, 25, 2, 10};

3

Memory layout of two-dimensional arrays

Users Perception or interpretation

Actual memory layout:

2 3 1 4 5

3 5 7 3 1

2 3 1 4 5 3 5 7 3 1

4

• Initialization:

#define NUMROWS 3#define NUMCOLS 4int val[NUMROWS][NUMCOLS] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} };

• The inner braces can be omitted:int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};

• Initialization is done in row order

Two-Dimensional Arrays

5

Two-Dimensional Arrays#define NUMROWS 3#define NUMCOLS 4 int val [NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};

6

Initializing Two-Dimensional Arrays Elements

• Can be initialized during declaration

• Values are grouped by rows

• Example:

int scores[3][3]= {{3,5,7},

{2,4,6},

{11,9,13}};

7

Reading in the two-dimensional array elements

• Read values from the keyboard or a file

• Can be done using a loop (for loop)

• Example:

int x[3][3];

int i, j;

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

{

printf(“Enter the values in row %d: ”, i+1);

for (j = 0; j < 3; j++)

scanf (“%d”, &x[i][j]);

}

8

Displaying in the two-dimensional array elements

• Example:

int x[3][3]= {{3,5,7},{2,4,6},{11,9,13}};

int i, j;

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

{

printf("Displaying values for row %d: \n", i+1);

for (j = 0; j < 3; j++)

printf("%d ", x[i][j]);

printf("\n");

}

9

Sorting Array Values

10

Sorting Array Values

• To arrange the values in array in certain logical order.

• Various sorting algorithmsSelection sortBubble sortInsertion sortQuick sort

11

The Selection Sort algorithm

• List is divided into two sub lists, sorted and unsorted, which is divided by imaginary wall.

• Uses n-1 passes (where n is the number of elements)

• For each pass, the smallest element selected and placed to its correct position.

12

How selection sort works1. Find the minimum value in the list

2. Swap it with the value in the first position

3. Repeat the steps above for remainder of the list (starting at the second position)

• Example of this sort algorithm sorting a total of six elements:

13

Code for a simple selection sort for(x=0; x<n; x++)

{ index_of_min = x;

for(int y=x; y<n; y++)

{ if(array[index_of_min]<array[y])

index_of_min = y;

temp = array[x];array[x] = array[index_of_min];

array[index_of_min] = temp;

}

}

(swapping values)

*Refer to note : Exchanging values in an array

14

Bubble Sort

• Most popular, easiest to understand

• Values are swapped until list is sorted

• “Bubble”-like movement of lowest value to top of list

7 4 1 9 2

4 7 1 9 2

4 1 7 9 2

4 1 7 9 2

4 1 7 2 9

*Please note that all this results after only one iteration.  This loop will repeat itself until all values are sorted in increasing order. 

The order is ok (7 & 9), no need to swap

Swap 7&4

Swap 7&1

Swap 9&2

15

The Bubble Sort algorithm

• Uses n passes (where n is the number of elements)

• For each pass, an element is compared with the next element down the list and swapped if not in correct order.

for (i = (array_size - 1); i >= 0; i--)

{ for (j = 1; j <= i; j++)

{

if (numbers[j-1] > numbers[j])

{ temp = numbers[j-1];numbers[j-1] =

numbers[j]; numbers[j] = temp;

}

}

}

16

The Insertion Sort algorithm

• Uses n-1 passes

• For each pass, the smallest element selected and placed to its correct position.

for (i=1; i<n; i++) { j=i; 2

t=a[j]; while (j>0 && a[j-1]>t) {

a[j]=a[j-1]; j--;

}a[j]=t;

}

17

Insertion Sort Algorithm

• Here is an example:5 7 0 3 4 2 6 1 (0)

5 7 0 3 4 2 6 1 (0)

0 5 7 3 4 2 6 1 (2)

0 3 5 7 4 2 6 1 (2)

0 3 4 5 7 2 6 1 (2)

0 2 3 4 5 7 6 1 (4)

0 2 3 4 5 6 7 1 (1)

0 1 2 3 4 5 6 7 (6)

For each iteration, the number of positions the inserted element has moved is shown in brackets.

On the left side the sorted part of the sequence is shown in red.

18

Searching Array Values

19

Searching Arrays

• The process used to find the location of a target among a list of objects

• In the case of an array, searching means that given a value, we want to find the location (index) of the first element in the array that contains the value.

• Types of SearchSequential or Linear Search Binary Search

20

Sequential / Linear Search

• Easy, but inefficient

• Start at the beginning and stop when a match is found

• Does not require arrays to be sorted

21

Pseudocode for linear search technique

• For each item in the list. Check to see if the item you're looking for

matches the item in the list. • If it matches, return the location where you found

it (the index). • If it does not match, continue searching until you

reach the end of the list.– If we get here, we know the item does not exist in the list.

22

Sequential / Linear Search (Example)

23

Binary Search

• For sorted list or arrays only

• Compares the search value with value in the middle.

• Splits the list into two: works with half the list each time until a match is found

• Faster

top related