Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Array and Sorting

Download as pdf or txt
Download as pdf or txt
You are on page 1of 100

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGG.
Bachelor of Engineering (Computer Science & Engineering)
DATA STRUCTURES

ARRAY IN DATA STRUCTURE DISCOVER . LEARN . EMPOWER


ARRAY IN DATA
STRUCTURE • https://www.sitesbay.com/data-
structure/c-array-data-structure
COURSE OUTCOMES

CO Number Course Outcome

Understand the concept of Linear and Nonlinear


CO1
data structures and their algorithms

Classifying various asymptotic notations to find


CO2
the complexity of an algorithm.
Identify the basic operations like insertion,
CO3 deletion and traversing on data structures.

Analyze and Compare searching and sorting


CO4
techniques.
CO3 is achieved in chapter 2
Evaluate appropriate data structure while
CO5
designing the algorithms.
2
Poll Question
Question : The data structure used in hierarchical data model is
(a) Array and Tree
(b) Linked list and Array
(c) Tree and Graph
(d) Graph and Linked List

3
Problem
Problem the developer were facing without using array concept.

int a; print f(“%d”,a); scanf(“%d”,&a);


int b;
int c;
int d;
or
int a,b, c, ..........;

4
Arrays
•An array, is a data structure consisting of a
collection of elements (values or variables).
•An array is a collection of items stored at
contiguous memory locations. The idea is to
store multiple items of the same type together.
•This makes it easier to calculate the position
of each element by simply adding an offset to a
base value, i.e., the memory location of the
first element of the array (generally denoted by
the name of the array).
•The elements of the array are stored in
consecutive memory locations and are
referenced by an index (also known as the
subscript)
•In C, the array index starts from zero. http://www.mathcs.emory.edu/~cheung/Courses/171/
Syllabus/1-intro/rev=arrays.html

5
DECLARATION OF ARRAYS
• Declaring an array means specifying the following:
• Data type—the kind of values it can store, for example, int, char, float,
double.
• Name—to identify the array.
• Size—the maximum number of values that the array can hold.
• Arrays are declared using the following syntax:
type name[size];
• For example :
• int mark[5] = {19, 10, 8, 17, 9};
• int mark[] = {19, 10, 8, 17, 9};
• In the memory, the array will be stored as

6
Poll Question
Question : An array stores all its data elements in non consecutive
memory locations.
• True
• False

7
Different Types of Arrays are Declared

8
Calculating the Length of an Array
• The length of an array is given by the number of elements stored in it.
Formula : Length = upper_bound – lower_bound + 1
• upper_bound is the index of the last element
• lower_bound is the index of the first element in the array

Example :Let Age[5] be an array of integers such that Age[0] = 2, Age[1]


= 5, Age[2] = 3, Age[3] = 1, Age[4] = 7 . Show the memory
representation of the array and calculate its length.
Solution: The memory representation of the array Age[5] is given as:
Length = upper_bound – lower_bound + 1
Here, lower_bound = 0, upper_bound = 4
Therefore, length = 4 – 0 + 1 = 5
9
Poll Question
Question : Lower bound is the index of the last element in an array
• True
• False

10
Calculating the Address of Array Elements
• an array stores all its data elements in consecutive memory locations,
storing just the base address, that is the address of the first element in
the array, is sufficient.
• The address of other data elements can simply be calculated using the
base address.
• Formula : Address of data element, A[k] = BA(A) + w(k – lower_bound)
• A is the array
• k is the index of the element of which we have to calculate the address,
• BA is the base address of the array A,
• w is the size of one element in memory,
• lower_bound is the index of the first element in the array.

11
Example of Calculating the Address of Array
Elements
Question: Given an array int marks[]={99,67,78,56,88,90,34,85}, calculate the
address of marks[4] if the base address = 1000.
Solution:

We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.
Address of data element, A[k] = BA(A) + w(k – lower_bound)
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008 12
OPERATIONS ON ARRAYS
1. Traversing an array
2. Inserting an element in an array
3. Searching an element in an array
4. Deleting an element from an array
5. Merging two arrays
6. Sorting an array in ascending or descending order

13
Poll Question
______ is the address of the first element in the array
a) Upper bound
b) Base address

14
Traversing an Array
• Traversing an array means accessing each and every element of the
array for a specific purpose.
• Traversing the data elements of an array A can include printing every
element, counting the total number of elements, or performing any
process on these elements.
• Array is a linear data structure (because all its elements form a
sequence), traversing its elements is very simple and straightforward.

