Anda di halaman 1dari 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 a computer so that it can be used efficiently.

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 contains and what the type is for these elements

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 as arrays, and the number enclosed in the brackets indicates the number of elements in the array. To access elements in an array, you identify an individual element by using its subscript number, also called its index. The numbering starts with 0. Hence, candy[0] is the first element of the candy array, and candy[364] is the 365th and last element.

Initialization
Arrays are often used to store data needed for a program. For example, a 12-element array can store the number of days in each month. In cases such as these, it's convenient to initialize the array at the beginning 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, the compiler counts the number of items in the list and makes the array that large.

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

Array Bounds

You have to make sure you use array indices that are within bounds; that is, you have to make sure they have values valid for 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 range 0 through 19, because the compiler won't check for you.

Specifying an Array Size

int arr[SIZE]; // symbolic integer constant double lots[144]; // literal integer constant

Example 1:

Example 1:

Example 2

Example 2

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 storage for this array, both the number of rows and the number of columns must be included in the arrays declaration. Calling the array val, the following is the correct specification for this two-dimensional array: int val[3][4]; Similarly, the declarations double volts[10][5]; char code[6][26];

Example:

Multidimensional Arrays

Example 1:

Example 1:

Example 2:

Example 2:

The sorting problem


Input: a list of n elements Output: the same list of n elements rearranges in increasing 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 studying sorting

Sorting Algorithm
Bubble Sort Selection Sort Insertion Sort

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

Bubble Sort

Start Unsorted Compare, swap (0, 1) Compare, swap (1, 2) Compare, no swap Compare, noswap

Compare, swap (4, 5)


99 in position

Bubble Sort

Pass 2

swap (0, 1)
no swap

no swap
swap (3, 4)

21 in position

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

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!

Selection Sort

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.

Insertion Sort

Anda mungkin juga menyukai