Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

INTRODUCTION TO ARRAYS

SACHIN CHINTHAPALLY
21x31a7213
Arrays
Arrays are fundamental data structures used in computer programming. They provide a way
to store collections of elements of the same data type in contiguous memory locations.
Declaring and Initializing Arrays

Declaration
An array is declared by specifying its data type and size, which represents the number of elements it can hold.
For example, "int numbers[5]" declares an array named 'numbers' that can store 5 integer values.

Initialization
Arrays can be initialized during declaration by assigning values to elements within curly braces. For instance, "int
numbers[5] = {1, 2, 3, 4, 5}" initializes the 'numbers' array with these values.

Dynamic Allocation
In some programming languages, arrays can be dynamically allocated using functions like 'malloc' or 'new' to
define their size at runtime, which allows for more flexibility in handling data.
Types of Arrays
1 One-Dimensional 2 Two-Dimensional Arrays 3 Multidimensional Arrays
Arrays
One-dimensional arrays store a Two-dimensional arrays organize data Multidimensional arrays extend the
sequence of elements, typically of in rows and columns, effectively concept of rows and columns to multiple
the same data type, in a single row. creating a table-like structure. They dimensions, allowing you to store data in
They are similar to a list of items are useful for representing matrices a more complex structure. They are often
with a fixed order. and storing tabular data. used for applications like image
processing and game development.
Basic Operations on Arrays
Traversal Visiting each element of an array in a sequential manner,
typically used for processing or displaying array elements.

Insertion Adding a new element to an array at a specified position


involves shifting existing elements to accommodate the new
element.

Deletion Removing an element from an array requires shifting elements


to fill the gap left by the deleted element.
Linear Search Algorithm
In Linear Search, we iterate over all the elements of the
array and check if it the current element is equal to the
target element. If we find any element to be equal to the
target element, then return the index of the current
element. Otherwise, if no element is equal to the target
element, then return -1 as the element is not found.
Binary Search Algorithm
Binary search is a search algorithm used to find the
position of a target value within a sorted array. It
works by repeatedly dividing the search interval in
half until the target value is found or the interval is
empty. The search interval is halved by comparing the
target element with the middle value of the search
space.
Time complexity of linear and binary search
Bubble Sort

1 Understanding Bubble Sort


Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order.

2 Iterative Process
This process continues until the list is sorted. The algorithm gets its name from the way smaller elements "bubble
up" to their correct positions.

3 Time Complexity
Bubble sort has a time complexity of O(n^2), making it inefficient for large datasets, but it's easy to understand and
implement.
Bubble Sort
Insertion Sort
Sorted Subarray
1 Elements are already in order

Next Element
2
The next element to be inserted

Unsorted Subarray
3
The remaining elements

Insertion sort is a simple sorting algorithm that builds the final sorted array one element at a time.

It iterates through the unsorted portion of the array, comparing each element to the elements in the sorted subarray and inserting it in
the correct position.
Insertion Sort
Selection Sort
Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the
unsorted portion of the list and moving it to the sorted portion of the list.

The algorithm repeatedly selects the smallest (or largest) element from the unsorted portion of the list and swaps it with the first element
of the unsorted part. This process is repeated for the remaining unsorted portion until the entire list is sorted.

1 2
3

5
Merge Sort
1 Divide
Recursively divide the unsorted array into two halves until you reach subarrays of size
1. These are considered sorted by default.

2 Conquer
Merge the sorted subarrays iteratively, comparing elements from each half and placing
the smaller one in a new sorted array.

3 Combine
Continue merging sorted subarrays until a single sorted array is obtained,
encompassing all the original elements.
Quick Sort
The key process in quickSort is a partition(). The target of partitions is to place the pivot (any element can be chosen to be a pivot)
at its correct position in the sorted array and put all smaller elements to the left of the pivot, and all greater elements to the right
of the pivot.

Partition is done recursively on each side of the pivot after the pivot is placed in its correct position and this finally sorts the array.
Time complexities of different Sorting techniques
Advantages and Disadvantages of Arrays

Advantages Disadvantages
Arrays are efficient and effective in storing Arrays have a fixed size, making them
and retrieving data. Their contiguous inflexible. If the array is full, you'll need to
memory allocation allows for fast access create a new one, potentially copying data,
and traversal. Arrays are also versatile and which can be inefficient. Additionally,
can be used to implement various data inserting or deleting elements at arbitrary
structures. positions can be expensive.

Alternatives
Dynamic data structures like linked lists and hash tables offer more flexibility and dynamic size,
but come with their own trade-offs.

You might also like