0% found this document useful (0 votes)
10 views

Arrays_ Introduction (1)

Uploaded by

aryasagar0412
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Arrays_ Introduction (1)

Uploaded by

aryasagar0412
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Arrays: Introduction

Arrays: Introduction

• Arrays: Introduction
• Arrays: Declaration and Initialization
• Types of Arrays
• Operations on Arrays
• Searching Techniques in Arrays
• Linear Search
• Binary Search
• Bubble Sort
• Insertion Sort
• Merge Sort
Arrays: Introduction

• Quick Sort
• Selection Sort
• Bubble Sort Algorithm
• Insertion Sort Algorithm
• Merge Sort Algorithm
• Quick Sort Algorithm
• Selection Sort Algorithm
• Advantages of Arrays
• Disadvantages of Arrays
• Applications of Arrays
Arrays: Introduction

• Definition of Arrays: Arrays are data structures


consisting of a collection of elements, each
identified by an index or key, allowing for efficient
storage and access of multiple items.
• Importance in Programming: Arrays provide a
systematic way of organizing data, enabling
developers to implement algorithms efficiently
and manage large datasets.
• Basic Characteristics: Arrays have fixed size,
homogeneous elements, and allow random
access through indices, making them
fundamental in various programming tasks.
Arrays: Declaration and Initialization

• Declaring Arrays: Arrays are declared by


specifying the data type followed by square
brackets and the array name, allowing memory
allocation for storing elements of that type.
• Initializing Arrays: Arrays can be initialized at the
time of declaration by providing a list of values
within curly braces, setting the initial contents of
the array.
• Example in Different Languages: In C++, an array
can be declared as `int arr[5];` and initialized as
`int arr[5] = {1, 2, 3, 4, 5};`. In Python, arrays are
created using lists, e.g., `arr = [1, 2, 3, 4, 5]`.
Types of Arrays

• One-Dimensional Arrays: One-dimensional


arrays are linear arrays where elements are
arranged in a single row, allowing straightforward
access via a single index.
• Multi-Dimensional Arrays: Multi-dimensional
arrays, such as two-dimensional arrays
(matrices), have elements arranged in multiple
rows and columns, enabling complex data
storage and manipulation.
• Dynamic Arrays: Dynamic arrays can change size
during program execution, providing flexibility in
memory usage, commonly implemented using
array-like structures such as vectors in C++ or lists
in Python.
Operations on Arrays

• Traversal: Traversal involves accessing each


element of the array sequentially, typically using
loops, to perform operations like printing or
modifying values.
• Insertion and Deletion: Insertion adds elements
to a specific position in the array, while deletion
removes elements. Both operations may require
shifting elements to maintain array structure.
• Updating Elements: Updating involves changing
the value of an existing element at a given index,
allowing modifications without altering the
array's size.
Searching Techniques in Arrays

• Linear Search: Linear search involves checking


each element sequentially until the desired
element is found or the end of the array is
reached, making it simple but potentially slow for
large arrays.
• Binary Search: Binary search requires a sorted
array and repeatedly divides the search interval
in half, significantly reducing the number of
comparisons and improving search efficiency.
• Comparison of Techniques: Linear search works
on unsorted arrays and is easier to implement,
while binary search is faster but requires pre-
sorted arrays, making it more suitable for large
datasets with frequent searches.
Linear Search

• Definition: Linear search is a straightforward


algorithm that sequentially checks each element
of an array until the desired element is found or
the array ends.
• Implementation: Linear search can be easily
implemented using a loop. It is particularly useful
for small or unsorted arrays due to its simplicity.
• Time Complexity: The time complexity of linear
search is O(n), where n is the number of
elements in the array, making it less efficient for
large datasets.
Binary Search

• Definition: Binary search is an efficient algorithm


for finding an element in a sorted array by
repeatedly dividing the search interval in half.
• Implementation: Binary search can be
implemented using iterative or recursive
methods, significantly reducing the number of
comparisons needed.
• Time Complexity: The time complexity of binary
search is O(log n), where n is the number of
elements, making it much faster than linear
search for large, sorted arrays.
Bubble Sort

• Definition: 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.
• Implementation: The process is repeated until
the list is sorted. Bubble sort is easy to
understand and implement but is inefficient for
large datasets.
• Time Complexity: The time complexity of bubble
sort is O(n²) in the average and worst cases,
making it suitable only for small datasets or
educational purposes.
Insertion Sort

• Definition: Insertion sort is a simple sorting


algorithm that builds the final sorted array one
item at a time by comparing each new element
to the existing sorted elements and inserting it
into its correct position.
• Implementation: The algorithm works similarly
to how cards are sorted in a hand. It is efficient
for small data sets and mostly sorted arrays.
• Time Complexity: The time complexity of
insertion sort is O(n²) in the average and worst
cases, but it performs well for nearly sorted data
with a best-case time complexity of O(n).
Merge Sort

• Definition: Merge sort is a divide-and-conquer


algorithm that divides the array into halves, sorts
each half, and then merges the sorted halves to
produce a sorted array.
• Implementation: The algorithm recursively splits
the array until each subarray has one element,
then merges the subarrays in a sorted manner. It
is stable and efficient for large datasets.
• Time Complexity: The time complexity of merge
sort is O(n log n) for all cases, making it more
efficient than quadratic algorithms like bubble
and insertion sort for large datasets.
Quick Sort

