This document discusses different types of arrays and sorting/searching algorithms in C programming. It defines one-dimensional, two-dimensional, and multi-dimensional arrays. It also explains linear search, binary search, bubble sort, and selection sort algorithms - including their applications, merits, and demerits. Key array types include static arrays declared at compile-time with a fixed size, and dynamic arrays allocated at runtime using functions like malloc().
2. What is Array?
when we want to store more data type of the
same value we need to declare more variable
which leads to more memory utilization. C
supports array that is a derived data type.
An array is a fixed size sequential group of
elements of the same data types which are
referred by common name. In other words array
is a collective name given to a group of ‘similar
quantities’.
An array is a series of elements of the same
data type placed consecutively in memory
that can be individually referenced by adding
an index to a unique name.
3. Types of arrays
There are 3 types of array:
1) One-Dimensional array.
2) Two-Dimensional array.
3) Multi-Dimensional array.
4. 1- Dimensional array
A list of items can be given one variable name
using only one subscript and such a variable
is called a single subscripted variable or a
one dimensional array. The subscript begins
with 0.
5. How to declare 1-D array
Type <variablename> [size];
Eg:
int a[5];
float b[4];
char c[5];
6.
7. Initialization of 1D array
After an array is declared, its elements must be
initialized. Otherwise they will contain “garbage”.
An array can be initialized at 2 stages:
1) At compile time
2) At runtime
8. Compile time initialization
We can initialize the elements of array in the same
way as the ordinary variable during the time of
declaration.
Syntax:
type <array name> [size]={values};
Eg:
int a[4]={1,1,1,2};
float b[4]={1.2, 2.34, 31.2,-2};
The values in the list are separated by commas. The
size may be omitted. In such cases the compiler
allocates enough space for all initialized elements.
Eg: int a[]={1,1,1,1};
Here the variable a will contain four elements with
initial value 1.
9. Compile time initialization may be partial. That is
number of initialize may be less than the declared
size. In such case the remaining elements are
initialized to zero in case of int and float type and
null in case of character.
Eg:
int a[4]={1,1};
float b[2]={1.2,1.3};
char c[4]={‘h’,’I’};
10. Run time initialization
An array can be explicitly initialized at run time. This is
generally done for initializing large array.
for(i=0;i<=200;i++)
{
If(i<50) {
Sum[i]=5.0;
}
Else {
Sum[i]=1.0;
}
}
11. 2D Array
There could be situations where a table of values
will have to be stored. We can think of this table
as a matrix consisting of rows and columns. C
allows us to define such tables of items by using
2D array. 2D arrays are declared as follows:
Syntax :
Type <array name> [rows size] [column size];
Eg : int table [3][3];
Here the name of the array is table having 3 rows
and 3 columns.
Initializing 2D array
Int table[2][2]={0,0,1,1};
12. Multi Dimensional Array
C allows arrays of three or more dimensions. The
exact limit is determined by the compiler.
Syntax:
type <array name> [size1][size2][size3]….[sizen];
Eg: int a[3][3][2][5][6];
ANSI C does not specify any limit for array
dimension. However, most compilers permit 7 to
10 dimensions.
13. Dynamic Arrays
An array created at compile time by specifying size in
the source code has a fixed size and cannot be
modified at run time.
The process of allocating memory at compile time is
known a static memory allocation and the arrays that
receive static memory allocation are called static
arrays.
But in C it is possible to allocate memory to arrays at
run time. This feature is known as dynamic memory
allocation and the arrays created at run time are
called dynamic arrays.
Dynamic arrays are created using pointer variables
and memory management functions called malloc,
calloc and realloc. These functions are included in the
14. Searching
Searching is a process of finding the location of
the specified element in a list. The specified
element is often called the search key. If the
process of searching finds a match of the search
key with a list element value, the search is said to
be successful otherwise unsuccessful. The two
search techniques are:
1.) Sequential Search
2.) Binary Search
15. LINEAR SEARCH
This is simplest technique to find out an element
in an unsorted list. Suppose, we have an
unsorted list which contain n elements and want
to search the value of a particular elements for
that we have to check each elements one by one
and find at any passion.
One of the most straightforward and elementary
searches is the sequential search, also known as
a linear search.
16. Applications of Linear Search->
To find out particular string linear search is
efficient
Linear search is usually very simple to implement
and is practical when the list has only few
elements or when performing a single search in
an unordered list
Pseduocode game is the eg of linear search
17. Merits of Linear Search->
The primary advantage of linear search is its
simplicity.
It is also very resource efficient and memory
efficient. It also operates equally well on both
unsorted and sorted data.
18. Demerits of Linear Search->
It needs more space and time complexity
In linear search if the key element is the last
element and the search is from first element that
is a worst case or if the key element is the first
element and the search is from last element then
also is the worst case.
19. BINARY SEARCH
Binary search works for sorted list and it is very
efficient searching technique.
It is used to find the location of given element or
record in the list. Other information associated
with the element can also be fetched if required.
To find the location of a file in the computer
directory one can use this searching technique.
If we are on the internet then it is not easy task
using linear search to find out the information
about some records or files.
For this one prefers binary searching technique.
22. Demerits of binary search
The disadvantage of binary search in any
language is that you must provide an ordered list.
23. Sorting
Sorting means arranging a set of data in some
order. There are different methods that are used
to sort the data in ascending order or descending
order.
24. BUBBLE SORT
It is very simple sorting technique thus this
technique is not efficient to other techniques.
Suppose we sort the element in descending
order. The bubble sort loops through the elements
in the list comparing the adjacent element and
moves the largest element to the top of the list.
25. Application of Bubble Sort
In a small group of data we can use bubble sort
like classroom
Sorting data of friends group
Sorting data of our books collection
26. Merits of Bubble Sort
Easy to implement
Efficient for small data set
A bubble sort is a sort where adjacent items in the
array or list are scanned repeatedly swapping as
necessary until one full scan performs no swaps.
27. Demerits of Bubble Sort
A bubble sort, sort N elements where N is the
size of array or list because of out of position item
is only moved one position per scan and sorting
continues until all the elements are not swapped.
It is less efficient in large data set.
28. SELECTON SORT
The selection sort starts from element and
searches the entire list until it find the minimum
value. The sort palace the minimum value in the
first place. select the second element and
searches for the smallest element. The process
continuous until the complete the list.
The Selection Sort is a very basic sort. It works by
finding the smallest element in the array and
putting it at the beginning of the list and then
repeating that process on the unsorted remainder
of the data. Rather than making successive
swaps with adjacent elements like bubble sort,
selection sort makes only one, swapping the
smallest number with the number occupying its
correct position.
29. Application of Selection Sort
In a small group of data we can use selection sort
Sorting of friends group
Sorting of data of our books collection
30. Merits of Selection Sort
Easy to implement
Efficient for small data set
Demerits of Selection Sort
The worst case occurs if the array is already
sorted in descending order.
Selection sort spends most of its time trying to
find the minimum element in the unsorted part of
the array
It clearly shows the similarity between selection
sort and bubble sort.