SlideShare a Scribd company logo
COMPUTER
PROGRAMMING
Lecture 05
Prepared By Mr. V. S. Patil Dept (CSE)/AEC
Unit 1 Problem Solving
Syllabus
Prepared By Mr. V. S. Patil Dept (CSE)/AEC
2
1
Understand insertion sort algorithms
Bubble sort algorithms
Objectives
Sorting
•Sorting is defined as arranging the record in some logical manner.
•Sorted = ordered based on a particular way.
•Generally, collections of data are presented in a sorted manner.
•Examples of Sorting:
–Words in a dictionary are sorted (and case distinctions are ignored).
–Files in a directory are often listed in sorted order.
–The index of a book is sorted (and case distinctions are ignored).
•InsertIon sort
•BuBBle sort
Insertion sort
•Suppose an array A with n element A[1], A[2], A[1],…A[n] is
in the memory.
The insertion sort algorithm scans A from A[1] to A[n]
inserting each element A[k] into its proper position in the
previously Sorted sub array A[1], A[2],…. A[k-1].
Cont…….
•That is:-
Pass 1: A[1] by itself is trivially sorted.
Pass 2: A[2] is inserted either before or after A[1] so
that : A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1],
A[2], that is before A[1], between A[1] and A[2], or after
A[2] so that A[1] ,A[2], A[3] is sorted.
Continue..
Pass 4: A[4] is inserted into its proper place in A[1]
,A[2] ,A[3] so that A[1] A[2] A[3] A[4] is sorted.
.
.
.
Pass [n]: A[n] is inserted into its proper place in A[1],
A[2], A[3]…….. A[n-1] so that A[1], A[2]…….. A[n] is
sorted.
2
1
3
4
6
5
Set A[0]= - ~ [Initialize sentinel element]
Repeat step 3 to 5 for k=2,3,…n
Set TEMP:=A[k] and PTR:=k-1
Repeat while TEMP< A[PTR]
a)Set A[PTR+1]:=A[PTR] [MOVES ELEMENT
FORWARD]
b)Set PTR:= PTR-1 [End of loop]
Set A[PTR+1]:=TEMP [Insert element in to its proper place]
ReturnStop
Algorithm Development
(Insertion sort) INSERTION (A,N)
This algorithm sort the array A with N element.
Example: sorting numberes
23 17 45 18 12 22
Consider the list of unsorted numbersConsider the list of unsorted numbers
Example: sorting numbered cards
23 17 45 18 12 22
1 2 6543
Unsorted List
Example: sorting numbered cards
23 17 45 18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
23
17 45 18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
2317
45 18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
2317 45
18 12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
2317 4518
12 22
1 2 6543
1 2 6543
Unsorted List
Sorted List
Example: sorting numbered cards
1812 2217 23 45
1 2 6543
1 2 6543
Sorted List
Bubble Sort: Idea
• Idea: bubble in water.
Bubble in water moves upward. Why?
• How?
–When a bubble moves upward, the water from above will
move downward to fill in the space left by the bubble.
Bubble sort
Compare each element (except the last two) with its
neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final
places
Compare each element (except the last one) with its neighbor
to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on
the left
Continue..
Example of bubble sort
7 2 8 5 4
2 7 8 5 4
2 7 8 5 4
2 7 5 8 4
2 7 5 4 8
2 7 5 4 8
2 7 5 4 8
2 5 7 4 8
2 5 4 7 8
2 5 4 7 8
2 5 4 7 8
2 4 5 7 8
2 4 5 7 8
2 4 5 7 8
(done)
•Let A be the list of N number. Sorting A refers to the
operation of re-arranging the element of A so that they
are in increasing order,
So that :- A[1] <A[2]< A[3] <……….A[N].
•For example suppose A originally the list.
8, 4, 19, 2, 7, 13, 5, 16.
After sorting A the list is
2, 4, 5, 7, 8, 13, 16, 19.
Bubble sort:-
1
1.Repeat
step 2and
3for k=1 to
N-1
2
Set PTER=1
[Initialize
pass
pointer
PTER].
3
Repeat
while
PTR<=N-K
[Executes
pass].
.
4
a)If DATA
[PTR]>DATA[PTR+1]
Interchange
DATA[PTER]
and DATA[PTR+1]
[End of if structure]
5
b)Set
PTR:=PTR+1
[End of inner
loop]
[End of step 1
outer loop]
6
Exit
Return
Stop
(Bubble sort) BUBBLE(Data, N)
Here DATA is an array with N element. This algorithm sort the
elements in DATA.
Question Bank
1. What is mean by sorting?
2. Draw the flowchart for insertion sort.
3. Write process to sort following list using Bubble sort.
12 45 34 67 98 42 23 7 87

