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

DS Module 1

This document provides an introduction to data structures. It defines data structures as a method of organizing large amounts of data efficiently so that operations on that data are easy. Data structures can be primitive, like integers and characters, or non-primitive, like arrays and lists. The document discusses linear data structures like arrays, stacks, queues and linked lists that organize data in a linear order, as well as non-linear structures like trees and graphs. It also covers common operations on lists like insertion, deletion and searching, and sorting techniques like insertion sort and bubble sort.

Uploaded by

joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DS Module 1

This document provides an introduction to data structures. It defines data structures as a method of organizing large amounts of data efficiently so that operations on that data are easy. Data structures can be primitive, like integers and characters, or non-primitive, like arrays and lists. The document discusses linear data structures like arrays, stacks, queues and linked lists that organize data in a linear order, as well as non-linear structures like trees and graphs. It also covers common operations on lists like insertion, deletion and searching, and sorting techniques like insertion sort and bubble sort.

Uploaded by

joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

1 Introduction to Data Structures

Whenever we want to work with large amount of data, then organizing


that data is very important.

If that data is not organized effectively, it is very difficult to perform any


task on that data.

If it is organized effectively then any operation can be performed easily


on that data.
A data structure can be defined as follows..
Data structure is a method of organizing large amount of data more
efficiently so that any operation on that data becomes easy.

Every data structure is used to organize the large amount of data


Every data structure follows a particular principle .

The operations in a data structure should not violate the basic


principle of that data structure.
Data structures are divided into two types:
Primitive data structures.
 Non-primitive data structures.
• Primitive Data Structures are the basic data structures that directly
operate upon the machine instructions.
• They have different representations on different computers
• Integers, floating point numbers, character constants, string
constants and pointers come under this category.
• Non-primitive data structures are more complicated data structures
and are derived from primitive data structures.

• They emphasize on grouping same or different data items with


relationship between each data item.

• Arrays, lists and files come under this category.


• A data structure is said to be linear if its elements form a sequence or
a linear list. The linear data structures like an array, stacks, queues and
linked lists organize data in linear order.

• A data structure is said to be non linear if its elements form a


hierarchical classification where, data items appear at various levels.
 Trees and Graphs are widely used non-linear data
structures. Tree and graph structures represents hierarchial
relationship between individual data elements. Graphs are
nothing but trees with certain restrictions removed
Linear Lists
• A list is a finite sequence of zero or more elements. If the elements
are all of type T , then we say that the type of the list is “list of T .”

• Thus we can have lists of integers, lists of real numbers, lists of


structures and so on. A list is often written with its elements
separated by commas and enclosed in parentheses: (a1, a2, . . . , an)
where the an’s are the elements of the list
Examples: 1. The list of prime numbers less than 20, in
order of size: (2, 3, 5, 7, 11, 13, 17, 19)

2.The list of noble gasses, in order of atomic weight:


(helium, neon, argon, krypton, xenon, radon)

3. The list of the numbers of days in the months of a


non-leap year: (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31) As this example reminds us, the same element can
appear more than once on a list.
The Length of a List:
• The length of a list is the number of occurrences of elements on the
list.
• If the number of elements is zero, then the list is said to be Empty list.
• We use the Greek letter € (epsilon) to represent the empty list.
• We can also represent the empty list by a pair of parentheses
surrounding nothing: ().
• It is important to remember that length counts positions, not distinct
symbols, and so a symbol appearing k times on a list adds k to the
length of the list
Examples:
1. The list of prime numbers less than 20, in order of size: (2, 3, 5, 7, 11, 13, 17, 19)
Length=8

2. The list of noble gasses, in order of atomic weight: (helium, neon, argon, krypton,
xenon, radon)
Length=6

3. The list of the numbers of days in the months of a non-leap year: (31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31)

Length=12
Parts of a List:
• If a list is not empty, then it consists of a first element, called the head
and the list remainder of the list, called the tail.
• For instance, the head of list (helium, neon, argon, krypton, xenon,
radon) is helium,

• while the tail is the list consisting of the remaining five elements
(neon, argon, krypton, xenon, radon)
Operations on Lists
• Insertion
• Deletion
• Lookup
Data Representation Methodologies
• The common data representation or data stored methods are
i) Formula (array)based representation
ii) Linked list(pointer) representation
iii) Indirect addressing representation
Formula based representation:
• In this representation, we use a mathematical formula to determine
where to store elements of a list.
• The simplest case is to store successive element of a list in successive
memory location.

