unit 1.2 Array
unit 1.2 Array
• Arrays :
The simplest type of data structure is a linear (or one
dimensional) array. A list of a finite number n of similar
data referenced respectively by a set of n consecutive
numbers, usually 1, 2, 3 . . . . . . . n.
if A is chosen the name for the array, then the elements of
A are denoted by subscript notation a1, a2, a3….. an
or by the parenthesis notation
A (1), A (2), A (3) . . . . . . A (n)
or by the bracket notation
A [1], A [2], A [3] . . . . . . A [n]
Continue..
• Example 1: A linear array STUDENT consisting of the
names of six students is pictured in below figure. Here
STUDENT [1] denotes John Brown, STUDENT [2] denotes
Sandra Gold, and so on.
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3}; //declaring and initializing array
size=sizeof(arr)/sizeof(arr[0]); //sizeof(arr) will give 20 and sizeof(arr[0]) will give 4
printf("The array elements are: ");
for(i=0;i<size;i++)
printf("\narr[%d]= %d", i, arr[i]);
}
Output:
The array elements are:
arr[0]= 1
arr[1]= -9
arr[2]= 17
arr[3]= 4
arr[4]= -3
Index
• Insertion in arrays
• Deletion in arrays
Insertion in arrays
void main()
{
int i, size, x, pos;
int arr[]={2, 4, 6, 8, 12};
size=sizeof(arr)/sizeof(arr[0]);
printf("The array elements before insertion operation:\n");
for(i=0;i<size;i++)
printf("arr[%d] = %d\n",i, arr[i]);
printf("Enter the element to be insert: ");
scanf("%d", &x);
printf("Enter the position where you want to insert the element: ");
scanf("%d", &pos);
size = size + 1;
printf("The array elements after insertion operation:\n");
for(i=size-1;i>=pos-1;i--)
arr[i+1]=arr[i];
arr[pos-1]=x;
for(i=0;i<size;i++)
printf("arr[%d] = %d\n",i, arr[i]);
}
Continue..
Output:
The array elements before insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 12
Enter the element to be insert: 10
Enter the position where you want to insert the element: 5
The array elements after insertion operation:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
arr[5] = 12
Deletion in arrays
• Deletion operation in Array or delete an element from
an Array simply means, Removing an existing element
from a specific position of an array.
• Let’s see how you can delete an element from a specific
position of an array step-by-step:
Continue..
Variables we are using here:
1. a : Name of the array.
2. size : Size of the array (i.e., total number of
elements in the array)
3. i : Loop counter or counter variable for
the for loop.
4. pos : The position from where we wish to delete
the element.
The following algorithm deletes a data element from
a specific position pos (position specified by
the programmer) of a linear array ‘a‘.
Continue..
Algorithm to Delete an element from an Array:
Step 01: Start
Step 02: [Initialize counter variable. ] Set i = pos - 1
Step 03: Repeat Step 04 and 05 for i = pos - 1 to i < size
Step 04: [Move ith element backward (left). ] set a[i] = a[i+1]
Step 05: [Increase counter. ] Set i = i + 1
Step 06: [End of step 03 loop. ]
Step 07: [Reset size of the array. ] set size = size - 1
Step 08: Stop
In the above algorithm, step 2 to step 5 shifts (moves) each such
element one location (position) backward (left) whose position is
greater than the position of the element which we wish to delete.
Continue..
Continue..
Implementation in C:
// writing a program in C to delete an element from an array
void main()
{
int i, size, pos;
int a[]={2, 4, 6, 8, 12};
size=sizeof(a)/sizeof(a[0]);
printf("The array elements before deletion operation:\n");
for(i=0;i<size;i++)
printf("a[%d] = %d\n", i, a[i]);
printf("Enter the position from where you wish to delete the element: ");
scanf("%d", &pos);
printf("The array elements after deletion operation:\n");
for(i=pos-1;i<size;i++)
a[i]=a[i+1];
size = size - 1;
for(i=0;i<size;i++)
printf("a[%d] = %d\n", i, a[i]);
}
Continue..
Output:
The array elements before deletion operation:
a[0] = 2
a[1] = 4
a[2] = 6
a[3] = 8
a[4] = 12
Enter the position from where you wish to delete the element: 3
The array elements after deletion operation:
a[0] = 2
a[1] = 4
a[2] = 8
a[3] = 12
Index
• Binary Search
• Complexity Analysis
Binary Search
• The worst-case condition for bubble sort occurs when elements of the array
are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort a
given array is (n-1). where ‘n’ is a number of elements present in the array.
At pass 1 : Number of comparisons = (n-1)
Number of swaps = (n-1)
At pass 2 : Number of comparisons = (n-2)
Number of swaps = (n-2)
At pass 3 : Number of comparisons = (n-3)
Number of swaps = (n-3)
.
.
.
At pass n-1 : Number of comparisons = 1
Number of swaps = 1
Continue..
• Now , calculating total number of comparison
required to sort the array
= (n-1) + (n-2) + (n-3) + . . . 2 + 1
= (n-1)*(n-1+1)/2 { by using sum of N natural
Number formula }
= n (n-1)/2
• For Worst case:
• Total number of swaps = Total number of
comparison
Total number of comparison (Worst case) = n(n-1)/2
Total number of swaps (Worst case) = n(n-1)/2
Continue..
• Worst and Average Case Time Complexity: O(N2).
The worst case occurs when an array is reverse
sorted.
• Best Case Time Complexity: O(N). The best case
occurs when an array is already sorted.
• Auxiliary Space: O(1)
Quick Sort
• Quicksort is the widely used sorting algorithm that
makes n log n comparisons in average case for
sorting an array of n elements.
• It is a faster and highly efficient sorting algorithm.
This algorithm follows the divide and conquer
approach.
• Divide and conquer is a technique of breaking
down the algorithms into subproblems, then
solving the subproblems, and combining the results
back together to solve the original problem.
Continue..
• Quick sort picks an element as pivot, and then it
partitions the given array around the picked pivot
element. In quick sort, a large array is divided into
two arrays in which one holds values that are
smaller than the specified value (Pivot), and
another array holds the values that are greater than
the pivot.
• After that, left and right sub-arrays are also
partitioned using the same approach. It will
continue until the single element remains in the
sub-array.
Continue..
Choosing the pivot
• Picking a good pivot is necessary for the fast
implementation of quicksort. However, it is typical
to determine a good pivot. Some of the ways of
choosing a pivot are as follows –
• Pivot can be random, i.e. select the random pivot from
the given array.
• Pivot can either be the rightmost element of the
leftmost element of the given array.
• Select median as the pivot element.
Continue..
Algorithm:
//start –> Starting index, end --> Ending index
Quicksort(array, start, end)
{
if (start < end)
{
pIndex = Partition(A, start, end)
Quicksort(A,start,pIndex-1)
Quicksort(A,pIndex+1, end)
}
}
Continue..
Partition Algorithm:
partition (array, start, end)
{
// Setting rightmost Index as pivot
pivot = arr[end];
Case Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n2)
Continue..
• Best Case Complexity - In Quick sort, the best-case occurs
when the pivot element is the middle element or near to
the middle element. The best-case time complexity of quick
sort is O(n*logn).
• Average Case Complexity - It occurs when the array
elements are in jumbled order that is not properly
ascending and not properly descending. The average case
time complexity of quick sort is O(n*logn).
• Worst Case Complexity - In quick sort, worst case occurs
when the pivot element is either greatest or smallest
element. Suppose, if the pivot element is always the last
element of the array, the worst case would occur when the
given array is sorted already in ascending or descending
order. The worst-case time complexity of quick sort is O(n2).
Continue..
• Though the worst-case complexity of quicksort is
more than other sorting algorithms such as Merge
sort and Heap sort, still it is faster in practice.
Worst case in quick sort rarely occurs because by
changing the choice of pivot, it can be implemented
in different ways. Worst case in quicksort can be
avoided by choosing the right pivot element.
2. Space Complexity
The space complexity of quicksort is O(n*logn).
Index
• Merging arrays
• Merge Sort
• Complexity Analysis
Merging Arrays
Case Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n*logn)
Continue..
• Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of merge sort
is O(n*logn).
• Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of merge sort is O(n*logn).
• Worst Case Complexity - It occurs when the array elements are required
to be sorted in reverse order. That means suppose you have to sort the
array elements in ascending order, but its elements are in descending
order. The worst-case time complexity of merge sort is O(n*logn).
2. Space Complexity
The space complexity of merge sort is O(n). It is because, in merge sort,
an extra variable is required for swapping.
Index
• Multi-dimensional arrays
• Representation of Multi-dimensional arrays
Course Outcome
• CO1: Identify how arrays, linked lists, stacks,
queues, trees, and graphs are represented in
memory and used by algorithms
Course Objective
• To develop understanding among the students
about the concept of the data structures
• To demonstrate methods to perform operations on
different data structures
• To analyze the various operations for complexity
Multi-dimensional arrays
• A multi-dimensional array can be termed as an array
of arrays that stores homogeneous data in tabular
form. Data in multidimensional arrays are stored in
row-major order.
• The general form of declaring N-dimensional
arrays is:
data_type array_name[size1][size2]....[sizeN];
data_type: Type of data to be stored in the array.
array_name: Name of the array
size1, size2,… ,sizeN: Sizes of the dimension
Continue..
Examples:
Two dimensional array: int two_d[10][20];
Three dimensional array: int three_d[10][20][30];
• The total number of elements that can be stored in a
multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example:
• The array int x[10][20] can store total (10*20) = 200
elements.
• Similarly array int x[5][10][20] can store total (5*10*20) =
1000 elements.
Continue..
Two-Dimensional Array
Two – dimensional array is the simplest form of a
multidimensional array. We can see a two –
dimensional array as an array of one-dimensional
array for easier understanding.
• The basic form of declaring a two-dimensional array
of size x, y:
Syntax:
data_type array_name[x][y];
Here, data_type is the type of data to be stored.
Continue..
• We can declare a two-dimensional integer array say
‘x’ of size 10,20 as: int x[10][20];
• Elements in two-dimensional arrays are commonly
referred to by x[i][j] where i is the row number and
‘j’ is the column number.
• A two – dimensional array can be seen as a table
with ‘x’ rows and ‘y’ columns where the row
number ranges from 0 to (x-1) and the column
number ranges from 0 to (y-1).
Representation
• A two – dimensional array ‘x’ with 3 rows and 3
columns is shown below:
Continue..
• Initializing Two – Dimensional Arrays: There are
various ways in which a Two-Dimensional array can
be initialized.
• First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array has 3 rows and 4 columns. The elements
in the braces from left to right are stored in the table also
from left to right. The elements will be filled in the array in
order, the first 4 elements from the left in the first row, the
next 4 elements in the second row, and so on.
• Second Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
Continue..
• Third Method:
For queries
Email: vishal.e12820@cumail.in
105