More Related Content

What's hot

Linked list
Linked listLinked list
Linked list
CS Simple education
 
Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists | Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists |
FabMinds
 
6.queue
6.queue6.queue
6.queue
Chandan Singh
 
Array 2
Array 2Array 2
Array 2
Abbott
 
Sorting
SortingSorting
Counting sort
Counting sortCounting sort
Counting sort
zahraa F.Muhsen
 
Arrays
ArraysArrays
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
SORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortSORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix Sort
Devanshu Taneja
 
Log properties
Log propertiesLog properties
Log properties
mlobrien15
 
Log properties
Log propertiesLog properties
Log properties
mlobrien15
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithms
SiddhantShelake
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Emmanuel college
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
Module 01 Stack and Recursion
Module 01 Stack and RecursionModule 01 Stack and Recursion
Module 01 Stack and Recursion
Tushar B Kute
 
Presentation
PresentationPresentation
Presentation
Sayed Hoque
 

What's hot (20)

Linked list
Linked listLinked list
Linked list
 
Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists | Python Programming | JNTUA | UNIT 3 | Lists |
Python Programming | JNTUA | UNIT 3 | Lists |
 
6.queue
6.queue6.queue
6.queue
 
Array 2
Array 2Array 2
Array 2
 
Sorting
SortingSorting
Sorting
 
Counting sort
Counting sortCounting sort
Counting sort
 
Arrays
ArraysArrays
Arrays
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
SORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortSORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix Sort
 
Log properties
Log propertiesLog properties
Log properties
 
Log properties
Log propertiesLog properties
Log properties
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Module 01 Stack and Recursion
Module 01 Stack and RecursionModule 01 Stack and Recursion
Module 01 Stack and Recursion
 
Presentation
PresentationPresentation
Presentation
 

Viewers also liked

COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6
Vishal Patil
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
Vishal Patil
 
Important Questions of fourier series with theoretical study Engg. Mathem...
Important Questions  of  fourier series with theoretical study   Engg. Mathem...Important Questions  of  fourier series with theoretical study   Engg. Mathem...
Important Questions of fourier series with theoretical study Engg. Mathem...
Mohammad Imran
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
Vishal Patil
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
Sucess
SucessSucess
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Shivam Singh
 

Viewers also liked (10)

COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6COMPUTER PROGRAMMING UNIT 1 Lecture 6
COMPUTER PROGRAMMING UNIT 1 Lecture 6
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1
 
Important Questions of fourier series with theoretical study Engg. Mathem...
Important Questions  of  fourier series with theoretical study   Engg. Mathem...Important Questions  of  fourier series with theoretical study   Engg. Mathem...
Important Questions of fourier series with theoretical study Engg. Mathem...
 
COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3COMPUTER PROGRAMMING UNIT 1 Lecture 3
COMPUTER PROGRAMMING UNIT 1 Lecture 3
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Sucess
SucessSucess
Sucess
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Similar to COMPUTER PROGRAMMING UNIT 1 Lecture 5

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
ssuserd602fd
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
Mandeep Singh
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
AJAYVISHALRP
 
Sorting2
Sorting2Sorting2
Sorting2
Saurabh Mishra
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
Chandan Singh
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
student
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
DSA.pdf
DSA.pdfDSA.pdf
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
MuhammadUmerIhtisham
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
Ankur Srivastava
 
arrays
arraysarrays
CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptxCSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
MeghaKulkarni27
 
Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"
Ra'Fat Al-Msie'deen
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 

Similar to COMPUTER PROGRAMMING UNIT 1 Lecture 5 (20)

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Sorting2
Sorting2Sorting2
Sorting2
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
DSA.pdf
DSA.pdfDSA.pdf
DSA.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
arrays
arraysarrays
arrays
 
CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptxCSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 

Recently uploaded

ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
shivani5543
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 

Recently uploaded (20)

ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 