• For example: An array. location(i) = i - 1, so we can find ith element in


array[i-1]
Linked list representation:
• In this representation, the element of a list may be stored in any
arbitrary set of memory locations.
• Each element has an explicit pointer that tells the location of the next
element in the list.
• For example: A linked list. Single linked list operations:
1. Insert end
2. Insert begin
3. Insert position
4. Delete begin
5. Delete position
6. Delete end
7. Traversing the List
Indirect addressing representation:
• A combination of 1) and 2). That means the list elements may be
stored in any arbitrary set of location, but we maintain a table such
that ith table entry to tell us where the ith list element is. So, the
table stores the addresses of the list element.
Searching: Linear and Binary
• Linear Search:
• Linear search algorithm finds given element in a list of elements with O(n)
time complexity where n is total number of elements in the list.
• This search process starts comparing of search element with the first element
in the list.
• If both are matching then results with element found otherwise search
element is compared with next element in the list.
• If both are matched, then the result is "element found".
• Otherwise, repeat the same with the next element in the list until search
element is compared with last element in the list, if that last element also
doesn't match, then the result is "Element not found in the list".
Linear search is implemented using following
steps..
• Step 1: Read the search element from the user
• Step 2: Compare, the search element with the first element in the list.
Step 3: If both are matching, then display "Given element found!!!"
and terminate the function
• Step 4: If both are not matching, then compare search element with
the next element in the list.
• Step 5: Repeat steps 3 and 4 until the search element is compared
with the last element in the list.
• Step 6: If the last element in the list is also doesn't match, then
display "Element not found!!!" and terminate the function.
Example:
• Consider the following list of element and search element...
BINARY SEARCH:
Binary SEARCH ALGORITHM
Begin
Set beg = 0
Set end = n-1
Set mid = (beg + end) / 2
while ( (beg <= end) and (a[mid] ≠ item) ) do
if (item < a[mid]) then
Set end = mid - 1
else
Set beg = mid + 1
endif
Set mid = (beg + end) / 2
endwhile
if (beg > end) then
Set loc = -1
else
Set loc = mid
endif
End
Explanation

• Binary Search Algorithm searches an element by comparing it with the


middle most element of the array.
Then, following three cases are possible-

• Case-01
If the element being searched is found to be the middle most element, its
index is returned.
• Case-02
• If the element being searched is found to be greater than the middle
most element,
• then its search is further continued in the right sub array of the middle
most element.
Case-03

• If the element being searched is found to be smaller than the middle most
element,
• then its search is further continued in the left sub array of the middle most
element.

• Note: This iteration keeps on repeating on the sub arrays until the desired
element is found or size of the sub array reduces to zero.
Sorting Technique
• Sorting is storage of data in sorted order; it can be ascending or
descending order.
• Sorting makes searching faster.
• There are so many things in our life where searching is needed for
getting information.
• The sorting methods can be divided into two parts i.e. internal and
external.
• In internal sorting, data that is going to be sorted will be in main
memory
• In external sort, data will be on auxiliary storage like tape, floppy, disk
etc.
• The insertion sort inserts each element in proper place.
• This is same as playing cards, in which we place the cards in proper
order.
• There are n elements in the array and we place each element of array
at proper place in the previously sorted element list.
• Let us take there are n elements the array arr. Then process of inserting each
element in proper place is as
• Pass 1- arr[0] is already sorted because of only one element.
• Pass 2-arr[1] is inserted before or after arr[0]. So arr[0] and arr[1] are sorted.
• Pass 3- arr[2] is inserted before arr[0] or in between arr[0] and arr[1] or after
arr[1]. So arr[0], arr[1] and arr[2] are sorted
• Pass 4- arr[3] is inserted into its proper place in array arr[0], arr[1], arr[2] So,
arr[0] arr[1] arr[2] and arr[3] are sorted.
• ………………………………………. …………………………………………
………………………………………..
• Pass N- arr[n-1] is inserted into its proper place in array.
Algorithm
• Insertion(int x[ ],int n)
• 1. Start
• 2. set i=1
• 3. repeat the steps 4,5,8 and 9 while(i<n)
• 4. set key=x[i] and j=i-1
• 5. repeat the steps 6 and 7 while j>=0 && x[j]>key
• 6. set x[j+1] =x[j]
• 7. j=j-1
• 8. set x[j+1]=key
• 9. set i=i+1
• 10. stop
Sample Code—For Tracing Insertion Sort
• For(i=1;i<n;i++)
•{
• Temp=a[i]
• j-=i-1;
• While(j>=0 && a[j]>temp)
•{
• A[j+1]=a[j];
• j--;
• }a[j+1]=temp;}
Bubble sort
• Bubble sort is based on the idea of repeatedly comparing pairs of
adjacent elements and then swapping their positions if they exist in the
wrong order.
• Idea:
• Repeatedly pass through the array
• Swaps adjacent elements that are out of order

