0% found this document useful (0 votes)
5 views20 pages

C Array Declaration and Algorithms

The document provides an overview of arrays in C, including their declaration, initialization, and the syntax used for creating them. It also covers algorithms for searching (linear and binary search) and sorting (selection sort and bubble sort) arrays. Key concepts include the fixed size of arrays and the use of primitive data types.

Uploaded by

sunitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views20 pages

C Array Declaration and Algorithms

The document provides an overview of arrays in C, including their declaration, initialization, and the syntax used for creating them. It also covers algorithms for searching (linear and binary search) and sorting (selection sort and bubble sort) arrays. Key concepts include the fixed size of arrays and the use of primitive data types.

Uploaded by

sunitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Module - 2

Array Declaration
An array is a variable that can store multiple
values under a single name
An array in C is a fixed-size collection of similar
data items stored in contiguous memory
location
used to store the collection of primitive data
types such as int, char, float, etc
size and type of an array cannot be changed once
it is declared.
Arrays declaration and initialization
• Syntax
• dataType arrayName[arraySize];
– Int a[5];
– Float b[10];
– Char str[20];
– Double d[10];
– Int a[] = {1,2,3,4,5};
– Int a[5] = {1,2,3,4};
ND array initialization
Example
Accessing and updating array elements
Reading and Displaying matrix
Linear Search Algorithm
Algorithm Linear_Search( a,size,key)
}}
{ a: array of elements; size : size of
the array if(found==0)

key : element to search in the array print(“element not found”)

found=0; return -1
for(i=0;i<size;i++) • }
{
if(a[i]==key)
{
found=1;
print(“element Found”)
return i;
Binary Search
Algorithm Binary_search(a,l,h,key) else if ( key > a[m])
{
a: array of elements
l=m+1;
l: lower limit; h: upper limit else
key: key element to search
h=m-1
found=0;
while(l<=h) }
{
return -1;
m=(l+h)/2
if(key==a[m]) }
return m;
Selection sort algorithm
Algorithm Selection_sort(a,size) a[i] ↔a[m]
{ a: array elements
}
size : size of the array
for(i=0;i<n-1;i++)
• }
{
m=i
for(j=i+1;j<n;j++)
{
If(a[m]<a[j])
m=j
• }
Bubble sort Algorithm
Algorithm Bubble_Sort(a,size)

{ a: array elements

size : size of the array

for(i=0;i<n-1;i++)

for(j=0;j<n-i-1;j++)

{ if(a[j] > a[j+1])

a[j]↔a[j+1]

You might also like