SlideShare a Scribd company logo
2
Most read
3
Most read
14
Most read
SORTING METHOD DATA STRUCTURE
PROF. SUNIL D. CHUTE
HEAD DEPT OF COMPUTER SCIENCE
M.G. COLLEGE ARMORI
BUBBLE SORT
• 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.
HOW BUBBLE SORT WORKS?
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're
keeping it short and precise.
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.
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.
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.
And when there's no swap required, bubble sorts learns that an array is completely
sorted.
Now we should look into some practical aspects of bubble sort.
ALGORITHM
• 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
SELECTION SORT ALGORITHM
• 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.
HOW SELECTION SORT WORKS?
• Set the first element as minimum
• Compare minimum with the second element. If the second element is smaller
than minimum, assign the second element as minimum. Compare minimum with the third
element. Again, if the third element is smaller, then assign minimum to the third element
otherwise do nothing. The process goes on until the last element.
• After each iteration, minimum is placed in the front of the unsorted list.
• For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are
repeated until all the elements are placed at their correct positions.
Sorting method data structure
Sorting method data structure
Sorting method data structure
SELECTION SORT ALGORITHM
• selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
INSERTION SORT
• 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.
HOW INSERTION SORT WORKS?
• 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.
INSERTION SORT ALGORITHM
insertionSort(array)
mark first element as sorted
for each unsorted element X
'extract' the element X
for j <- lastSortedIndex down to 0
if current element j > X
move sorted element to the right by 1
break loop and insert X here
end insertionSort

More Related Content

PPTX
Selection sort
PPT
Selection sort
PPTX
Presentation on the topic selection sort
PPTX
single linked list
PPTX
Stack using Array
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
SORTING techniques.pptx
PPTX
Searching and sorting
Selection sort
Selection sort
Presentation on the topic selection sort
single linked list
Stack using Array
Binary Search - Design & Analysis of Algorithms
SORTING techniques.pptx
Searching and sorting

What's hot (20)

PDF
linked lists in data structures
PPTX
Bubble Sort Algorithm Presentation
PPTX
Binary Search Algorithm.pptx
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Binary search
PPTX
B and B+ tree
PDF
linear search and binary search
PPTX
Doubly Linked List
PPTX
linked list in data structure
PPSX
Linear and binary search
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPT
3.2 insertion sort
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
Binary Tree in Data Structure
PPTX
sorting and its types
PPTX
Linked list
PPTX
Sorting algorithms
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
linked list in Data Structure, Simple and Easy Tutorial
linked lists in data structures
Bubble Sort Algorithm Presentation
Binary Search Algorithm.pptx
BINARY TREE REPRESENTATION.ppt
Binary search
B and B+ tree
linear search and binary search
Doubly Linked List
linked list in data structure
Linear and binary search
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
3.2 insertion sort
Data Structures - Lecture 8 [Sorting Algorithms]
Binary Tree in Data Structure
sorting and its types
Linked list
Sorting algorithms
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
linked list in Data Structure, Simple and Easy Tutorial
Ad

Similar to Sorting method data structure (20)

PPTX
Selection and insertion sort
PPTX
Selection and insertion sort
PDF
Unit v data structure-converted
PPTX
Selection Sort and Insertion Sort
PPTX
Selection sort and insertion sort
PPTX
Selection-sort-in-algorithm and complexity.pptx
PPTX
Sorting types and Algorithms
PPTX
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
PPTX
Selection Sort & Insertion Sorts Algorithms
PPTX
Selection Sort & Insertion Sorts Algorithms
PPTX
Searching & Sorting Algorithms
PPTX
sortting 2 sderftgyurvtynurftgyhujse.pptx
PPTX
Data Structures and Algorithms
PDF
advanced searching and sorting.pdf
PPTX
Sorting
PPTX
Sorting Data structure And Algorithm.pptx
PDF
PPTX
Data structure using c module 3
PDF
Ijcse13 05-01-048
Selection and insertion sort
Selection and insertion sort
Unit v data structure-converted
Selection Sort and Insertion Sort
Selection sort and insertion sort
Selection-sort-in-algorithm and complexity.pptx
Sorting types and Algorithms
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
Selection Sort & Insertion Sorts Algorithms
Selection Sort & Insertion Sorts Algorithms
Searching & Sorting Algorithms
sortting 2 sderftgyurvtynurftgyhujse.pptx
Data Structures and Algorithms
advanced searching and sorting.pdf
Sorting
Sorting Data structure And Algorithm.pptx
Data structure using c module 3
Ijcse13 05-01-048
Ad

More from sunilchute1 (10)

PPTX
Programming construction tools
PPTX
Introduction to data structure
PPTX
Introduction to data structure
PPTX
Input output statement
PPTX
Basic data types in python
PPTX
Call by value and call by reference in java
PPTX
Java method
PPTX
Constructors in java
PPTX
C loops
PPT
Basic of c language
Programming construction tools
Introduction to data structure
Introduction to data structure
Input output statement
Basic data types in python
Call by value and call by reference in java
Java method
Constructors in java
C loops
Basic of c language

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Lesson notes of climatology university.
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPH.pptx obstetrics and gynecology in nursing
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Sports Quiz easy sports quiz sports quiz
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
Lesson notes of climatology university.
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial disease of the cardiovascular and lymphatic systems

Sorting method data structure

  • 1. SORTING METHOD DATA STRUCTURE PROF. SUNIL D. CHUTE HEAD DEPT OF COMPUTER SCIENCE M.G. COLLEGE ARMORI
  • 2. BUBBLE SORT • 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.
  • 3. HOW BUBBLE SORT WORKS? We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and precise. 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.
  • 4. We find that 27 is smaller than 33 and these two values must be swapped. 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.
  • 5. 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. And when there's no swap required, bubble sorts learns that an array is completely sorted. Now we should look into some practical aspects of bubble sort.
  • 6. ALGORITHM • 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
  • 7. SELECTION SORT ALGORITHM • 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.
  • 8. HOW SELECTION SORT WORKS? • Set the first element as minimum • Compare minimum with the second element. If the second element is smaller than minimum, assign the second element as minimum. Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.
  • 9. • After each iteration, minimum is placed in the front of the unsorted list. • For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until all the elements are placed at their correct positions.
  • 13. SELECTION SORT ALGORITHM • selectionSort(array, size) repeat (size - 1) times set the first unsorted element as the minimum for each of the unsorted elements if element < currentMinimum set element as new minimum swap minimum with first unsorted position end selectionSort
  • 14. INSERTION SORT • 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.
  • 15. HOW INSERTION SORT WORKS? • 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.
  • 16. • 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.
  • 17. • 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.
  • 18. INSERTION SORT ALGORITHM insertionSort(array) mark first element as sorted for each unsorted element X 'extract' the element X for j <- lastSortedIndex down to 0 if current element j > X move sorted element to the right by 1 break loop and insert X here end insertionSort