• Note: Easier to implement, but slower than Insertion sort


Algorithm
• Bubblesort(int x[ ],int n)
• 1. start
• 2. set i=0
• 3. repeat steps 4,5 and 8 while(i<n-1)
• 4. j=i+1
• 5. repeat steps 6,7 while(j<n)
• 6. if x[j]<x[i]
• temp=x[i] //exchange x[i] and x[j]
• x[i]=x[j]
• x[j]=temp
• 7. set j=j+1
• 8. set i=i+1
• 9. stop
• If n elements are given in memory then for sorting we do following steps:
• If n elements are given in memory then for sorting we do following steps:
1. First compare the 1st and 2nd element of array if 1st <2nd then compare the 2nd with
3rd
2. If 2nd >3rd
Then interchange the value of 2nd and 3rd.

3. Now compare the value of 3rd (which has the value of 2nd) with 4th
4. Similarly compare until the (n-1)th element is compared with nth element.
5. Now the highest value element is reached at the nth place.
6. Now elements will be compared until n-1 elements.
Sample Snippet—Tracing code for bubble
sort
• For(i=0;i<n-1;i++)
•{
• For(j=0;j<n-1-i ;j++)
•{
• If(A[j]>A[j+1])
• {temp=a[j];
• A[j]=a[j+1]
• A[j+1]=temp;} }}
Practise Problem
• 42 23 74 11 65 58 94 36 99 87
• Sort above array by using bubble sort
• Insertion Sort
• Trace the sorting by referring the tracing code
Selection sort
• The Selection sort algorithm is based on the idea of finding the
minimum or maximum element in an unsorted array and then putting
it in its correct position in a sorted array.

• Idea:
• Find the smallest element in the array
• Exchange it with the element in the first position
• Find the second smallest element and exchange it with the element
in the second position
• Continue until the array is sorted
Algorithm:

Selection(int x[ ],int n)
1. start
2. set i=0
3. repeat steps 4,5 and 7 while(i<n-1)
4. set min=i and set j=i+1
5. repeat the steps 6 while(j<n)
6. if(x[j]<x[min]) set min=j
7. temp=x[i]
x[i]=x[min]
x[min]=temp
8. stop
Selection sort Code Snippet:
Tracing code:
Merge sort
Merge sort is a divide-and-conquer algorithm based on the idea
of breaking down a list into several sub-lists until each sublist
consists of a single element and merging those sublists in a
manner that results into a sorted list.

Idea:
• Divide the unsorted list into N sublists, each
containing 1 element.
• Take adjacent pairs of two singleton lists and merge them to
form a list of 2 elements. N will now convert into N/2 lists of
size 2.
• Repeat the process till a single sorted list of obtained. 61
62
63
Example1

64
Algorithm:

65
Quick sort

Quick sort is based on the divide-and-conquer approach.


Idea:
• choosing one element as a pivot element and partitioning
the array around it
such that:
• Left side of pivot contains all the elements that are less than
the pivot element
• Right side contains all elements greater than the pivot

66
Implementation :
Select the first element of array as the pivot element First, we will see how the
partition of the array takes place around the pivot.

67
Example1

68
Example1

69
Quick Sort Algorithm

partition(a, m, n)
1.int pivot = a[m];
2. int i=m;
3. int j=n;
4. do repeat steps 5-7 while (i < j)
5. do i++; while ( a[i] < pivot ) is true
6. do j--; while ( a[j] > pivot ) is true
7. if ( i < j ) interchange a[i] and a[j]
8. a[m]=a[j]
9. a[j]=pivot;
10. return j;
Algorithm:

71

You might also like