15
The algorithm for array traversal
Step 1: [INITIALIZATION] SET I= lower_bound
Step 2: Repeat Steps 3 to 4 while I<= upper_bound
Step 3: Apply Process to A[I]
Step 4: SET I=I+1 [END OF LOOP]
Step 5: EXIT
• In Step 1, we initialize the index to the lower bound of the array.
• In Step 2, a while loop is executed.
• Step 3 processes the individual array element as specified by the array name and
index value.
• Step 4 increments the index value so that the next array element could be
processed. The while loop in Step 2 is executed until all the elements in the array
are processed, i.e., until I is less than or equal to the upper bound of the array.

16
Traversing Linear Arrays
Example : An automobile company uses an array AUTO to record the number of auto mobile sold
each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300 automobiles were sold.
b) Print each year and the number of automobiles sold in that year.
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : = NUM+1
3. Exit.

17
Example traversing: Write a program to read and display n numbers using an array
int main()
Output :
{
Enter the number of elements in the array : 5
int i, n, arr[20];
arr[0] = 1
clrscr();
arr[1] = 2
printf("\n Enter the number of elements in the array : ");
arr[2] = 3
scanf("%d", &n);
arr[3] = 4
for(i=0;i<n; i++)
arr[4] = 5
{
The array elements are 1 2 3 4 5
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}
18
Inserting an Element in an Array
• Insertion at the beginning of an array
• Insertion at the given index of an array
• Insertion after the given index of an array
• Insertion before the given index of an array

19
Poll Question
Question: If an array is declared as arr[] = {1,3,5,7,9}; then what is the
value of arr[3]?
(a) 1
(b) 7
(c) 9
(d) 5

20
Insertion at the Beginning of an Array
• When the insertion happens at the beginning, it causes all the existing data items
to shift one step downward.
• Here, we design and implement an algorithm to insert an element at the
beginning of an array.
• We assume A is an array with N elements. The maximum numbers of elements it
can store is defined by MAX. We shall first check if an array has any empty space
to store any element and then we proceed with the insertion process
• Algorithm:
begin
IF N = MAX, return
ELSE N = N + 1
For All Elements in A Move to next adjacent
location
A[FIRST] = New_Element
end
21
Program: Insertion at the Beginning of an Array
void main() array[index] = value; // add new element at first position
{ N++; // increase N to reflect number of elements
int array[MAX] = {1, 2, 4, 5}; printf("Printing array after insertion −\n"); // print to
int N = 4; // number of elements in array confirm

int i = 0; // loop variable


for(i = 0; i < N; i++)

int index = 2; // index location to insert new value


{

int value = 3; // new data element to be inserted


printf("array[%d] = %d\n", i, array[i]);

printf("Printing array before insertion −\n"); // print array before }


insertion
}
for(i = 0; i < N; i++) Output:
{
printf("array[%d] = %d \n", i, array[i]);
}
for(i = N; i >= index; i--) // now shift rest of the elements downwards
{
array[i+1] = array[i];
}

22
Insertion After the Given Index of an Array

• In this scenario given a location


(index) of an array after which a
new data element (value) has to be
inserted.
• Only the seek process varies, the
rest of the activities are the same
as in the previous example.
• We assume A is an array
with N elements. The maximum
numbers of elements it can store is
defined by MAX.
23
Program:Insertion After the Given Index of an Array
#include <stdio.h> array[i + 1] = array[i];
#define MAX 5 }
// add new element at first position
void main()
array[index + 1] = value;
{
// increase N to reflect number of elements
int array[MAX] = {1, 2, 4, 5};
N++;
int N = 4; // number of elements in array
// print to confirm
int i = 0; // loop variable printf("Printing array after insertion −\n");
int index = 1; // index location after which value will be inserted for(i = 0; i < N; i++)
int value = 3; // new data element to be inserted {
// print array before insertion printf("array[%d] = %d\n", i, array[i]);
printf("Printing array before insertion −\n"); }}
for(i = 0; i < N; i++)
{
printf("array[%d] = %d \n", i, array[i]);
}
// now shift rest of the elements downwards

for(i = N; i >= index + 1; i--)


{
24
Insertion After the Given Index of an Array
• In this scenario we are given a
location (index) of an array after
which a new data element
(value) has to be inserted.
• Only the seek process varies, the
rest of the activities are the
same as in the previous
example.
Algorithm
• We assume A is an array
with N elements. The maximum
numbers of elements it can store
is defined by MAX.
25
Program: Insertion After the Given Index of an Array
#include <stdio.h> // add new element at first position
#define MAX 5 array[index + 1] = value;
void main() // increase N to reflect number of elements
{ N++;
int array[MAX] = {1, 2, 4, 5}; // print to confirm printf("Printing array after insertion −\n");
int N = 4; // number of elements in array for(i = 0; i < N; i++)
int i = 0; // loop variable {
int index = 1; // index location after which value will printf("array[%d] = %d\n", i, array[i]);
}}
be inserted
int value = 3; // new data element to be inserted
// print array before insertion
printf("Printing array before insertion −\n");
for(i = 0; i < N; i++)
{
printf("array[%d] = %d \n", i, array[i]);
}
// now shift rest of the elements downwards
for(i = N; i >= index + 1; i--)
{ array[i + 1] = array[i];
}

26
Insertion Before the Given Index of an Array
• In this scenario a location
(index) of an array before which
a new data element (value) has
to be inserted. This time we seek
till index-1 i.e., one location
ahead of given index, rest of the
activities are same as in previous
example.
• Algorithm
• We assume A is an array
with N elements. The maximum
numbers of elements it can store
is defined by MAX.

27
Program: Insertion Before the Given Index of an Array
#include <stdio.h>
#define MAX 5 // add new element at first position
void main() array[index + 1] = value;
{ // increase N to reflect number of elements
int array[MAX] = {1, 2, 4, 5}; N++;
int N = 4; // number of elements in array // print to confirm
int i = 0; // loop variable printf("Printing array after insertion −\n");
int index = 3; // index location before which value will be for(i = 0; i < N; i++)
inserted {
int value = 3; // new data element to be inserted printf("array[%d] = %d\n", i, array[i]);
// print array before insertion }}
printf("Printing array before insertion −\n");
for(i = 0; i < N; i++)
{
printf("array[%d] = %d \n", i, array[i]);
}
// now shift rest of the elements downwards
for(i = N; i >= index + 1; i--)
{
array[i + 1] = array[i];
}

28
Delete An Element From The Middle Of An Array
• Deleting an element from an array means Step 1: [INITIALIZATION] SETI= POS
removing a data element from an already Step 2: Repeat Steps 3 and 4 while I<=N–1
existing array
• The algorithm DELETE will be declared as Step 3: SET A[I] = A[I+1]
DELETE(A, N, POS). Step 4: SETI=I+1 [END OF LOOP]
The arguments are: Step 5: SETN=N–1
(a) A, the array from which the element has to Step 6: EXIT
be deleted first initialize I with the position from which the
(b) N, the number of elements in the array element has to be deleted.
(c) POS, the position from which the element In Step 2, a while loop is executed which will move
has to be deleted all the elements having an index greater than POS
one space towards left to occupy the space
vacated by the deleted element.
When we say that we are deleting an element,
actually we are overwriting the element with the
value of its successive element.
In Step 5, we decrement the total number of
elements in the array by 1

29
SEARCHING ALGORTHIMS
• Searching is a process of finding a particular element among several
given elements.
• The search is successful if the required element is found.

30
Linear Search
• A linear search, also known as a sequential search, is a
method of finding an element within a list. It checks each
element of the list sequentially until a match is found or the
whole list has been searched.
• t traverses the array sequentially to locate the required
element.
• It searches for an element by comparing it with each element
of the array one by one.
• Linear Search Algorithm is applied when-
• No information is given about the array.
• The given array is unsorted or the elements are unordered.
• The list of data items is smaller.
• Complexity
• Worst case time complexity: O(N)
• Average case time complexity: O(N)
• Best case time complexity: O(1)
• Space complexity: O(1)

31
Linear Search Algorithm

Linear Search ( Array A, Value x) • initially, we need to mention or accept


the element to be searched from the
user.
Step 1: Set i to 1 • Then, we create a for loop and start
Step 2: if i > n then go to step 7 searching for the element in a sequential
fashion.
Step 3: if A[i] = x then go to step 6
• As soon as the compiler encounters a
Step 4: Set i to i + 1 match i.e. array[element] == key value,
Step 5: Go to Step 2 return the element along with its position
in the array.
Step 6: Print Element x Found at index i and
go to step 8 • If no values are found that match the
input, it returns -1
Step 7: Print element not found
Step 8: Exit

32
Program for Linear Searching Algorithm
#include <stdio.h> int key = 100;
int size = 10;
int LINEAR_SEARCH(int inp_arr[], int size, int val) int res = LINEAR_SEARCH(arr, size, key);
{ if (res == -1)

for (int i = 0; i < size; i++) printf("ELEMENT NOT FOUND!!");


if (inp_arr[i] == val) else
return i; printf("Item is present at index %d", res);
return -1;
return 0;
}
}
int main(void)
{
int arr[] = { 10, 20, 30, 40, 50, 100, 0 };

33
Poll Question
A linear search algorithm is also known as a...
A. Binary search algorithm
B. Bubble sort algorithm
C. Sequential search algorithm

34
Poll Question
Which of these is a type of searching algorithm?
A. Linear search
B. Word search
C. Search engine

35
Binary Search
• Binary Search is one of the fastest searching BINARY_SEARCH(A, lower_bound, upper_bound,
algorithms. VAL)
• Binary search is a fast search algorithm with Step 1: [INITIALIZE] SET BEG = lower_bound
run-time complexity of Ο(log n). END = upper_bound, POS = - 1
• This search algorithm works on the principle Step 2: Repeat Steps 3 and 4 while BEG <=END
of divide and conquer. Step 3: SET MID = (BEG + END)/2
• For this algorithm to work properly, the data Step 4: IF A[MID] = VAL
collection should be in the sorted form SET POS = MID
• Instead of performing the search by going PRINT POS
through the data in a sequence, the binary Go to Step 6
algorithm randomly accesses the data to find ELSE IF A[MID] > VAL
the required element. This makes the search SET END = MID - 1
cycles shorter and more accurate. ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT

36
Example Binary Search

First, we shall determine half of the array by using this formula


mid = low + (high - low) /2
0 + (9 - 0 ) / 2 = 4

37
Example Binary Search cont.
we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the value at
location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we also know
that the target value must be in the upper portion of the array.

We change our low to mid + 1 and find the new mid value again.
low = mid +1
mid = low + (high - low) /2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value must be
in the lower part from this location.

38
Example Binary Search cont.
.

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We


find that it is a match.

39
Program for Binary Search
#include <stdio.h> else if(array[index] < s)
int main() {
{ low = index+1;
int array[11] = {2,3,4,5,6,7,8,9,22,33,45}; }
int low = 0; else{
int high = 10; high = index-1;
int flag = 0; }
int s = 4; }
while(low <= high){ if(flag == 0)
int index = low+(high-low)/2; {
if(array[index] == s){ printf("Not Found!\n");
flag = 1; }
printf("Founded: %d \n",index);
break; return 0;
} }

40
Poll Question
On average, which searching algorithm is more efficient?
A. Binary Search
B. Linear Search

41
Poll Question
The complexity of binary search algorithm is
(a) O(n)
(b) O(n2 )
(c) O(n log n)
(d) O(log n)

42
Sorting Algorithms

• Sorting Algorithms are methods of Types of Sorting Algorithms:


reorganizing a large number of items 1. Quick Sort
into some specific order such as
highest to lowest, or vice-versa, or 2. Bubble Sort
even in some alphabetical order. 3. Merge Sort
• These algorithms take an input list, 4. Insertion Sort
processes it (i.e, performs some
operations on it) and produce the 5. Selection Sort
sorted list. 6. Heap Sort
• The most common example we 7. Radix Sort
experience every day is sorting clothes
or other items on an e-commerce 8. Bucket Sort
website either by lowest-price to
highest, or list by popularity, or some
other order.

43
Bubble Sort Algorithm
• Bubble sort is a simple sorting
algorithm.
• This sorting algorithm is
comparison-based algorithm in
which each pair of adjacent
elements is compared and the
elements are swapped if they are
not in order.
• This algorithm is not suitable for
large data sets as its average and
worst case complexity are of Ο(n2)
where n is the number of items.

44
Complexity Analysis of Bubble Sort
• In Bubble Sort, n-1 comparisons will be done in the 1st pass, n-2 in 2nd pass, n-3 in 3rd
pass and so on. So the total number of comparisons will be,
(n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1
Sum = n(n-1)/2
i.e O(n2)
• Hence the time complexity of Bubble Sort is O(n2).
The main advantage of Bubble Sort is the simplicity of the algorithm.
• The space complexity for Bubble Sort is O(1), because only a single additional memory
space is required i.e. for temp variable.
• Also, the best case time complexity will be O(n), it is when the list is already sorted.
• Following are the Time and Space complexity for the Bubble Sort algorithm.
• Worst Case Time Complexity [ Big-O ]: O(n2)
• Best Case Time Complexity [Big-omega]: O(n)
• Average Time Complexity [Big-theta]: O(n2)
• Space Complexity: O(1)

45
Example of Bubble Sort cont.

Bubble sort starts with very first two elements, comparing them to
check which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted


locations. Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be


swapped.

46
Example of Bubble Sort cont.

The new array should look like this −


Next we compare 33 and 35. We find that both are in already sorted
positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.

47
Example of Bubble Sort cont.
We swap these values. We find that we have reached the end of the
array. After one iteration, the array should look like this −

To be precise, we are now showing how an array should look like after
each iteration. After the second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

48
Algorithm of Bubble Sort
We assume list is an array of n elements. We further assume that swap function swaps the values of
the given array elements.
begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

49
Program Bubble Sort
#define MAX 10 // loop through all numbers
int list[MAX] = {1,8,4,6,0,3,5,2,7,9}; for(i = 0; i < MAX-1; i++)
{
void display() {
swapped = false
int i;
int temp;
printf("[“]); int i,j;
// navigate through all items bool swapped = false;
for(i = 0; i < MAX; i++) { // loop through all numbers
printf("%d ",list[i]); for(i = 0; i < MAX-1; i++)
{
}
swapped = false;
printf("]\n");
// loop through numbers falling ahead
}
for(j = 0; j < MAX-1-i; j++) {
void bubbleSort() printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]);
{
int temp;
int i,j;
bool swapped = false;

50
Program Bubble Sort cont.
// check if next number is lesser than current no // if no number was swapped that means
// swap the numbers. // array is sorted now, break the loop.
// (Bubble up the highest number) if(!swapped) {
break; }
if(list[j] > list[j+1]) { printf("Iteration %d#: ",(i+1));
temp = list[j]; display();
list[j] = list[j+1]; }
list[j+1] = temp; }
swapped = true; void main() {
printf(" => swapped [%d, %d]\n",list[j],list[j+1]); printf("Input Array: ");
} else { display();
printf(" => not swapped\n"); printf("\n");
}
bubbleSort();
} printf("\nOutput Array: ");
display();
}

51
Poll Question
In which sorting, consecutive adjacent pairs of elements in the array
are compared with each other?
(a) Bubble sort
(b) Binary Search

52
Insertion Sort Algorithm
• Insertion sort is a very simple sorting
algorithm in which the sorted array (or list)
is built one element at a time.
• The main idea behind insertion sort is that
it inserts each item into its proper place in
the final list.
• To save memory, most implementations of
the insertion sort algorithm work by
moving the current data element past the
already sorted values and repeatedly
interchanging it with the preceding value
until it is in its correct place.
• Insertion sort is less efficient as compared
to other more advanced algorithms such as
quick sort, heap sort, and merge sort.

53
Properties Insertion Sort Algorithm
• INSERTION-SORT can take different amounts of time to sort two input
sequences of the same size depending on how nearly sorted they already
are.
• In INSERTION-SORT, the best case occurs if the array is already sorted.
• T [Best Case]= O(n)
• If the array is in reverse sorted order i.e in decreasing order, INSERTION-
SORT gives the worst case results.
• T [Worst Case]= θ(n²)
• Average Case: When half the elements are sorted while half not
• The running time of insertion sort therefore belongs to both Ω(n) and O(n²)

54
Technique Insertion Sort
• The array of values to be sorted is divided into two sets. One that stores
sorted values and another that contains unsorted values.
• The sorting algorithm will proceed until there are elements in the unsorted
set.
• Suppose there are n elements in the array. Initially, the element with index
0 (assuming LB = 0) is in the sorted set. Rest of the elements are in the
unsorted set.
• The first element of the unsorted partition has array index 1 (if LB = 0).
• During each iteration of the algorithm, the first element in the unsorted set
is picked up and inserted into the correct position in the sorted set.

55
Example of Insertion Sort cont.

• Insertion sort compares the first two elements.

• It finds that both 14 and 33 are already in ascending order. For now,
14 is in sorted sub-list.

• Insertion sort moves ahead and compares 33 with 27.

56
Example of Insertion Sort cont.
• And finds that 33 is not in the correct position.

• It swaps 33 with 27. It also checks with all the elements of sorted sub-
list. Here we see that the sorted sub-list has only one element 14, and
27 is greater than 14. Hence, the sorted sub-list remains sorted after
swapping.

• By now we have 14 and 27 in the sorted sub-list. Next, it compares 33


with 10.

57
Example of Insertion Sort cont.
• These values are not in a sorted order.

• So we swap them.
• However, swapping makes 27 and 10 unsorted.

• Hence, we swap them too.


• Again we find 14 and 10 in an unsorted order.

58
Example of Insertion Sort cont.
• We swap them again. By the end of third iteration, we have a sorted
sub-list of 4 items.

• This process goes on until all the unsorted values are covered in a
sorted sub-list. Now we shall see some programming aspects of
insertion sort.

59
Insertion Sort Algorithm
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than
the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

60
Complexity Analysis of Insertion Sort

• Insertion sort is an efficient sorting algorithm, as it does not run on present


conditions using for loops, but instead it uses one while loop, which avoids
extra steps once the array gets sorted.
• Insertion sort is efficient, still, if we provide an already sorted array to the
insertion sort algorithm, it will still execute the outer for loop, thereby
requiring n steps to sort an already sorted array of n elements, which
makes its best case time complexity a linear function of n.
• Worst Case Time Complexity [ Big-O ]: O(n2)
• Best Case Time Complexity [Big-omega]: O(n)
• Average Time Complexity [Big-theta]: O(n2)
• Space Complexity: O(1)

61
Advantages of Insertion Sort
• It is easy to implement and efficient to use on small sets of data.
• It can be efficiently implemented on data sets that are already
substantially sorted.
• It performs better than algorithms like selection sort and bubble sort.
Insertion sort algorithm is simpler than shell sort, with only a small trade-
off in efficiency. It is over twice as fast as the bubble sort and almost 40 per
cent faster than the selection sort.
• For nearly-sorted data, it’s incredibly efficient (very near O(n) complexity)
• It works in-place, which means no auxiliary storage is necessary i.e.
requires only a constant amount O(1) of additional memory space
• Efficient for (quite) small data sets.
• Stable, i.e. does not change the relative order of elements with equal keys

62
disadvantages of Insertion Sort
• It is less efficient on list containing more number of elements
• Insertion sort needs a large number of element shifts

63
SELECTION SORT
• Selection sort is a sorting algorithm
that has a quadratic running time
complexity of O(n2 ), thereby making
it inefficient to be used on large lists.
• Although selection sort performs
worse than insertion sort algorithm, it
is noted for its simplicity and also has
performance advantages over more
complicated algorithms in certain
situations.
• Selection sort is generally used for
sorting files with very large objects
(records) and small keys.

64
Example Of Selection Sort
• For the first position in the sorted list, the whole list is scanned
sequentially. The first position where 14 is stored presently, we search
the whole list and find that 10 is the lowest value.

• So we replace 14 with 10. After one iteration 10, which happens to be


the minimum value in the list, appears in the first position of the
sorted list.

65
Example Of Selection Sort
• For the second position, where 33 is residing, we start scanning the
rest of the list in a linear manner.

• We find that 14 is the second lowest value in the list and it should
appear at the second place. We swap these values.

• After two iterations, two least values are positioned at the beginning
in a sorted manner.

66
Example Of Selection Sort
The same process is applied to the
rest of the items in the array.
Following is a pictorial depiction
of the entire sorting process −

67
Algorithm Selection Sort

Step 1 − Set MIN to location 0


Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted

68
Selection Sort
Advantages Disadvantages
The main advantage of the The primary disadvantage of the
selection sort is that it performs selection sort is its poor efficiency
well on a small list. when dealing with a huge list of
items.
Because it is an in-place sorting The selection sort requires n-
algorithm, no additional temporary squared number of steps for
storage is required beyond what is sorting n elements.
needed to hold the original list.
Its performance is easily influenced Quick Sort is much more efficient
by the initial ordering of the items than selection sort
before the sorting process.
69
Complexity Analysis of Selection Sort

• Selection Sort requires two nested for loops to complete itself,


one for loop is in the function selectionSort, and inside the first loop
we are making a call to another function indexOfMinimum, which has
the second(inner) for loop.
• Hence for a given input size of n, following will be the time and space
complexity for selection sort algorithm:
• Worst Case Time Complexity [ Big-O ]: O(n2)
• Best Case Time Complexity [Big-omega]: O(n2)
• Average Time Complexity [Big-theta]: O(n2)
• Space Complexity: O(1)

70
Poll Question
For insertion sort, the best case occurs when the array is already
sorted.
True
False.

71
Merge Sort Algorithm

• Merge sort is a sorting algorithm


that uses the divide, conquer, and
combine algorithmic paradigm.
• Merge sort repeatedly breaks
down a list into several sublists
until each sublist consists of a
single element and merging those
sublists in a manner that results
into a sorted list.
• Merge Sort is quite fast, and has a
time complexity of O(n*log n).
72
Example of Merge Sort cont.

• We know that merge sort first divides the whole array iteratively into
equal halves unless the atomic values are achieved. We see here that
an array of 8 items is divided into two arrays of size 4.

• This does not change the sequence of appearance of items in the


original. Now we divide these two arrays into halves.

73
Example of Merge Sort cont.
• We further divide these arrays and we achieve atomic value which can no more
be divided.

• Now, we combine them in exactly the same manner as they were broken down.
Please note the color codes given to these lists.
• We first compare the element for each list and then combine them into another
list in a sorted manner. We see that 14 and 33 are in sorted positions. We
compare 27 and 10 and in the target list of 2 values we put 10 first, followed by
27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.

74
Example of Merge Sort cont.
• In the next iteration of the combining phase, we compare lists of two
data values, and merge them into a list of found data values placing
all in a sorted order.

• After the final merging, the list should look like this −

75
Algorithm Merge Sort

• Merge sort keeps on dividing the list into equal halves until it can no
more be divided. By definition, if it is only one element in the list, it is
sorted. Then, merge sort combines the smaller sorted lists keeping
the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more
be divided.
Step 3 − merge the smaller lists into new list in sorted order.

76
Complexity Analysis of Merge Sort
• Time complexity of Merge Sort is O(n*log n) in all the 3 cases (worst,
average and best) as merge sort always divides the array in two
halves and takes linear time to merge two halves.
• It requires equal amount of additional space as the unsorted array.
Hence its not at all recommended for searching large unsorted arrays.
• It is the best Sorting technique used for sorting Linked Lists.

77
Advantages and disadvantages of merge sort
Pros:
• It is quicker for larger lists because unlike insertion it doesn't go through the whole list
several times.
• The merge sort is slightly faster than the heap sort for larger sets
• 𝑂(𝑛𝑙𝑜𝑔𝑛) worst case asymptotic complexity.
• Stable sorting algorithm
Cons
• Slower comparative to the other sort algorithms for smaller data sets
• Marginally slower than quick sort in practice
• Goes through the whole process even if the list is sorted
• It uses more memory space to store the sub elements of the initial split list.
• It requires twice the memory of the heap sort because of the second array.

78
Poll Question
Which algorithm uses the divide, conquer, and combine algorithmic
paradigm?
(a) Selection sort
(b) Insertion sort
(c) Merge sort

79
Poll Question
What are the Types of Arrays ?
A) int, long, float, double
B) struct, enum
C) char
D) All the above.

