SlideShare a Scribd company logo
1 of 23
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

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
 
Array 2
Array 2Array 2
Array 2Abbott
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortManishPrajapati78
 
SORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortSORTTING IN LINEAR TIME - Radix Sort
SORTTING IN LINEAR TIME - Radix SortDevanshu Taneja
 
Log properties
Log propertiesLog properties
Log propertiesmlobrien15
 
Log properties
Log propertiesLog properties
Log propertiesmlobrien15
 
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 recurrencesjayavignesh86
 
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 algorithmsSiddhantShelake
 
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 RecursionTushar B Kute
 

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 6Vishal Patil
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATILVishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1COMPUTER PROGRAMMING UNIT 1 Lecture 1
COMPUTER PROGRAMMING UNIT 1 Lecture 1Vishal 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 3Vishal Patil
 
COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2COMPUTER PROGRAMMING UNIT 1 Lecture 2
COMPUTER PROGRAMMING UNIT 1 Lecture 2Vishal Patil
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsShivam 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.pptxssuserd602fd
 
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 JYOTHISowmya Jyothi
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash 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-14sumitbardhan
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
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.pptxchouguleamruta24
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ 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

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 

Recently uploaded (20)

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.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