• Definition: Quick sort is a divide-and-conquer


algorithm that selects a pivot element, partitions
the array into elements less than and greater
than the pivot, and recursively sorts the
subarrays.
• Implementation: The algorithm works by
selecting a pivot and rearranging elements so
that all elements less than the pivot come before
it and all elements greater come after it. This
process is repeated for each subarray.
• Time Complexity: The time complexity of quick
sort is O(n log n) on average, but it can degrade
to O(n²) in the worst case if the pivot selection is
poor. However, with good pivot selection, it is
very efficient.
Selection Sort

• Definition: Selection sort is a simple sorting


algorithm that divides the array into a sorted and
an unsorted region. It repeatedly selects the
smallest (or largest) element from the unsorted
region and swaps it with the first unsorted
element.
• Implementation: The algorithm works by
iteratively selecting the minimum element from
the unsorted part of the array and swapping it
with the element at the current position. This
process continues until the entire array is sorted.
• Time Complexity: The time complexity of
selection sort is O(n²) in the average and worst
cases, making it inefficient for large datasets but
useful for its simplicity and ease of
implementation.
Bubble Sort Algorithm

• Algorithm Logic: The bubble sort algorithm


repeatedly steps through the array, compares
adjacent elements, and swaps them if they are in
the wrong order. This process is repeated until no
swaps are needed, indicating that the array is
sorted.
• Pseudocode: ```
• for i = 0 to n-1
• for j = 0 to n-i-1
• if arr[j] > arr[j+1]
• swap(arr[j], arr[j+1])
• ```
• Implementation in Python: ```python
• def bubble_sort(arr):
• n = len(arr)
Insertion Sort Algorithm

• Algorithm Logic: The insertion sort algorithm


builds the final sorted array one item at a time by
comparing each new element to the existing
sorted elements and inserting it into its correct
position.
• Pseudocode: ```
• for i = 1 to n-1
• key = arr[i]
• j = i-1
• while j >= 0 and arr[j] > key
• arr[j+1] = arr[j]
• j = j-1
• arr[j+1] = key
• ```
Merge Sort Algorithm

• Algorithm Logic: The merge sort algorithm


divides the array into halves, sorts each half, and
then merges the sorted halves to produce a
sorted array. It uses the divide-and-conquer
approach.
• Pseudocode: ```
• if left < right
• mid = (left + right) / 2
• mergeSort(arr, left, mid)
• mergeSort(arr, mid+1, right)
• merge(arr, left, mid, right)
• ```
• Implementation in Python: ```python
• def merge_sort(arr):
Quick Sort Algorithm

• Algorithm Logic: Quick sort selects a pivot


element, partitions the array into elements less
than and greater than the pivot, and recursively
sorts the subarrays.
• Pseudocode: ```
• if left < right
• pivot = partition(arr, left, right)
• quickSort(arr, left, pivot - 1)
• quickSort(arr, pivot + 1, right)
• ```
• Implementation in Python: ```python
• def partition(arr, low, high):
• pivot = arr[high]
• i = low - 1
Selection Sort Algorithm

• Algorithm Logic: Selection sort repeatedly selects


the smallest (or largest) element from the
unsorted region and swaps it with the first
unsorted element.
• Pseudocode: ```
• for i = 0 to n-1
• min_idx = i
• for j = i+1 to n
• if arr[j] < arr[min_idx]
• min_idx = j
• swap(arr[min_idx], arr[i])
• ```
• Implementation in Python: ```python
• def selection_sort(arr):
Advantages of Arrays

• Efficient Data Access: Arrays allow constant-time


access to elements using indices, making retrieval
and modification operations very efficient.
• Memory Management: Arrays use contiguous
memory allocation, which helps in optimizing the
performance of programs by taking advantage of
the locality of reference.
• Simple Data Structure: Arrays provide a
straightforward way to manage and manipulate
collections of data, making them easy to
implement and use in various algorithms and
applications.
Disadvantages of Arrays

• Fixed Size: Arrays have a fixed size, meaning their


length must be defined at the time of creation.
This can lead to inefficient memory usage if the
array size is not accurately estimated.
• Inefficient Insertions and Deletions: Inserting or
deleting elements in arrays requires shifting
elements, which can be time-consuming,
especially for large arrays.
• Homogeneous Data Types: Arrays can only store
elements of the same data type, limiting their
flexibility when dealing with different types of
data in a single structure.
Applications of Arrays

• Storing Data Collections: Arrays are used to store


collections of data in a single variable, enabling
efficient data management and access. Common
examples include storing lists of numbers, strings,
or objects.
• Implementing Data Structures: Arrays serve as
the foundation for implementing other data
structures like stacks, queues, and hash tables,
providing the necessary underlying structure for
efficient operations.
• Algorithm Implementation: Many algorithms,
such as searching and sorting, are implemented
using arrays due to their ability to provide quick
access to elements. Examples include binary
search and various sorting algorithms like quick
sort and merge sort.

You might also like