COMPUTER PROGRAMMING UNIT 1 Lecture 5

  • 1. COMPUTER PROGRAMMING Lecture 05 Prepared By Mr. V. S. Patil Dept (CSE)/AEC
  • 2. Unit 1 Problem Solving Syllabus Prepared By Mr. V. S. Patil Dept (CSE)/AEC
  • 3. 2 1 Understand insertion sort algorithms Bubble sort algorithms Objectives
  • 4. Sorting •Sorting is defined as arranging the record in some logical manner. •Sorted = ordered based on a particular way. •Generally, collections of data are presented in a sorted manner. •Examples of Sorting: –Words in a dictionary are sorted (and case distinctions are ignored). –Files in a directory are often listed in sorted order. –The index of a book is sorted (and case distinctions are ignored). •InsertIon sort •BuBBle sort
  • 5. Insertion sort •Suppose an array A with n element A[1], A[2], A[1],…A[n] is in the memory. The insertion sort algorithm scans A from A[1] to A[n] inserting each element A[k] into its proper position in the previously Sorted sub array A[1], A[2],…. A[k-1].
  • 6. Cont……. •That is:- Pass 1: A[1] by itself is trivially sorted. Pass 2: A[2] is inserted either before or after A[1] so that : A[1], A[2] is sorted. Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is before A[1], between A[1] and A[2], or after A[2] so that A[1] ,A[2], A[3] is sorted.
  • 7. Continue.. Pass 4: A[4] is inserted into its proper place in A[1] ,A[2] ,A[3] so that A[1] A[2] A[3] A[4] is sorted. . . . Pass [n]: A[n] is inserted into its proper place in A[1], A[2], A[3]…….. A[n-1] so that A[1], A[2]…….. A[n] is sorted.
  • 8. 2 1 3 4 6 5 Set A[0]= - ~ [Initialize sentinel element] Repeat step 3 to 5 for k=2,3,…n Set TEMP:=A[k] and PTR:=k-1 Repeat while TEMP< A[PTR] a)Set A[PTR+1]:=A[PTR] [MOVES ELEMENT FORWARD] b)Set PTR:= PTR-1 [End of loop] Set A[PTR+1]:=TEMP [Insert element in to its proper place] ReturnStop Algorithm Development (Insertion sort) INSERTION (A,N) This algorithm sort the array A with N element.
  • 9. Example: sorting numberes 23 17 45 18 12 22 Consider the list of unsorted numbersConsider the list of unsorted numbers
  • 10. Example: sorting numbered cards 23 17 45 18 12 22 1 2 6543 Unsorted List
  • 11. Example: sorting numbered cards 23 17 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 12. Example: sorting numbered cards 23 17 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 13. Example: sorting numbered cards 2317 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 14. Example: sorting numbered cards 2317 45 18 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 15. Example: sorting numbered cards 2317 4518 12 22 1 2 6543 1 2 6543 Unsorted List Sorted List
  • 16. Example: sorting numbered cards 1812 2217 23 45 1 2 6543 1 2 6543 Sorted List
  • 17. Bubble Sort: Idea • Idea: bubble in water. Bubble in water moves upward. Why? • How? –When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.
  • 18. Bubble sort Compare each element (except the last two) with its neighbor to the right If they are out of order, swap them This puts the second largest element next to last The last two elements are now in their correct and final places
  • 19. Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place Compare each element (except the last three) with its neighbor to the right Continue as above until you have no unsorted elements on the left Continue..
  • 20. Example of bubble sort 7 2 8 5 4 2 7 8 5 4 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 7 5 4 8 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 5 4 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8 2 4 5 7 8 (done)
  • 21. •Let A be the list of N number. Sorting A refers to the operation of re-arranging the element of A so that they are in increasing order, So that :- A[1] <A[2]< A[3] <……….A[N]. •For example suppose A originally the list. 8, 4, 19, 2, 7, 13, 5, 16. After sorting A the list is 2, 4, 5, 7, 8, 13, 16, 19. Bubble sort:-
  • 22. 1 1.Repeat step 2and 3for k=1 to N-1 2 Set PTER=1 [Initialize pass pointer PTER]. 3 Repeat while PTR<=N-K [Executes pass]. . 4 a)If DATA [PTR]>DATA[PTR+1] Interchange DATA[PTER] and DATA[PTR+1] [End of if structure] 5 b)Set PTR:=PTR+1 [End of inner loop] [End of step 1 outer loop] 6 Exit Return Stop (Bubble sort) BUBBLE(Data, N) Here DATA is an array with N element. This algorithm sort the elements in DATA.
  • 23. Question Bank 1. What is mean by sorting? 2. Draw the flowchart for insertion sort. 3. Write process to sort following list using Bubble sort. 12 45 34 67 98 42 23 7 87