80
One Dimensional Arrays
• A one-dimensional array is one in which only one subscript
specification is needed to specify a particular element of the array.
• A one-dimensional array is a list of related variables. Such lists are
common in programming.
One-dimensional array can be declared as follows :
Data_type var_name[Expression];
• The initializers are specified within braces and separated by commas.
int ex[5] = { 10, 5, 15, 20, 25};
char word[10] = { 'h', 'e', 'l', 'l', 'o' } ;

81
Two Dimensional Array

• Two dimensional arrays are also called table or matrix, two dimensional
arrays have two subscripts.
• Two dimensional array in which elements are stored column by column is
called as column major matrix.
• Two dimensional array in which elements are stored row by row is called as
row major matrix.
• First subscript denotes number of rows and second subscript denotes the
number of columns.
• 2D arrays are created to implement a relational database look alike data
structure
• The simplest form of the Multi Dimensionl Array is the Two Dimensionl
Array. A Multi Dimensionl Array is essence a list of One Dimensionl Arrays.

82
Two Dimensional Array

syntax of declaring two •


dimensional array:
• int arr[max_rows][max_columns];
Two dimensional arrays can be
declared as follows :
• int int_array2d[10][10] ; // A two
dimensional array
Initializing a Two Dimensional
• The two dimensional array, the elements are
Array: organized in the form of rows and columns. First
• int array[3][3] = { 1, 2, 3, 4, 5, 6, 7, element of the first row is represented by a[0][0]
where the number shown in the first index is the
8, 9 } ; number of that row while the number shown in
the second index is the number of the column.

