Arrays, Records and Pointers
Arrays, Records and Pointers
Pointers
Csc-391
Data Structures and Algorithms
2
Introductio
n data structure. Is used to store similar types of data. An array is a
Linear
finite collection of similar elements stored in adjacent memory locations.
Remarks
Pointer arrays and pointers:
Array of pointers is called pointer array. Details on pointers and arrays is discussed
in Array, func, point
document
Group Group Group Group
Jagged arrays: 1 2 3 4
Arrays whose rows or Evans Conrad Davis Baker * * * * 0 0 0 0 0
columns begins with Harris Felt Segal Cooper * * * * * * * * *
different numbers * * 0 0 0 0 0 0 0
Lewis Glass Ford
of data elements and
Shaw Hill Gray * * * * * * 0 0 0
ends with unused space,
King Jones
are called
jagged array. Penn Reed
Silver
Troy
Dynamic arrays: Wagner
int *my_array;
my_array = new int[10]; // array size is defined during run-time
©SMT, Faculty, CSE, IUBAT
Data Structures and Algorithms
7
#include<iostream.h>
int main ()
{
int i,n; int * p;
Sample code for Dynamic
cout << "How many numbers would you like to type? "; Array declaration
cin >> i;
p= new int[i]; // it takes memory at run-time from Heap
if(p == NULL)
cout << "Error: memory could not be allocated";
else
{
for(n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
int*k=p; // to hold the base address of dynamic array
cout << "You have entered: \n";
for(n=0; n<i; n++) cout << *k<< ", "; k++;
cout<<"\n";
delete[] p; // it release the memory to send it back to Heap
}
return 0;
}
©SMT, Faculty, CSE, IUBAT
Data Structures and Algorithms
8
A structure is
usually
declared before
main( )
function.
Searching
Linear Search
The linear search compares each element of the array with the search key
until the search key is found. To determine that a value is not in the
array, the program must compare the search key to every element in the array. It
is also called “Sequential Search” because it traverses the data sequentially
to locate the element.
Searching
Binary Search
It is useful for the large sorted arrays. The binary search algorithm can
only be used with sorted array and eliminates one half of the elements in
the array being searched after each comparison.
Searching
Binary Search
Sorting
Bubble Sort
The technique we use is called “Bubble Sort” because the bigger value
gradually
bubbles their way up to the top of array like air bubble rising
in water, while the small values sink to the bottom of array.
Sorting
Bubble Sort
Sorting
Bubble Sort