Dsd Lecture 1

29

Transcript of Dsd Lecture 1

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 1/29

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 2/29

Data Structure

 A data structure is a

specialized format for organizing and

storing data. General data structure

types include the array, the file,the record, the table, the tree, and so on

 A data structure is a particular way of 

storing and organizing data in acomputer so that it can be

used efficiently.

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 3/29

 Array

 An array is composed of a series of 

elements of one data type.

 An array declaration tells the compiler 

how many elements the array containsand what the type is for these elements

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 4/29

Example

float candy[365]; /* array of 365 floats */

char code[12]; /* array of 12 chars */

int states[50]; /* array of 50 ints */

The brackets ([]) identify candy and the rest asarrays, and the number enclosed in the bracketsindicates the number of elements in the array.

To access elements in an array, you identify anindividual element by using its subscript number,also called its index. The numbering starts with 0.Hence, candy[0] is the first element of the candyarray, and candy[364] is the 365th and last element.

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 5/29

Initialization

 Arrays are often used to store data needed for aprogram. For example, a 12-element array can storethe number of days in each month. In cases such asthese, it's convenient to initialize the array at thebeginning of a program.

Example:int days[MONTHS] =

{31,28,31,30,31,30,31,31,30,31,30,31};

const int days[] = {31,28,31,30,31,30,31,31,30,31};

Note:

When you use empty brackets to initialize an array, thecompiler counts the number of items in the list andmakes the array that large.

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 6/29

Assigning Array Values

 After an array has been declared, you

can assign values to array members by

using an array index, or subscript.

int oxen[SIZE] = {5,3,2,8};

int yaks[SIZE];

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 7/29

 Array Bounds

You have to make sure you use arrayindices that are within bounds; that is, youhave to make sure they have values validfor the array. For instance, suppose you

make the following declaration:int doofi[20];

Then it's your responsibility to make sure

the program uses indices only in the range0 through 19, because the compiler won'tcheck for you.

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 8/29

Specifying an Array Size

int arr[SIZE]; // symbolic integer constant

double lots[144]; // literal integer 

constant

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 9/29

Example 1:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 10/29

Example 1:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 11/29

Example 2

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 12/29

Example 2

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 13/29

Two-Dimensional Array

 A two-dimensional array, sometimes referred to as a table, consists of both rows and columns of elements. For example, the following array of numbers is called a two-dimensional array of integers:

8 16 9 52

3 15 27 6

14 25 2 10

This array consists of three rows and four columns. To reserve storagefor this array, both the number of rows and the number of columns mustbe included in the array’s declaration. 

Calling the array val, the following is the correct specification for thistwo-dimensional array:

int val[3][4];

Similarly, the declarationsdouble volts[10][5];

char code[6][26];

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 14/29

Example:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 15/29

Multidimensional Arrays 

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 16/29

Example 1:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 17/29

Example 1:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 18/29

Example 2:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 19/29

Example 2:

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 20/29

The sorting problem

Input: a list of n elements 

Output: the same list of n elements rearranges inincreasing or decreasing order 

Why do we care so much about sorting?

sorting is used by many applications

Sorting is initial step of many algorithms

many techniques can be illustrated by studyingsorting

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 21/29

Sorting Algorithm

Bubble Sort

Selection Sort

Insertion Sort

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 22/29

Bubble Sort

Take multiple passes over the array

Swap adjacent places when values are

out of order  Invariant: each pass guarantees that

largest remaining element is in the

correct (next last) position

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 23/29

Bubble Sort

Start – Unsorted

Compare, swap (0, 1)

Compare, swap (1, 2)

Compare, no swap

Compare, noswap

Compare, swap (4, 5)

99 in position

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 24/29

Bubble Sort

Pass 2

swap (0, 1)

no swap

no swap

swap (3, 4)

21 in position

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 25/29

Bubble Sort

Pass 3

no swap

no swap

swap (2, 3)

12 in position, Pass 4

no swap

swap (1, 2)

8 in position, Pass 5

swap (1, 2)

Done

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 26/29

Selection Sort

Take multiple passes over the array

Keep already sorted array at high-end

Find the biggest element in unsorted part Swap it into the highest position in unsorted part

Invariant: each pass guarantees that one more

element is in the correct position (same as

bubbleSort)

a lot fewer swaps than bubbleSort!

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 27/29

Selection Sort

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 28/29

Insertion Sort

Take multiple passes over the array

Keep already sorted array at low-end

Find next unsorted element Insert it in correct place, relative to the ones

already sorted

Invariant: each pass increases size of 

sorted portion. Different invariant vs.

bubble and selection sorts.

7/28/2019 Dsd Lecture 1

http://slidepdf.com/reader/full/dsd-lecture-1 29/29

Insertion Sort