0% found this document useful (0 votes)
8 views24 pages

ARRAYS

arrays ,operations,sorting and searching

Uploaded by

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

ARRAYS

arrays ,operations,sorting and searching

Uploaded by

Minisha N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Arrays

1 INTRODUCTION

2 DECLARATION &TYPES OF ARRAY

Index
3 OPERATIONS OF ARRAY

4 SEARCHING TECHNIQUES

5 SORTING TECHNIQUES

6 DISADVANTAGES
Introduction
An array is a data structure that stores a collection of elements, typically
of the same data type, in a contiguous block of memory. Arrays are used
to store multiple values in a single variable, which can be accessed using
an index.
Representation

Int is a type of data value.


Data items stored in an array are known as elements.
The location or placing of each element has an index value.
Declaration Syntax:
datatype Array_Name[size] = { value1, value2, value3, ….. valueN };

DECLARING ARRAY BY DEFINING THE SIZE:


The array’s size mentions the maximum number of elements it can store.
int newarray1[15];
DECLARING ARRAY BY INITIALISING ELEMENTS:
The compiler allocates an array of size equivalent to the number of elements.
The array initialisation happens during the declaration time.
int newarray1[] = {5, 10, 20, 30, 40}
DECLARING ARRAY BY DEFINING THE SIZE:
If the number of initialised elements is less than the array’s size, the compiler
automatically initialises the remaining elements to 0. Here’s an example:
int newarray1[5] = {5, 10, 20, 30, 40};
Types of Arrays: There are two types of arrays:
One-Dimensional Arrays
Multi-Dimensional Arrays

ONE -DIMENSIONAL ARRAYS

It is a kind of linear array. It involves single sub-scripting. The [] (brackets) is


used for the subscript of the array and to declare and access the elements
from the array.
Syntax: DataType ArrayName [size];
int a[10];
MULTI-DIMENSIONAL ARRAYS
1.TWO-DIMENSIONAL ARRAYS
An array involving two subscripts [] [] is known as a two-dimensional array.
They are also known as the array of the array.
Syntax: DataType ArrayName[row_size][column_size];
For Example: int arr[5][5];

2. THREE-DIMENSIONAL ARRAYS

When we require to create two or more tables of the elements to declare the
array elements, then in such a situation we use three-dimensional arrays.
Syntax: DataType ArrayName[size1][size2][size3];
For Example: int a[5][5][5];
Operations on Array
Traversal: Accessing each element of an array sequentially, either from the
beginning to the end or vice versa.

Insertion: Adding a new element to an array, which may require shifting


existing elements. The new element can be inserted at the beginning, end, or
any specified location.

Deletion: Removing an element from an array, which may require shifting the
remaining elements to fill the gap. The element to be deleted can be at any
location.
Search: Finding a specific element in an array by comparing it with each
element until a match is found. Common search algorithms include linear
search and binary search.

Sorting: Arranging array elements in a specific order. Common sorting


algorithms include bubble sort, insertion sort, selection sort, merge sort, and
quicksort. Sorting facilitates efficient searching and data processing.

Access: Retrieving the value of an element by specifying its index. Array


access is fast and efficient as it does not require traversal.
Searching techniques
LINEAR SEARCH
int linearSearch(int a[], int n, int val)
{
// Going through array sequencially
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
TIME COMPLEXITY
BINARY SEARCH
int binarySearch(int a[], int beg, int end, int val)
{ int mid;
if(end >= beg)
{ mid = (beg + end)/2;
if(a[mid] == val)
{ return mid; }
else if(a[mid] < val) //recursive calls
{ return binarySearch(a, mid+1, end, val);
}
else
{
return binarySearch(a, beg, mid-1, val);
} }
return -1;
}
TIME COMPLEXITY
Sorting techniques
BUBBLE SORT
void bubble(int a[], int n)
{ int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{ temp = a[i];
Best Case O(n)
a[i] = a[j];
a[j] = temp;
Average Case O(n2)
} }
} }
Worst Case O(n2)
INSERTION SORT
void insert(int a[], int n)
{ int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1; }
a[j+1] = temp; }
}
Best Case O(n)

Average Case O(n2)

Worst Case O(n2)


MERGE SORT void merge(int a[], int beg, int mid, int end)
while (i < n1 && j < n2) {
void merge(int a[], int beg, int mid, int end) {
int i, j, k; if(LeftArray[i] <= RightArray[j])
int n1 = mid - beg + 1; { a[k] = LeftArray[i];
int n2 = end - mid; i++; }
int LeftArray[n1], RightArray[n2]; //temporary else
arrays { a[k] = RightArray[j];
/* copy data to temp arrays */ j++; }
for (int i = 0; i < n1; i++) k++; }
LeftArray[i] = a[beg + i]; while (i<n1)
for (int j = 0; j < n2; j++) { a[k] = LeftArray[i];
RightArray[j] = a[mid + 1 + j]; i++; k++; }
while (j<n2) {
i = 0; /* initial index of first sub-array */ a[k] = RightArray[j];
j = 0; /* initial index of second sub-array j++;
k = beg; /* initial index of merged sub-array */ k++; } }
void mergeSort(int a[], int beg, int end)
{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}

Best Case O(n*logn)

Average Case O(n*logn)

Worst Case O(n*logn)


QUICK SORT
SELECTION SORT
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void selectionSort(int arr[], int n) {
for (int j = 0; j< n - 1; j++) {
int min = j;
for (int i = j + 1; i < n; i++) {
Best Case O(n2)
if (arr[i] < arr[min])
min = i;}
Average Case O(n2)
swap(&arr[min], &arr[j]);
} Worst Case O(n2)
}
Advantages
Fast Access: Directly access any element using its index in constant time
(O(1)).
Quick Retrieval: Contiguous memory storage allows efficient data
access.
Memory Efficient: Known size at compile time, reducing memory
fragmentation.
Versatile: Can store various data types, including integers, floats, and
objects.
Easy to Use: Simple to implement and understand, ideal for beginners.
Hardware Friendly: Compatible with most hardware architectures.
Disadvantages
Fixed Size: Arrays can't be resized without creating a new array and
copying data.
Memory Allocation: Large arrays can exhaust system memory.
Slow Insertion/Deletion: Adding or removing elements requires shifting
others.
Wasted Space: Unused elements waste memory.
Homogeneous Elements: Arrays support only one data type.
Inflexibility: Fixed size and single data type limit flexibility compared to
linked lists and trees.
Thank You

You might also like