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

Data Structures and Algorithms - Arrays

This document provides an overview of data structures, focusing on arrays as a fundamental type. It discusses the characteristics of linear, static, dynamic, and non-linear data structures, along with basic operations such as traversal, insertion, deletion, search, and update. Additionally, it covers sorting techniques like bubble sort, selection sort, insertion sort, merge sort, and quicksort.

Uploaded by

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

Data Structures and Algorithms - Arrays

This document provides an overview of data structures, focusing on arrays as a fundamental type. It discusses the characteristics of linear, static, dynamic, and non-linear data structures, along with basic operations such as traversal, insertion, deletion, search, and update. Additionally, it covers sorting techniques like bubble sort, selection sort, insertion sort, merge sort, and quicksort.

Uploaded by

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

Data Structures and Algorithms - Arrays

• Data structures are the fundamental building


blocks of computer programming. They
define how data is organized, stored, and
manipulated within a program.
Understanding data structures is very
important for developing efficient and
effective algorithms. In this tutorial, we will
explore the most commonly used data
structures, including arrays, linked lists,
stacks, queues, trees, and graphs.
• What is Data Structure?
• A data structure is a storage that is used to
store and organize data. It is a way of arranging
data on a computer so that it can be accessed
and updated efficiently.
• A data structure is not only used for organizing
the data. It is also used for processing,
retrieving, and storing data. There are different
basic and advanced types of data structures that
are used in almost every program or software
system that has been developed. So we must
have good knowledge about data structures.
• Linear Data Structure: Data structure in which data elements are
arranged sequentially or linearly, where each element is attached to
its previous and next adjacent elements, is called a linear data
structure.
Example: Array, Stack, Queue, Linked List, etc.
• Static Data Structure: Static data structure has a fixed memory size.
It is easier to access the elements in a static data structure.
Example: array.
• Dynamic Data Structure: In dynamic data structure, the size is not
fixed. It can be randomly updated during the runtime which may be
considered efficient concerning the memory (space) complexity of
the code.
Example: Queue, Stack, etc.
• Non-Linear Data Structure: Data structures where data elements
are not placed sequentially or linearly are called non-linear data
structures. In a non-linear data structure, we can’t traverse all the
elements in a single run only.
Examples: Trees and Graphs.
• Array is a collection of data items of the same data type
referred to by a common name. Individual data items
can be accessed by an integer called the ‘index’ or
‘subscript’ and these items occupy contiguous
• Array is a container which can hold a fix number of
items and these items should be of the same type. Most
of the data structures make use of arrays to implement
their algorithms. Following are the important terms to
understand the concept of Array.
• Element − Each item stored in an array is called an
element.
• Index − Each location of an element in an array has a
numerical index, which is used to identify the element.
• Array Representation
• Arrays can be declared in various ways in
different languages.
• For illustration, let's take C array declaration.
• Arrays can be declared in various ways in
different languages. For illustration, let's take
C array declaration.
Row Major and column Major
representation
As per the above illustration, following are the
important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10
elements.
• Each element can be accessed via its index.
For example, we can fetch an element at index
6 as 9.
Basic Operations

Following are the basic operations supported by an array.


• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or
by the value.
• Update − Updates an element at the given index.
In C, when an array is initialized with size, then it
assigns defaults values to its elements in
following order.
Data Type Default Value
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
Traverse Operation
This operation is to traverse through the elements of an array.
Example
#include <stdio.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, n = 5;
int i = 0;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Insertion Operation

• Insert operation is to insert one or more data


elements into an array. Based on the
requirement, a new element can be added at
the beginning, end, or any given index of
array.
• It is very inefficient to insert an element in an
array.
Deletion Operation

• Deletion refers to removing an existing


element from the array and re-organizing all
elements of an array.
• It is very inefficient to delete an element from
an array.
Search Operation

• You can perform a search for an array element


based on its value or its index.
#include <stdio.h>
void main()
{ int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("LA[%d] = %d \n", i, LA[i]);
}
while( j < n)
{
if( LA[j] == item )
{ break; }
j = j + 1;
}
printf("Found element %d at position %d\n", item, j+1);
}
Update Operation

• Update operation refers to updating an