83
Multidimensional Arrays
• Multidimensional arrays are arrays with
two or more dimensions, i.e. tables with
values for "rows", "columns" and possibly
for additional "axes" or "coordinates".
• Multidimensional arrays use additional
subscripts for indexing
The first references array dimension
1, the row.
The second references dimension 2,
the column.
The third references dimension 3.
This illustration uses the concept of
a page to represent dimensions 3 and
higher.

84
Example
• To access the element in the second row, third column of page 2, for
example, you use the subscripts (2,3,2)

85
Poll Question
If an array is declared as int arr[5][5], how many elements can it store?
(a) 5
(b) 25
(c) 10
(d) 0

86
Poll Question
What are the legal indexes for the array ar, given the following
declaration: int[] ar = {2, 4, 6, 8 }
A. 0, 1, 2, 3
B. 1, 2, 3, 4
C. 2, 4, 6, 8
D. 0, 2, 4, 6

87
Pointers in Data Structure
• Pointers are the variables that are used to store the location of value
present in the memory.
• A pointer to a location stores its memory address.
• The process of obtaining the value stored at a location being referenced by
a pointer is known as dereferencing.
• It is the same as the index for a textbook where each page is referred by its
page number present in the index. One can easily find the page using the
location referred to there.
• Such pointers usage helps in the dynamic implementation of various data
structures such as stack or list.
• Syntax: <datatype> *variable_name

