DS Module 1
DS Module 1
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.
• 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
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
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