SlideShare a Scribd company logo
Data Structure
M.Sc. Computer Science
I Semester
Ms. Arati Singh
Department of Computer Science
Unit–V
DataStructure-SortingTechniques
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to
arrange data in a particular order. Most common orders are in numerical or lexicographical
order.
The importance of sorting lies in the fact that data searching can be optimized to a very high
level, if data is stored in a sorted manner. Sorting is also used to represent data in more
readable formats. Following are some of the examples of sorting in real-life scenarios −
• Telephone Directory − The telephone directory stores the telephone numbers of
people sorted by their names, so that the names can be searched easily.
• Dictionary − The dictionary stores words in an alphabetical order so that searching of
any word becomes easy.
In-placeSortingandNot-in-placeSorting
Sorting algorithms may require some extra space for comparison and temporary storage of
few data elements. These algorithms do not require any extra space and sorting is said to
happen in-place, or for example, within the array itself. This is called in-place sorting. Bubble
sort is an example of in-place sorting.
However, in some sorting algorithms, the program requires space which is more than or equal
to the elements being sorted. Sorting which uses equal or more space is called not-in-place
sorting. Merge-sort is an example of not-in-place sorting.
StableandNotStableSorting
If a sorting algorithm, after sorting the contents, does not change the sequence of similar
content in which they appear, it is called stable sorting.
If a sorting algorithm, after sorting the contents, changes the sequence of similar content in
which they appear, it is called unstable sorting.
Stability of an algorithm matters when we wish to maintain the sequence of original elements,
like in a tuple for example.
AdaptiveandNon-AdaptiveSortingAlgorithm
A sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted' elements in
the list that is to be sorted. That is, while sorting if the source list has some element already
sorted, adaptive algorithms will take this into account and will try not to re-order them.
A non-adaptive algorithm is one which does not take into account the elements which are
already sorted. They try to force every single element to be re-ordered to confirm their
sortedness.
ImportantTerms
Some terms are generally coined while discussing sorting techniques, here is a brief
introduction to them −
Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater
than the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next
element is greater than the previous element.
Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than
the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is
less than the previous element.
Non-Increasing Order
A sequence of values is said to be in non-increasing order, if the successive element is less
than or equal to its previous element in the sequence. This order occurs when the sequence
contains duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every
next element is less than or equal to (in case of 3) but not greater than any previous element.
Non-Decreasing Order
A sequence of values is said to be in non-decreasing order, if the successive element is
greater than or equal to its previous element in the sequence. This order occurs when the
sequence contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order,
as every next element is greater than or equal to (in case of 3) but not less than the previous
one.
InsertionSort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which
is always sorted. For example, the lower part of an array is maintained to be sorted. An element
which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has
to be inserted there. Hence the name, insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into the sorted
sub-list (in the same array). 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.
HowInsertionSortWorks?
We take an unsorted array for our example.
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.
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.
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.
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.
Algorithm
Now we have a bigger picture of how this sorting technique works, so we can derive simple
steps by which we can achieve insertion sort.
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
SelectionSort
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and
the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the
entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array. This process continues moving
unsorted array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are
of Ο(n2
), where n is the number of items.
HowSelectionSortWorks?
Consider the following depicted array as an example.
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.
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.
The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process −
Now, let us learn some programming aspects of selection sort.
Algorithm
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
Pseudocode
procedure selection sort
1. list : array of items
2. n : size of list
3. for i = 1 to n - 1
4. /* set current element as minimum*/
5. min = i
6. /* check the element to be minimum */
7. for j = i+1 to n
8. if list[j] < list[min] then
9. min = j;
10. end if
11. end for
12. /* swap the minimum element with the current element*/
13. if indexMin != i then
14. swap list[min] and list[i]
15. end if
16. end for
17. end procedure
MergeSortAlgorithm
Merge sort is a sorting technique based on divide and conquer technique. With worst-case
time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
HowMergeSortWorks?
To understand merge sort, we take an unsorted array as the following −
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.
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.
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 −
Now we should learn some programming aspects of merge sorting.
Algorithm
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.
Pseudocode
We shall now see the pseudocodes for merge sort functions. As our algorithms point out two
main functions − divide & merge.
Merge sort works with recursion and we shall see our implementation in the same way.
procedure mergesort( var a as array )
if ( n == 1 ) return a
var l1 as array = a[0] ... a[n/2]
var l2 as array = a[n/2+1] ... a[n]
l1 = mergesort( l1 )
l2 = mergesort( l2 )
return merge( l1, l2 )
end procedure
procedure merge( var a as array, var b as array )
var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while
while ( a has elements )
add a[0] to the end of c
remove a[0] from a
end while
while ( b has elements )
add b[0] to the end of c
remove b[0] from b
end while
return c
end procedure
RadixSort
Radix sort is a small method that many people intuitively use when alphabetizing a large list
of names. Specifically, the list of names is first sorted according to the first letter of each
name, that is, the names are arranged in 26 classes.
Intuitively, one might want to sort numbers on their most significant digit. However, Radix
sort works counter-intuitively by sorting on the least significant digits first. On the first pass,
all the numbers are sorted on the least significant digit and combined in an array. Then on the
second pass, the entire numbers are sorted again on the second least significant digits and
combined in an array and so on.
Algorithm: Radix-Sort (list, n)
shift = 1
for loop = 1 to keysize do
for entry = 1 to n do
bucketnumber = (list[entry].key / shift) mod 10
append (bucket[bucketnumber], list[entry])
list = combinebuckets()
shift = shift * 10
Analysis
Each key is looked at once for each digit (or letter if the keys are alphabetic) of the longest
key. Hence, if the longest key has m digits and there are n keys, radix sort has order O(m.n).
However, if we look at these two values, the size of the keys will be relatively small when
compared to the number of keys. For example, if we have six-digit keys, we could have a
million different records.
Here, we see that the size of the keys is not significant, and this algorithm is of linear
complexity O(n).
Example
Following example shows how Radix sort operates on seven 3-digits number.
Input 1st Pass 2nd Pass 3rd Pass
329 720 720 329
457 355 329 355
657 436 436 436
839 457 839 457
436 657 355 657
720 329 457 720
355 839 657 839
In the above example, the first column is the input. The remaining columns show the list after
successive sorts on increasingly significant digits position. The code for Radix sort assumes
that each element in an array A of n elements has d digits, where digit 1 is the lowest-order
digit and d is the highest-order digit.
DataStructureandAlgorithms-HashTable
Hash Table is a data structure which stores data in an associative manner. In a hash table, data
is stored in an array format, where each data value has its own unique index value. Access of
data becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses
hash technique to generate an index where an element is to be inserted or is to be located from.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array.
We're going to use modulo operator to get a range of key values. Consider an example of hash
table of size 20, and the following items are to be stored. Item are in the (key, value) format.
• (1,20)
• (2,70)
• (42,80)
• (4,25)
• (12,44)
• (14,32)
• (17,11)
• (13,78)
• (37,98)
Key Hash Array Index
1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17
LinearProbing
As we can see, it may happen that the hashing technique is used to create an already used
index of the array. In such a case, we can search the next empty location in the array by looking
into the next cell until we find an empty cell. This technique is called linear probing.
S.No. Key Hash Array Index
After Linear Probing, Array
Index
1 1 1 % 20 = 1 1 1
2 2 2 % 20 = 2 2 2
3 42 42 % 20 = 2 2 3
4 4 4 % 20 = 4 4 4
5 12 12 % 20 = 12 12 12
6 14 14 % 20 = 14 14 14
7 17 17 % 20 = 17 17 17
8 13 13 % 20 = 13 13 13
9 37 37 % 20 = 17 17 18
BasicOperations
Following are the basic primary operations of a hash table.
• Search − Searches an element in a hash table.
• Insert − inserts an element in a hash table.
• delete − Deletes an element from a hash table.
DataItem
Define a data item having some data and key, based on which the search is to be conducted in
a hash table.
struct DataItem {
int data;
int key;
};
HashMethod
Define a hashing method to compute the hash code of the key of the data item.
int hashCode(int key){
return key % SIZE;
}
SearchOperation
Whenever an element is to be searched, compute the hash code of the key passed and locate
the element using that hash code as index in the array. Use linear probing to get the element
ahead if the element is not found at the computed hash code.
Example
struct DataItem *search(int key) {
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
InsertOperation
Whenever an element is to be inserted, compute the hash code of the key passed and locate
the index using that hash code as an index in the array. Use linear probing for empty location,
if an element is found at the computed hash code.
Example
void insert(int key,int data) {
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
DeleteOperation
Whenever an element is to be deleted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing to get the element ahead
if an element is not found at the computed hash code. When found, store a dummy item there
to keep the performance of the hash table intact.
Example
struct DataItem* delete(struct DataItem* item) {
int key = item->key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] !=NULL) {
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
Reference
https://keithlyons.me/blog/2018/11/07/el30-graphing/
https://www.tutorialspoint.com/data_structures_algorithms/index.htm
https://www.geeksforgeeks.org/data-structures/

More Related Content

PDF
Sorting
A. S. M. Shafi
 
PPT
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
PPTX
Searching
Ashim Lamichhane
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PPT
Searching in c language
CHANDAN KUMAR
 
PPTX
Sorting types and Algorithms
Ali Khan
 
PPTX
Sorting
Ashim Lamichhane
 
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Searching
Ashim Lamichhane
 
Sorting algorithms
CHANDAN KUMAR
 
Searching in c language
CHANDAN KUMAR
 
Sorting types and Algorithms
Ali Khan
 

What's hot (20)

PPTX
Sorting method data structure
sunilchute1
 
PPTX
Selection sort and insertion sort
May Ann Mendoza
 
PPTX
Selection sorting
Himanshu Kesharwani
 
DOCX
Sorting
BHARATH KUMAR
 
PPTX
Training excel
Ravi Rai
 
PPTX
Selection sort
smlagustin
 
PPTX
Searching techniques
Archana Burujwale
 
PPTX
Search methods
zahraa F.Muhsen
 
PPTX
Selection and insertion sort
Ann Tugade
 
PPTX
Selection and insertion sort
Ann Tugade
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPTX
sorting and its types
SIVASHANKARIRAJAN
 
PPT
Selection sort
stella D
 
PDF
Binary search algorithm
maamir farooq
 
PPTX
Java Chapter 05 - Conditions & Loops: part 4
DanWooster1
 
PPTX
Sorting And Type of Sorting
MITULJAMANG
 
PPT
Binary Search
kunj desai
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPTX
Insertion sort
MYER301
 
PPTX
7 searching injava-binary
irdginfo
 
Sorting method data structure
sunilchute1
 
Selection sort and insertion sort
May Ann Mendoza
 
Selection sorting
Himanshu Kesharwani
 
Sorting
BHARATH KUMAR
 
Training excel
Ravi Rai
 
Selection sort
smlagustin
 
Searching techniques
Archana Burujwale
 
Search methods
zahraa F.Muhsen
 
Selection and insertion sort
Ann Tugade
 
Selection and insertion sort
Ann Tugade
 
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
sorting and its types
SIVASHANKARIRAJAN
 
Selection sort
stella D
 
Binary search algorithm
maamir farooq
 
Java Chapter 05 - Conditions & Loops: part 4
DanWooster1
 
Sorting And Type of Sorting
MITULJAMANG
 
Binary Search
kunj desai
 
linear search and binary search
Zia Ush Shamszaman
 
Insertion sort
MYER301
 
7 searching injava-binary
irdginfo
 
Ad

Similar to Unit v data structure-converted (20)

PPTX
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
PPTX
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PDF
Sorting
A. S. M. Shafi
 
PPTX
Selection Sort and Insertion Sort
tamayaoraquel
 
PDF
advanced searching and sorting.pdf
haramaya university
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PPTX
Sorting
Saharamily
 
PPTX
arrays in c
vidhi mehta
 
PPTX
Searching and sorting
PoojithaBollikonda
 
PPTX
What is a Sorting Algorithm Ishank mini project.pptx
SneakyGoblin1
 
PDF
Ijcse13 05-01-048
vital vital
 
PDF
Ijcse13 05-01-048
vital vital
 
PPTX
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPTX
Searching,sorting
LavanyaJ28
 
PPTX
Data Structures and Algorithms
Mark Jade Barrientos
 
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
Selection Sort & Insertion Sorts Algorithms
Ahmad177077
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Selection Sort and Insertion Sort
tamayaoraquel
 
advanced searching and sorting.pdf
haramaya university
 
MODULE 5-Searching and-sorting
nikshaikh786
 
Sorting
Saharamily
 
arrays in c
vidhi mehta
 
Searching and sorting
PoojithaBollikonda
 
What is a Sorting Algorithm Ishank mini project.pptx
SneakyGoblin1
 
Ijcse13 05-01-048
vital vital
 
Ijcse13 05-01-048
vital vital
 
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
Searching & Sorting Algorithms
Rahul Jamwal
 
Searching,sorting
LavanyaJ28
 
Data Structures and Algorithms
Mark Jade Barrientos
 
Ad

More from Shri Shankaracharya College, Bhilai,Junwani (20)

PPT
Environment Economics &Ethics invisible hand & Malthusian theory
Shri Shankaracharya College, Bhilai,Junwani
 
PPT
Azadi ka amrut mahotsav, Mahilayon ka yogdan swatantrata Sangram mein
Shri Shankaracharya College, Bhilai,Junwani
 
PDF
B.ed 1,scientific temper
Shri Shankaracharya College, Bhilai,Junwani
 
PDF
Aims and objectives of bio. sci. 14 9-20
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Ict application in bio.sc.24 9
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Isolation & preservation of culture of microorganism
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Learners understanding,unit 1, 15-9-20
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Basics concept of physical chemistry
Shri Shankaracharya College, Bhilai,Junwani
 

Recently uploaded (20)

PPTX
Quality control test for plastic & metal.pptx
shrutipandit17
 
PDF
Identification of unnecessary object allocations using static escape analysis
ESUG
 
PPTX
General Characters and Classification of Su class Apterygota.pptx
Dr Showkat Ahmad Wani
 
PPTX
Brain_stem_Medulla oblongata_functions of pons_mid brain
muralinath2
 
PPTX
The Toxic Effects of Aflatoxin B1 and Aflatoxin M1 on Kidney through Regulati...
OttokomaBonny
 
PDF
Paleoseismic activity in the moon’s Taurus-Littrowvalley inferred from boulde...
Sérgio Sacani
 
PPTX
Cell Structure and Organelles Slides PPT
JesusNeyra8
 
PPTX
fghvqwhfugqaifbiqufbiquvbfuqvfuqyvfqvfouiqvfq
PERMISONJERWIN
 
PDF
Sujay Rao Mandavilli Multi-barreled appraoch to educational reform FINAL FINA...
Sujay Rao Mandavilli
 
PPTX
Home Garden as a Component of Agroforestry system : A survey-based Study
AkhangshaRoy
 
PDF
Challenges of Transpiling Smalltalk to JavaScript
ESUG
 
PPTX
INTRO-TO-CRIM-THEORIES-OF-CRIME-2023 (1).pptx
ChrisFlickIII
 
PPTX
Modifications in RuBisCO system to enhance photosynthesis .pptx
raghumolbiotech
 
PPTX
Feeding stratagey for climate change dairy animals.
Dr.Zulfy haq
 
PPTX
first COT (MATH).pptxCSAsCNKHPHCouAGSCAUO:GC/ZKVHxsacba
DitaSIdnay
 
PPTX
Laboratory design and safe microbiological practices
Akanksha Divkar
 
PPTX
Qualification of.UV visible spectrophotometer pptx
shrutipandit17
 
PPTX
Hericium erinaceus, also known as lion's mane mushroom
TinaDadkhah1
 
PPTX
INTERNATIONAL CLASSIFICATION OF DISEASES ji.pptx
46JaybhayAshwiniHari
 
PDF
Approximating manifold orbits by means of Machine Learning Techniques
Esther Barrabés Vera
 
Quality control test for plastic & metal.pptx
shrutipandit17
 
Identification of unnecessary object allocations using static escape analysis
ESUG
 
General Characters and Classification of Su class Apterygota.pptx
Dr Showkat Ahmad Wani
 
Brain_stem_Medulla oblongata_functions of pons_mid brain
muralinath2
 
The Toxic Effects of Aflatoxin B1 and Aflatoxin M1 on Kidney through Regulati...
OttokomaBonny
 
Paleoseismic activity in the moon’s Taurus-Littrowvalley inferred from boulde...
Sérgio Sacani
 
Cell Structure and Organelles Slides PPT
JesusNeyra8
 
fghvqwhfugqaifbiqufbiquvbfuqvfuqyvfqvfouiqvfq
PERMISONJERWIN
 
Sujay Rao Mandavilli Multi-barreled appraoch to educational reform FINAL FINA...
Sujay Rao Mandavilli
 
Home Garden as a Component of Agroforestry system : A survey-based Study
AkhangshaRoy
 
Challenges of Transpiling Smalltalk to JavaScript
ESUG
 
INTRO-TO-CRIM-THEORIES-OF-CRIME-2023 (1).pptx
ChrisFlickIII
 
Modifications in RuBisCO system to enhance photosynthesis .pptx
raghumolbiotech
 
Feeding stratagey for climate change dairy animals.
Dr.Zulfy haq
 
first COT (MATH).pptxCSAsCNKHPHCouAGSCAUO:GC/ZKVHxsacba
DitaSIdnay
 
Laboratory design and safe microbiological practices
Akanksha Divkar
 
Qualification of.UV visible spectrophotometer pptx
shrutipandit17
 
Hericium erinaceus, also known as lion's mane mushroom
TinaDadkhah1
 
INTERNATIONAL CLASSIFICATION OF DISEASES ji.pptx
46JaybhayAshwiniHari
 
Approximating manifold orbits by means of Machine Learning Techniques
Esther Barrabés Vera
 

Unit v data structure-converted

  • 1. Data Structure M.Sc. Computer Science I Semester Ms. Arati Singh Department of Computer Science
  • 2. Unit–V DataStructure-SortingTechniques Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order. Most common orders are in numerical or lexicographical order. The importance of sorting lies in the fact that data searching can be optimized to a very high level, if data is stored in a sorted manner. Sorting is also used to represent data in more readable formats. Following are some of the examples of sorting in real-life scenarios − • Telephone Directory − The telephone directory stores the telephone numbers of people sorted by their names, so that the names can be searched easily. • Dictionary − The dictionary stores words in an alphabetical order so that searching of any word becomes easy. In-placeSortingandNot-in-placeSorting Sorting algorithms may require some extra space for comparison and temporary storage of few data elements. These algorithms do not require any extra space and sorting is said to happen in-place, or for example, within the array itself. This is called in-place sorting. Bubble sort is an example of in-place sorting. However, in some sorting algorithms, the program requires space which is more than or equal to the elements being sorted. Sorting which uses equal or more space is called not-in-place sorting. Merge-sort is an example of not-in-place sorting. StableandNotStableSorting If a sorting algorithm, after sorting the contents, does not change the sequence of similar content in which they appear, it is called stable sorting. If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which they appear, it is called unstable sorting.
  • 3. Stability of an algorithm matters when we wish to maintain the sequence of original elements, like in a tuple for example. AdaptiveandNon-AdaptiveSortingAlgorithm A sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted' elements in the list that is to be sorted. That is, while sorting if the source list has some element already sorted, adaptive algorithms will take this into account and will try not to re-order them. A non-adaptive algorithm is one which does not take into account the elements which are already sorted. They try to force every single element to be re-ordered to confirm their sortedness. ImportantTerms Some terms are generally coined while discussing sorting techniques, here is a brief introduction to them − Increasing Order A sequence of values is said to be in increasing order, if the successive element is greater than the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is greater than the previous element. Decreasing Order A sequence of values is said to be in decreasing order, if the successive element is less than the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less than the previous element. Non-Increasing Order A sequence of values is said to be in non-increasing order, if the successive element is less than or equal to its previous element in the sequence. This order occurs when the sequence contains duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every next element is less than or equal to (in case of 3) but not greater than any previous element.
  • 4. Non-Decreasing Order A sequence of values is said to be in non-decreasing order, if the successive element is greater than or equal to its previous element in the sequence. This order occurs when the sequence contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every next element is greater than or equal to (in case of 3) but not less than the previous one. InsertionSort This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). 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. HowInsertionSortWorks? We take an unsorted array for our example. 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. And finds that 33 is not in the correct position.
  • 5. 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. 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. We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
  • 6. 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. Algorithm Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by which we can achieve insertion sort. 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 SelectionSort Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison- based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2 ), where n is the number of items. HowSelectionSortWorks? Consider the following depicted array as an example. 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.
  • 7. 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. The same process is applied to the rest of the items in the array. Following is a pictorial depiction of the entire sorting process −
  • 8. Now, let us learn some programming aspects of selection sort.
  • 9. Algorithm 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 Pseudocode procedure selection sort 1. list : array of items 2. n : size of list 3. for i = 1 to n - 1 4. /* set current element as minimum*/ 5. min = i 6. /* check the element to be minimum */ 7. for j = i+1 to n 8. if list[j] < list[min] then 9. min = j; 10. end if 11. end for 12. /* swap the minimum element with the current element*/ 13. if indexMin != i then 14. swap list[min] and list[i] 15. end if 16. end for 17. end procedure
  • 10. MergeSortAlgorithm Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms. Merge sort first divides the array into equal halves and then combines them in a sorted manner. HowMergeSortWorks? To understand merge sort, we take an unsorted array as the following − 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. 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. 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.
  • 11. After the final merging, the list should look like this − Now we should learn some programming aspects of merge sorting. Algorithm 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. Pseudocode We shall now see the pseudocodes for merge sort functions. As our algorithms point out two main functions − divide & merge. Merge sort works with recursion and we shall see our implementation in the same way.
  • 12. procedure mergesort( var a as array ) if ( n == 1 ) return a var l1 as array = a[0] ... a[n/2] var l2 as array = a[n/2+1] ... a[n] l1 = mergesort( l1 ) l2 = mergesort( l2 ) return merge( l1, l2 ) end procedure procedure merge( var a as array, var b as array ) var c as array while ( a and b have elements ) if ( a[0] > b[0] ) add b[0] to the end of c remove b[0] from b else add a[0] to the end of c remove a[0] from a end if end while while ( a has elements ) add a[0] to the end of c
  • 13. remove a[0] from a end while while ( b has elements ) add b[0] to the end of c remove b[0] from b end while return c end procedure RadixSort Radix sort is a small method that many people intuitively use when alphabetizing a large list of names. Specifically, the list of names is first sorted according to the first letter of each name, that is, the names are arranged in 26 classes. Intuitively, one might want to sort numbers on their most significant digit. However, Radix sort works counter-intuitively by sorting on the least significant digits first. On the first pass, all the numbers are sorted on the least significant digit and combined in an array. Then on the second pass, the entire numbers are sorted again on the second least significant digits and combined in an array and so on. Algorithm: Radix-Sort (list, n) shift = 1 for loop = 1 to keysize do for entry = 1 to n do bucketnumber = (list[entry].key / shift) mod 10 append (bucket[bucketnumber], list[entry]) list = combinebuckets() shift = shift * 10
  • 14. Analysis Each key is looked at once for each digit (or letter if the keys are alphabetic) of the longest key. Hence, if the longest key has m digits and there are n keys, radix sort has order O(m.n). However, if we look at these two values, the size of the keys will be relatively small when compared to the number of keys. For example, if we have six-digit keys, we could have a million different records. Here, we see that the size of the keys is not significant, and this algorithm is of linear complexity O(n). Example Following example shows how Radix sort operates on seven 3-digits number. Input 1st Pass 2nd Pass 3rd Pass 329 720 720 329 457 355 329 355 657 436 436 436 839 457 839 457 436 657 355 657 720 329 457 720 355 839 657 839 In the above example, the first column is the input. The remaining columns show the list after successive sorts on increasingly significant digits position. The code for Radix sort assumes that each element in an array A of n elements has d digits, where digit 1 is the lowest-order digit and d is the highest-order digit. DataStructureandAlgorithms-HashTable
  • 15. Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data. Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash technique to generate an index where an element is to be inserted or is to be located from. Hashing Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the following items are to be stored. Item are in the (key, value) format. • (1,20) • (2,70) • (42,80) • (4,25) • (12,44) • (14,32) • (17,11) • (13,78) • (37,98) Key Hash Array Index 1 1 1 % 20 = 1 1 2 2 2 % 20 = 2 2 3 42 42 % 20 = 2 2
  • 16. 4 4 4 % 20 = 4 4 5 12 12 % 20 = 12 12 6 14 14 % 20 = 14 14 7 17 17 % 20 = 17 17 8 13 13 % 20 = 13 13 9 37 37 % 20 = 17 17 LinearProbing As we can see, it may happen that the hashing technique is used to create an already used index of the array. In such a case, we can search the next empty location in the array by looking into the next cell until we find an empty cell. This technique is called linear probing. S.No. Key Hash Array Index After Linear Probing, Array Index 1 1 1 % 20 = 1 1 1 2 2 2 % 20 = 2 2 2 3 42 42 % 20 = 2 2 3 4 4 4 % 20 = 4 4 4 5 12 12 % 20 = 12 12 12 6 14 14 % 20 = 14 14 14
  • 17. 7 17 17 % 20 = 17 17 17 8 13 13 % 20 = 13 13 13 9 37 37 % 20 = 17 17 18 BasicOperations Following are the basic primary operations of a hash table. • Search − Searches an element in a hash table. • Insert − inserts an element in a hash table. • delete − Deletes an element from a hash table. DataItem Define a data item having some data and key, based on which the search is to be conducted in a hash table. struct DataItem { int data; int key; }; HashMethod Define a hashing method to compute the hash code of the key of the data item. int hashCode(int key){ return key % SIZE; } SearchOperation Whenever an element is to be searched, compute the hash code of the key passed and locate the element using that hash code as index in the array. Use linear probing to get the element ahead if the element is not found at the computed hash code.
  • 18. Example struct DataItem *search(int key) { //get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] != NULL) { if(hashArray[hashIndex]->key == key) return hashArray[hashIndex]; //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } return NULL; } InsertOperation Whenever an element is to be inserted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing for empty location, if an element is found at the computed hash code.
  • 19. Example void insert(int key,int data) { struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); item->data = data; item->key = key; //get the hash int hashIndex = hashCode(key); //move in array until an empty or deleted cell while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) { //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } hashArray[hashIndex] = item; } DeleteOperation Whenever an element is to be deleted, compute the hash code of the key passed and locate the index using that hash code as an index in the array. Use linear probing to get the element ahead if an element is not found at the computed hash code. When found, store a dummy item there to keep the performance of the hash table intact.
  • 20. Example struct DataItem* delete(struct DataItem* item) { int key = item->key; //get the hash int hashIndex = hashCode(key); //move in array until an empty while(hashArray[hashIndex] !=NULL) { if(hashArray[hashIndex]->key == key) { struct DataItem* temp = hashArray[hashIndex]; //assign a dummy item at deleted position hashArray[hashIndex] = dummyItem; return temp; } //go to next cell ++hashIndex; //wrap around the table hashIndex %= SIZE; } return NULL; }