Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Intro To Data Structures and Algorithm

The document discusses different types of arrays such as one-dimensional arrays and how they can be declared and initialized. It also covers searching arrays using linear search, describing the algorithm which involves setting a flag, indexing through the array, and comparing each element to the search value until a match is found. The advantages and disadvantages of linear search are presented.

Uploaded by

alexisfante
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Intro To Data Structures and Algorithm

The document discusses different types of arrays such as one-dimensional arrays and how they can be declared and initialized. It also covers searching arrays using linear search, describing the algorithm which involves setting a flag, indexing through the array, and comparing each element to the search value until a match is found. The advantages and disadvantages of linear search are presented.

Uploaded by

alexisfante
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Intro to Data Structures and

Algorithm
Alexis L. Fante
Instructor
Array
• ARRAY is a data structure consisting of components of the same
type. It is a series of memory locations that can store value of the
same type.[1]
• declared to contain data of a specific data type
• components (memory locations) can be accessed by directly
referring to the index of a specific location
• used to store and process a set of values of the same data type
forming a logical group

X = {1, 2, 3, 4,5} y = {‘a’, ’b’, ’c’}

[1] P. 1, Albacea, Intro. to Data Structures & Algorithms, 2002


One-Dimensional Array
• known both as single-dimensional and a single-subscript array
• all the elements are stored consecutively in a set of contiguous
memory locations
• the starting index value for arrays is always zero

Array Declaration and Initialization

int my_int_array[5]; double mydouble_array[15];


char mychar_array[10]; long int mylongint_array[10];
• It can be initialized statically or during the program run-time.
– the memory address allocated might have contained a garbage
value which is being previously used by the operating system or
other applications running previously or simultaneously with the
C program
– secure that the memory to be used will either be initialized to
contain a specific value assigned by the programmer

Static Initialization

int my_int_array[5] ={0, 0, 0, 0, 0};


char my_char_array[5] = {‘ ‘,’ ‘,’ ‘,’ ‘,’ ‘};
int my_int_array[5]= {1, 2, 3, 4, 5};
char my_char_array[5] = {‘a’,’b’,’c’,’d’,’e’};
• Character arrays or strings can be initialized in three ways:

– char codes[6] = {‘s’,’a’,’m’,’p’,’l’,’e’};


– char codes[ ] = {‘s’,’a’,’m’,’p’,’l’,’e’};
– char codes[ ] = ”sample” or char codes[ ] = “ “;

• For nos. 1 & 2 both are initialized with six array locations for an
array named codes.

• The no. 3 initialization creates or assigns the string “sample” to


array codes. This however assigns a seven character array where
the last cell contains the escape sequence \0, known as the NULL
character. The NULL character is automatically appended to all
strings by C compiler.
Examples :
char codes[6] = “sample”;
int intarr [7] = {5,14,23,7,19,6,90};
char codes[5] = {‘s’,’a’,’m’,’p’,’l’,’e’};

s a m p l e \0

codes [0 ] codes [1 ] codes [ 2] codes [ 3 ] codes [ 4 ] codes [5 ] codes [ 6 ]

5 14 23 7 19 6 90

intarr [0 ] intarr [1 ] intarr [ 2] intarr [ 3 ] intarr [ 4 ] intarr [5 ] intarr [ 6 ]

s a m p l e

codes [0 ] codes [1 ] codes [ 2] codes [ 3 ] codes [ 4 ] codes [5 ]


Array as Function Arguments

• Like any other data, array elements or the array itself can be used
as function arguments, either in a user defined function (UDF) or
system defined function.

int grades[5] = { 98, 75, 65, 80, 88};

Findlow(grades[1],grades[5] / 2);
Findmax(grades);
Searching

• Linear Search
- also known as Sequential Search
- every item in the list is being compared with the search token, the
value that is to be search, until the search token is found or unfound
- is similar with looking for a term or meaning of a word in a
dictionary, looking for a name in a list of students, or searching for a
topic in a book index
- Advantage:
1. The algorithm is simple
2. The list need not be sorted in a particular order
Algorithm for Linear Search

Set a Found flag to .F.


Set an index value to -1
Begin with the first item in the list
While there are still items in the list AND the found flag is .F.
Compare the item with the desired item
If the item is found
Set the Found flag to .T.
Set the index value to the item’s position in the list
Endif
EndWhile
Return the Index Value
Algorithm for Linear Search continued.

- Aside from the advantages enumerated, another advantage of linear


search is if the element is located toward the front part of the list. The
searching will be done in a shorter period of time. The setback
however is if the search token is located at the back of the list

- On an average and assuming that the search token is equally likely


to be anywhere within the list, the number of required of comparisons
are N/2, where N is the number of items in the list. For a 10-element
list, the average number of comparisons needed for a linear search is
5, and for 10,000-element list the average number of comparisons is
5000.[1]

[1] P 419, Ibid.

You might also like