Más contenido relacionado
La actualidad más candente (20)
Más de Shakila Mahjabin (10)
Arrays
- 2. 2
Data Structures and Algorithms
Introductio
nLinear data structure. Is used to store similar types of data. An array is a
finite collection of similar elements stored in adjacent memory locations.
Decleration of the Arrays: (In C)
the array name, the element type and the array size.
Examples:
int a[20], b[3],c[7]; // one-dimensional arrays
float f[5], c[2];
char m[4], n[20];
Initialization of an array is the process of assigning initial values.
Examples:
float, b[3]={2.0, 5.5, 3.14};
char name[4]= {‘E’,’m’,’r’,’e’};
int c[10]={0};
© SMT, Faculty, CSE, IUBAT
- 6. 6
Data Structures and Algorithms
Remarks
Pointer arrays and pointers:
Array of pointers is called pointer array. Details on pointers and arrays is discussed
in Array, func, point
document
Jagged arrays:
Arrays whose rows or
columns begins with
different numbers
of data elements and
ends with unused space,
are called
jagged array.
Dynamic arrays:
int *my_array;
my_array = new int[10]; // array size is defined during run-time
Group
1
Group
2
Group
3
Group
4
Evans Conrad Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
King Jones
Penn Reed
Silver
Troy
Wagner
* * * * 0 0 0 0 0
* * * * * * * * *
* * 0 0 0 0 0 0 0
* * * * * * 0 0 0
©SMT, Faculty, CSE, IUBAT
- 7. 7
Data Structures and Algorithms
#include<iostream.h>
int main ()
{
int i,n; int * p;
cout << "How many numbers would you like to type? ";
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;
}
Sample code for Dynamic
Array declaration
©SMT, Faculty, CSE, IUBAT
- 8. 8
Data Structures and Algorithms
Records and structure (struct in C)
Records may contain different types of information. Structures are used to store
these records. A structure is a collection of logically related variables under a
single unit/name. (In c++ we can create class to store records)
Example:
struct Rectangle // this is type/name for structure
{ float Length;
float width;
float area;
};
A structure is
usually
declared before
main( )
function.
©SMT, Faculty, CSE, IUBAT
- 9. 9
Data Structures and Algorithms
Travers, Insert, Delete on Arrays
a) Traversing in Linear Array
b) inserting in Linear Array
©SMT, Faculty, CSE, IUBAT
- 10. 10
Data Structures and Algorithms
Travers, Insert, Delete on Arrays
c) Deleting from Linear Array
©SMT, Faculty, CSE, IUBAT
- 11. 11
Data Structures and Algorithms
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.
©SMT, Faculty, CSE, IUBAT
- 12. 12
Data Structures and Algorithms
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.
©SMT, Faculty, CSE, IUBAT
- 14. 14
Data Structures and Algorithms
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.
This technique is to make several passes through the array. On
each pass,
successive pairs of elements are compared. If a pair is in
increasing order (or the values are identical), we leave the values as
they are. If a pair is in decreasing order, their values are swapped in
the array.
©SMT, Faculty, CSE, IUBAT