88
Why do We Need Pointers in Data Structure

• Optimization of our code and improving the time complexity of one


algorithm.
• using pointers helps to reduce the time needed by an algorithm to
copy data from one place to another.
• it used the memory locations directly, any change made to the value
will be reflected at all the locations.

89
Poll Question
What does the following declaration mean?
int (*ptr)[10];
A. ptr is array of pointers to 10 integers
B. ptr is a pointer to an array of 10 integers.

90
Record
• A record is typically is used when a data file is organized into a table
structure.
• In such organization of data, a record belongs to a hierarchy of fields,
records and files.
• We can define record as a structured data type made up of a finite
collection of not necessarily homogenous (of same type) but related data
elements.
• The related data elements bound together in a record structure are
called fields or attributes.
• A file is a collection of similar records. Each data item itself may be a group
item composed of sub items; those items which are indecomposable are
called elementary items or atoms. Fields in a record are named and used as
identifiers.

91
Record Structure
• A record is a complex data type that is • Syntax:
built up of other items of data (each RECORD Artwork
of which of course has its own type). title
artist
• For example, the following
year_created
information could be associated with
works of art: on_public_display
ENDRECORD
title (of type string)
• The ::struct ::record package provides a
 artist (of type string) mechanism to group variables together as
year it was created (of type one data structure, similar to a 'C' structure.
integer) struct date {
on public display? (of type int year;
Boolean) int month;
int day;
• Records are a way to aggregate (put
};
together) different items of data.
92
Parallel Array