existing element from the array at a given
index.
#include <stdio.h>
void main()
{
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("LA[%d] = %d \n", i, LA[i]);
}
LA[k-1] = item;
printf("The array elements after updation :\n");
for(i = 0; i<n; i++)
{ printf("LA[%d] = %d \n", i, LA[i]);
}
}
Binary Search
int main()
{ int A[] = {1,2,4,6,8,9,12,34,54,90};
int n=55;
printf("%d is found at Index %d \n",n,iterativeBsearch(A,10,n)); return 0; }

int iterativeBsearch(int A[], int size, int element)


{ int start = 0; int end = size-1;
while(start<=end)
{ int mid = (start+end)/2;
if( A[mid] == element)
{ return mid; }
else if( element < A[mid] )
{ end = mid-1; }
else
{ start = mid+1; }
}
return -1;
}
• Compare x with the middle element.
• If x matches with middle element, we return
the mid index.
• Else If x is greater than the mid element, then
x can only lie in right half subarray after the
mid element. So we recur for right half.
• Else (x is smaller) recur for the left half.
Sorting Techniques
• Bubble sort,
• Selection sort,
• Insertion sort
• Merge sort
• Quick sort
int main()
{ int n, i, j, temp; int arr[n];
scanf("%d", &n);
for(i = 0; i < n; i++)
{ scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) // to keep track of number of cycles
{ for(j = 0; j < n ; j++) // to compare the elements within the particular
cycle
{ // swap if one element is greater than its adjacent element
if(arr[j] > arr[j + 1])
{ temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
} } }
for(i = 0; i < n; i++)
{ printf("%d ", arr[i]);
}
}
Insertion Sort
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Selection Sort
void swap(int *xp, int *yp)
{ int temp = *xp; *xp = *yp; *yp = temp;
}
void selectionSort(int arr[], int n)
{ int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{ // Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
Merge Sort

• Merge Sort is a Divide and Conquer algorithm. It


divides input array in two halves, calls itself for
the two halves and then merges the two sorted
halves.
• The merge() function is used for merging two
halves.
• The merge(arr, l, m, r) is key process that
assumes that arr[l..m] and arr[m+1..r] are sorted
and merges the two sorted sub-arrays into one.
1. Find the middle point to divide the array into
two halves: middle m = (l+r)/2
2. Call mergeSort for first half: Call
mergeSort(arr, l, m)
3. Call mergeSort for second half: Call
mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l +r / 2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}
void merge(int arr[], int l, int m, int r) /* Merge the temp arrays back into
{ arr[l..r]*/
int i, j, k; i = 0; // Initial index of first subarray
j = 0; // Initial index of second
int n1 = m - l + 1;
subarray
int n2 = r - m;
k = l; // Initial index of merged
subarray
/* create temp arrays */ while (i < n1 && j < n2) {
int L[n1], R[n2]; if (L[i] <= R[j]) {
arr[k] = L[i];
/* Copy data to temp arrays L[] and i++;
R[] */ }
for (i = 0; i < n1; i++) else {
L[i] = arr[l + i]; arr[k] = R[j];
for (j = 0; j < n2; j++) j++;
}
R[j] = arr[m + 1 + j];
k++;
}
/* Copy the remaining elements of L[], if
there
/* l is for left index and r
are any */ is right index of the
while (i < n1) {
arr[k] = L[i]; sub-array of arr to be
i++; sorted */
k++;
}

/* Copy the remaining elements of R[],


if there
are any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
QuickSort
Like Merge Sort, QuickSort is a Divide and
Conquer algorithm.
It picks an element as pivot and partitions the
given array around the picked pivot.
There are many different versions of quickSort
that pick pivot in different ways.

Note: pick first or last element as pivot.


• The key process in quickSort is partition().
• Target of partitions is, given an array and an
element x of array as pivot, put x at its correct
position in sorted array and put all smaller
elements (smaller than x) before x, and put all
greater elements (greater than x) after x.
#define MAX 10
int main()
{ int A[MAX];
printf("Input Array: ");
accept();
quickSort(0,MAX-1);
printf("Output Array: ");
display();
}
void quickSort(int left, int right)
{
if(right - left <= 0)
{ return 0; }
else
{
int pivot = A[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left,partitionPoint-1);
quickSort(partitionPoint+1,right);
}}
int partition(int left, int right, int pivot)
{ int LP = left -1, RP= right;
while(true)
{
while(A[++LP] < pivot)
{ //do nothing }
while(RP > 0 && A[--RP] > pivot)
{ //do nothing }
if(LP >= RP)
{ break; }
Else
{swap(LP,RP);
}
}
swap(LP,right);
return LP;
}

You might also like