• a parallel array is a data • For example in our contact list


structure for program you might have 4 different
representing arrays of records. arrays
• It keeps a separate, homogeneous firstName()
array for each field of the record, lastName()
each having the same number of phoneNumber()
elements. Then, objects located at address()
the same index in each array are • The data needs to be stored in the
implicitly the fields of a single arrays in a specifc order so that
record. firstName(5) will match up with
• Parallel Arrays are simply multiple lastName(5), phoneNumber(5) and
one dimensional arrays that address(5)
contain inter related data.

93
Example Parallel Array
• Dog name
Wally Skeeter Corky Jessie Sadie

• Round1 18 22 12 17 15

• Round2 20 25 16 18 17

• In the data represented above, the first array is the dog's name, the second array is the
dog's score in round 1 of the competition, and the third array is the dog's score in round
2 of the competition. The arrays are parallel, in that the dog in the first element of the
first array has the scores represented in the first elements of the second and third arrays.

94
Sparse Matrix

• A matrix is known as a sparse


matrix when it contains more ZERO
values than NON-ZERO values.
• In order to efficiently utilize the
memory, specialized algorithms
and data structures that take
advantage of the sparse structure
should be used.
• Sparse data can be easily
compressed, which in turn can
significantly reduce memory usage.
95
Sparse Matrix Representations

Triplet Representation (Array • For example, consider a matrix of


Representation) size 5 X 6 containing 6 number of
• In this representation, we consider non-zero values.
only non-zero values along with
their row and column index values.
• In this representation, the 0th row
stores the total number of rows,
total number of columns and the
total number of non-zero values in
the sparse matrix.
• The representation is of the type:
< row, col, element>

96
Sparse Matrix Representations
Linked Representation
• In linked representation, we use a linked list data structure to represent a sparse matrix.
• In linked list, each node has four fields.
o Row: Index of row, where non-zero element is located
o Column: Index of column, where non-zero element is located
o Value: Value of the non zero element located at index – (row, column)
o Next node: Address of the next node

97
Applications
• Array can be used for sorting elements, can perform matrix operation
& can be used in CPU scheduling.
• Stack is used in Expression evaluation
• Disk Scheduling.

98
REFERENCES

• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India

99
THANK YOU

For queries
Email: vaishali.cse@cumail.in

You might also like